Mastering the sudo Command in Linux: A Beginner’s Guide

The sudo command in Linux is one of the most essential tools for system administrators and developers. It allows users to execute commands with superuser privileges securely. Whether you’re managing a server, troubleshooting issues, or installing software, understanding the sudo command in Linux is crucial for efficient system management.

In this guide, we’ll explore what sudo is, how to use it effectively, and best practices to enhance security.

🎥 For a step-by-step visual guide, watch this video:

What is the sudo Command in Linux?

The sudo command in Linux stands for “superuser do” and enables permitted users to execute commands as the root user or another specified user. This allows secure administrative control while limiting full root access.

📝 Tip: Always use sudo responsibly to prevent accidental system modifications.

Check the manual page for sudo using:

man sudo

Basic Syntax of the sudo Command in Linux

The general syntax for using sudo is:

sudo [OPTIONS] COMMAND

Let’s look at some practical use cases of the sudo command in Linux.


🔹 Scenario 1: Installing Software with the sudo Command in Linux

When installing software, administrative privileges are required. For example, installing the Apache web server on Rocky Linux:

sudo dnf install httpd

✅ Running this command without sudo will result in a “permission denied” error.
sudo ensures that only authorized users can install packages.


🔹 Scenario 2: Viewing Logs with Elevated Privileges

Many system logs require root access. To check the secure log for authentication issues, use:

sudo cat /var/log/secure

✅ This is useful for troubleshooting login failures and security concerns.
sudo allows access without switching to the root account.


Useful sudo Options & Their Use Cases in Linux

🔹 1. Start a Root Shell Session (sudo -s)

If you need to run multiple commands with root privileges in an interactive session:

sudo -s

✅ Opens a shell as root without requiring a separate login.

🔹 2. Run a Command as Another User (sudo -u [USER])

Switching to another user without logging out:

sudo -u apache whoami

✅ Output: apache, confirming the command runs as the specified user.

🔹 3. Invalidate a sudo Session (sudo -k)

To force re-authentication for security reasons:

sudo -k

✅ Useful for shared systems to prevent unauthorized access.

🔹 4. Check Your sudo Privileges (sudo -l)

To see which commands you are allowed to execute with sudo:

sudo -l

✅ Helpful for auditing user permissions.


🔹 Scenario 3: Editing System Files Safely with the sudo Command in Linux

Editing system-critical files, like the SSH configuration file, requires sudo:

sudo nano /etc/ssh/sshd_config

✅ Always back up configuration files before making changes!
✅ Restart the SSH service to apply updates:

sudo systemctl restart sshd

Best Practices for Using the sudo Command in Linux Securely

✅ Use sudo only when necessary to minimize security risks.
✅ Regularly audit the sudoers file to control access.
✅ Use sudo visudo to edit the sudoers file safely.

sudo visudo

FAQs About the sudo Command in Linux

1️⃣ Why is sudo better than logging in as root?

Using sudo limits unnecessary root access, improving security and reducing accidental system-wide changes.

2️⃣ How do I add a user to the sudoers file in Linux?

Use the following command:

usermod -aG wheel username

This adds the user to the wheel group, which has sudo privileges.

3️⃣ How can I check the last sudo commands executed?

Run:

sudo journalctl _COMM=sudo

This shows a log of recent sudo activities.

4️⃣ What should I do if sudo stops working?

If you lose sudo access, boot into recovery mode and edit the sudoers file using:

visudo

5️⃣ How do I make sudo remember my password longer?

Modify the timeout in the sudoers file:

Defaults timestamp_timeout=15

This keeps sudo authentication valid for 15 minutes.


Conclusion

Mastering the sudo command in Linux is a fundamental skill for system administrators and developers. It ensures secure execution of administrative tasks while maintaining system integrity.

🔗 Watch the video tutorial for a detailed walkthrough

📌 Next Steps

  • Learn more about user management in Linux
  • Explore system monitoring commands

See also:

List of monitoring tools 

Linux Blogs

AWS Cloud Blogs

Database Blogs

DevOps Blogs

Interview Questions & Answers

Docker Blogs

Google Cloud Blogs







Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.