sudo Command — Running Commands as Another User (Usually Root)¶
The
sudo(Superuser Do) command allows authorized users to execute commands with elevated privileges without logging in as the root user. It is one of the most important security features in Linux and is widely used in enterprise environments, cloud platforms, DevOps pipelines, and system administration.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the purpose of
sudo - Differentiate
sudoandsu - Execute commands with elevated privileges
- Run commands as another user
- Understand the sudoers file
- Configure sudo access
- Troubleshoot sudo-related issues
- Apply sudo security best practices
Prerequisites¶
Complete:
- Module 1 – Linux Fundamentals
- Module 2 – Command Line Essentials
- Module 3 – Text Processing
- Module 4 – File Management and Permissions
- Module 5 Lessons 1–2
Why Learn sudo?¶
Imagine you're managing a production Linux server.
You need to:
- Install software
- Restart services
- Create users
- Modify system files
- Update packages
Should you log in as:
No.
Instead:
This provides better security, accountability, and auditing.
What is sudo?¶
sudo stands for:
Superuser Do
It allows an authorized user to execute commands as:
- Root (default)
- Another user
- Another group
without logging in as that account.
Why Use sudo?¶
Without sudo:
With sudo:
This minimizes the time spent with elevated privileges.
Basic Syntax¶
Example:
or
First-Time Authentication¶
The first sudo command prompts for your own password, not the root password.
Example:
After successful authentication, sudo remembers your credentials for a short period (the timeout is configurable).
Common sudo Examples¶
Update packages.
Install software.
Restart a service.
Edit a system file.
Create a user.
Check Current User¶
Without sudo.
Output:
Run as root.
Output:
Run a Command as Another User¶
Syntax:
Example:
Output:
(Replace nginx with an existing user on your system if necessary.)
Open a Root Shell¶
Using sudo.
or
Exit:
Use these only when multiple administrative commands are required.
sudo vs su¶
| sudo | su |
|---|---|
| Executes a single command | Switches to another user |
| Uses your password | Usually requires the target user's password (often the root password) |
| Logs commands | Limited auditing |
| More secure | Higher risk if used carelessly |
| Preferred for administration | Used when a full user session is required |
View Your sudo Privileges¶
Example:
The sudoers File¶
Configuration file:
Never edit it directly with a normal editor.
Instead use:
visudo checks the syntax before saving to help prevent configuration errors.
Example sudoers Entry¶
Meaning:
- User:
basha - On all hosts
- May run commands as any user and group
- Can execute any command
Administrative Groups¶
Many Linux distributions grant sudo access through a group.
Examples:
Ubuntu/Debian:
RHEL/Rocky/AlmaLinux:
Check your membership.
sudo Authentication Timeout¶
By default, sudo caches authentication for a limited time.
Re-authenticate immediately.
Invalidate cached credentials completely.
Common sudo Commands¶
Run as root.
Run as another user.
Root shell.
List permissions.
Edit sudoers.
Real Production Examples¶
Restart NGINX.
Restart Docker.
View system logs.
Edit SSH configuration.
Create a deployment directory.
Production Perspective¶
sudo is used daily for:
- System updates
- User management
- Service administration
- Software installation
- Security hardening
- Kubernetes node administration
- Docker administration
- Cloud VM management
It is the standard method for performing privileged tasks while maintaining accountability.
Hands-on Lab¶
Task 1¶
Display your current user.
Task 2¶
Run the same command with sudo.
Task 3¶
View your sudo permissions.
Task 4¶
Display your groups.
Task 5¶
Open a root shell.
Verify:
Exit:
Task 6¶
Invalidate cached sudo credentials.
Run another sudo command to observe the password prompt again.
Task 7¶
View the sudoers file safely.
Exit without making changes.
Task 8¶
Run a command as another user (replace nobody with an available account if necessary).
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
sudo | Run a privileged command | Daily administration |
sudo -i | Root login shell | Maintenance |
sudo -s | Root shell | Troubleshooting |
sudo -u | Run as another user | Testing |
sudo -l | View sudo privileges | Auditing |
sudo -k | Forget cached credentials | Security |
visudo | Safely edit sudoers | Administration |
Production Troubleshooting Scenario¶
Scenario
A DevOps engineer cannot restart a service.
Error:
Investigation:
The user is not a member of the administrative group.
An administrator grants the appropriate sudo access using the organization's standard process.
After re-authenticating (or starting a new login session if group membership changed), the engineer can successfully manage the service.
Best Practices¶
- Use
sudoinstead of logging in asroot. - Grant only the minimum privileges required.
- Edit the sudoers file only with
visudo. - Use administrative groups to manage sudo access.
- Review sudo permissions regularly.
- Use
sudo -ionly when a full root shell is genuinely needed.
Common Mistakes¶
❌ Logging in directly as root for routine tasks.
✅ Use a regular user account with sudo.
❌ Editing /etc/sudoers with a normal text editor.
✅ Always use:
❌ Granting unrestricted sudo access to every user.
✅ Apply the Principle of Least Privilege.
Interview Questions¶
Beginner¶
- What does
sudostand for? - Why is
sudopreferred over logging in asroot? - Which password does
sudorequest? - How do you view your sudo privileges?
Intermediate¶
- Explain the difference between
sudoandsu. - What is the purpose of
visudo? - How do you run a command as another user?
- What does
sudo -kdo?
Architect Level¶
- How would you securely manage administrative access across hundreds of Linux servers?
- Why is command auditing important for privileged operations?
- How would you design role-based sudo access for DevOps, DBAs, and Security teams?
Summary¶
In this lesson, you learned:
- What
sudois - Why
sudois preferred over direct root logins - Running commands with elevated privileges
- Running commands as another user
- Viewing sudo permissions
- Understanding the
sudoersfile - Using
visudosafely - Security best practices for administrative access
The sudo command is one of the most important security features in Linux. It enables controlled administrative access while maintaining accountability through command logging and minimizing the risks associated with working directly as the root user.
Key Takeaways¶
sudostands for Superuser Do.- Use
sudoinstead of logging in as therootuser. sudoauthenticates using your own password.- Use
sudo -uto run commands as another user. - Use
sudo -lto view your sudo privileges. - Always edit the
sudoersfile usingvisudo. - Follow the Principle of Least Privilege when granting administrative access.
What's Next?¶
Linux Password Policies — Securing User Authentication
You'll explore:
- Password authentication
- Password aging
- Password expiration
- Password complexity requirements
- Account locking and unlocking
- Password security best practices
- Enterprise compliance and security standards
Understanding password policies is essential for securing Linux systems and enforcing strong authentication practices in enterprise environments.