Skip to content

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

Linux Mastery → Module 5: Users and Groups → Lesson 3

Difficulty: Beginner → Intermediate

Reading Time: 50 Minutes

Course Progress

Course: Linux Mastery

Module: Users and Groups

Lesson: 3 of 10


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand the purpose of sudo
  • Differentiate sudo and su
  • 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:

root

No.

Instead:

sudo command

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:

Login as root


Perform task


Logout

With sudo:

Login as normal user


Run one privileged command


Continue working normally

This minimizes the time spent with elevated privileges.


Basic Syntax

sudo command

Example:

sudo apt update

or

sudo dnf update

First-Time Authentication

The first sudo command prompts for your own password, not the root password.

Example:

[sudo] password for basha:

After successful authentication, sudo remembers your credentials for a short period (the timeout is configurable).


Common sudo Examples

Update packages.

sudo apt update

Install software.

sudo apt install nginx

Restart a service.

sudo systemctl restart nginx

Edit a system file.

sudo nano /etc/hosts

Create a user.

sudo useradd developer

Check Current User

Without sudo.

whoami

Output:

basha

Run as root.

sudo whoami

Output:

root

Run a Command as Another User

Syntax:

sudo -u username command

Example:

sudo -u nginx whoami

Output:

nginx

(Replace nginx with an existing user on your system if necessary.)


Open a Root Shell

Using sudo.

sudo -i

or

sudo -s

Exit:

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

sudo -l

Example:

User basha may run the following commands...

The sudoers File

Configuration file:

/etc/sudoers

Never edit it directly with a normal editor.

Instead use:

sudo visudo

visudo checks the syntax before saving to help prevent configuration errors.


Example sudoers Entry

basha ALL=(ALL:ALL) ALL

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:

sudo

RHEL/Rocky/AlmaLinux:

wheel

Check your membership.

groups

sudo Authentication Timeout

By default, sudo caches authentication for a limited time.

Re-authenticate immediately.

sudo -k

Invalidate cached credentials completely.

sudo -K

Common sudo Commands

Run as root.

sudo command

Run as another user.

sudo -u user command

Root shell.

sudo -i

List permissions.

sudo -l

Edit sudoers.

sudo visudo

Real Production Examples

Restart NGINX.

sudo systemctl restart nginx

Restart Docker.

sudo systemctl restart docker

View system logs.

sudo journalctl -xe

Edit SSH configuration.

sudo nano /etc/ssh/sshd_config

Create a deployment directory.

sudo mkdir /opt/app

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.

whoami

Task 2

Run the same command with sudo.

sudo whoami

Task 3

View your sudo permissions.

sudo -l

Task 4

Display your groups.

groups

Task 5

Open a root shell.

sudo -i

Verify:

whoami

Exit:

exit

Task 6

Invalidate cached sudo credentials.

sudo -k

Run another sudo command to observe the password prompt again.


Task 7

View the sudoers file safely.

sudo visudo

Exit without making changes.


Task 8

Run a command as another user (replace nobody with an available account if necessary).

sudo -u nobody whoami

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:

user is not in the sudoers file

Investigation:

groups

sudo -l

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 sudo instead of logging in as root.
  • 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 -i only 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:

sudo visudo

❌ Granting unrestricted sudo access to every user.

✅ Apply the Principle of Least Privilege.


Interview Questions

Beginner

  1. What does sudo stand for?
  2. Why is sudo preferred over logging in as root?
  3. Which password does sudo request?
  4. How do you view your sudo privileges?

Intermediate

  1. Explain the difference between sudo and su.
  2. What is the purpose of visudo?
  3. How do you run a command as another user?
  4. What does sudo -k do?

Architect Level

  1. How would you securely manage administrative access across hundreds of Linux servers?
  2. Why is command auditing important for privileged operations?
  3. How would you design role-based sudo access for DevOps, DBAs, and Security teams?

Summary

In this lesson, you learned:

  • What sudo is
  • Why sudo is preferred over direct root logins
  • Running commands with elevated privileges
  • Running commands as another user
  • Viewing sudo permissions
  • Understanding the sudoers file
  • Using visudo safely
  • 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

  • sudo stands for Superuser Do.
  • Use sudo instead of logging in as the root user.
  • sudo authenticates using your own password.
  • Use sudo -u to run commands as another user.
  • Use sudo -l to view your sudo privileges.
  • Always edit the sudoers file using visudo.
  • 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.