sudo Command in Linux: Cheat Sheet, Syntax & 15+ Practical Examples

sudo Command in Linux: Cheat Sheet, Syntax & 15+ Practical Examples

The sudo command in Linux allows users to execute administrative tasks securely without logging in as the root user. It provides controlled privilege escalation using the /etc/sudoers configuration. In this guide, you’ll learn sudo syntax, common use cases, and a quick cheat sheet for daily administration.


sudo Command - Quick Cheat Sheet

TaskCommand
Run command as rootsudo command
Run as another usersudo -u user command
Open root login shellsudo -i
Open temporary root shellsudo -s
List sudo privilegessudo -l
List privileges for another usersudo -l -U user
Edit sudoers file safelysudo visudo
Edit files with sudosudo -e file
Refresh cached credentialssudo -v
Reset cached passwordsudo -k
Remove cached credentials completelysudo -K
Run command in backgroundsudo -b command

Execute commands with elevated privileges (as root user)

By default, sudo executes commands as the root user when no target user is specified. This allows normal users to perform administrative tasks without logging in as root.

bash
sudo whoami

Output:

text
root

This behavior is commonly used to perform tasks that require elevated permissions such as accessing restricted files or modifying system configurations.


Execute commands as another user

Use sudo -u to switch user context

The -u option allows execution of commands as a different user instead of root.

bash
sudo -u username whoami

Example:

bash
sudo -u nobody whoami

Run commands with specific user ID or username

You can specify either a username or UID to run commands under a different identity.

bash
sudo -u 1001 id

This is useful for testing permissions or executing commands in another user's environment.


Control sudo session and authentication

Cache credentials using sudo -v

The -v option updates the user's cached credentials without running a command. This avoids repeated password prompts within the timeout period.

bash
sudo -v

This is commonly used before running multiple sudo commands.

Reset authentication using sudo -k and sudo -K

These options control sudo session timeout and credential caching.

bash
sudo -k
  • Invalidates cached credentials (next sudo requires password)
bash
sudo -K
  • Removes cached credentials completely

These options are useful for improving security, especially on shared systems.


Inspect and validate sudo permissions

List allowed commands using sudo -l

The -l option lists all commands a user is allowed to run with sudo.

bash
sudo -l

This helps verify access before executing privileged commands.

Check privileges for another user using sudo -U

You can check sudo privileges of another user (requires appropriate permissions).

bash
sudo -l -U username

This is useful for auditing user access.


Start privileged shell sessions

Use sudo -i for login shell

Starts a full login shell as root with root environment.

bash
sudo -i

Use sudo -s for non-login shell

Starts a shell with elevated privileges but retains current user environment.

bash
sudo -s

Compare sudo -i vs sudo -s behavior

Featuresudo -isudo -s
Shell typeLogin shellNon-login shell
EnvironmentRoot environmentCurrent user environment
HOME/rootUser home
Use caseFull admin sessionQuick tasks

Modify and manage sudo configuration

Edit sudoers safely using visudo

Always use visudo to edit sudo configuration safely.

bash
sudo visudo

Use /etc/sudoers.d for modular configuration

Instead of editing the main file, create separate config files.

bash
sudo visudo -f /etc/sudoers.d/custom

Define custom rules and command restrictions

Example rule:

bash
username ALL=(ALL) /usr/bin/systemctl

This allows limited command execution instead of full access.


Control command execution behavior

Run commands in background using sudo -b

Execute commands in background.

bash
sudo -b command

Use non-interactive mode with sudo -n

Prevents sudo from prompting for password.

bash
sudo -n command

Handle command parsing using sudo --

Stops sudo from interpreting command options.

bash
sudo -- command

Useful when command contains special flags.


Handle environment variables with sudo

Preserve environment variables using sudo -E

The sudo -E option allows you to preserve environment variables when running commands with sudo. By default, sudo clears most environment variables for security, so using -E is useful when scripts or applications depend on custom variables.

bash
sudo -E command

Example:

bash
export MY_VAR=test
sudo -E bash -c 'echo $MY_VAR'

Set correct home directory using sudo -H

The sudo -H option sets the HOME environment variable to the target user's home directory (usually /root). This prevents permission issues when running applications that rely on correct home paths.

bash
sudo -H command

Example:

bash
sudo -H bash -c 'echo $HOME'

Pass custom environment variables to commands

You can pass environment variables inline while executing a command with sudo. This is useful for temporarily modifying PATH or other runtime configurations.

bash
sudo VAR=value command

Example:

bash
sudo PATH=$PATH:/custom/bin command

Compare environment behavior with and without sudo

You can compare how environment variables change when using sudo. This helps in debugging issues related to missing variables.

bash
env
sudo env

Use sudo in automation and scripts

Configure passwordless execution (NOPASSWD)

Add rule in sudoers:

bash
username ALL=(ALL) NOPASSWD: ALL

Avoid interactive prompts in scripts

Use non-interactive mode:

bash
sudo -n command

Troubleshoot sudo issues and errors

Debug permission denied errors

Ensure user is part of sudo group:

bash
groups username

Fix sudoers syntax issues

Always use:

bash
visudo

Incorrect syntax can break sudo access.

Handle missing sudo or misconfiguration

Install sudo if missing:

bash
apt install sudo

or

bash
yum install sudo

Frequently Asked Questions

1. What is sudo command in Linux?

sudo allows a permitted user to execute commands as root or another user without logging in as that user.

2. What is the difference between sudo -i and sudo -s?

sudo -i starts a login shell as root, while sudo -s starts a shell with root privileges but keeps the current environment.

3. How to list sudo privileges for a user?

Use sudo -l to list all commands a user is allowed to run with sudo.

4. How to add user to sudoers file?

Use visudo or add the user to sudo or wheel group depending on your Linux distribution.

5. Why sudo command is not working?

It may fail due to missing permissions, incorrect sudoers configuration, or user not being part of sudo group.

Summary

The sudo command is an essential tool in Linux for securely executing administrative tasks without logging in as the root user. It provides controlled privilege escalation using the /etc/sudoers configuration, ensuring better security and accountability.

In this guide, you learned how to use sudo with practical examples, manage user privileges, handle permissions, work with environment variables, and use sudo in scripts and automation. Understanding these concepts will help you perform daily system administration tasks more efficiently and securely.


Official Documentation

For more details, refer to the official sudo documentation:

sudo command man page

Rohan Timalsina

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.