Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

sudo Command Guide for Running a Single Command as Another User

sudo runs one command with elevated privileges — as root, or as another user — after checking that you’re allowed to.


What is sudo, and Why Use It?

sudo (“superuser do”) is the everyday way to do administration on Linux. Instead of logging in as root, you run a single command prefixed with sudo, the system checks the /etc/sudoers policy, and — if you’re allowed — runs that one command as root.

It’s the difference between “I’m root now and everything I type is dangerous” and “I’m me, but this one command got root for a moment.”

Why Learn sudo?

  • Almost every admin task needs it — installing software, editing system files, managing services.
  • It’s safer than logging in as root: every use is logged, and you can’t accidentally break the system with a stray command.
  • It’s scoped — you grant specific commands to specific users, not full root.
  • It’s universal — the same tool on Linux, macOS, BSD, and most Unix-like systems.

sudo is a privilege escalator, not a login. Use it for the one command that needs it, not as a way of life. If everything needs sudo, the design (or your user setup) is wrong.


1. Run a Single Command

sudo command

Example:

# Update the package list (Debian/Ubuntu)
sudo apt update

# Edit a system file
sudo nano /etc/hosts
  • The first time you use sudo in a session, it asks for your own password — not root’s.
  • After that, it remembers you for a few minutes (default 5–15), so you’re not re-authenticating every command.

2. Become a Root Shell (Temporarily)

sudo -i or sudo su - gives you an interactive root shell:

sudo -i

Example:

$ sudo -i
root@server:~# whoami
root
root@server:~# exit
$ whoami
alice
  • sudo -i simulates a full login as root: root’s environment, root’s home directory.
  • exit (or Ctrl+D) drops you back to your normal user.
  • Use this only for extended admin sessions — and leave when done.

3. Run as Another User

sudo -u targets a specific user:

sudo -u postgres psql

Example:

# Run a command as the web server account
sudo -u www-data ls /var/www/

# Run a whole shell as another user
sudo -u deploy -i
  • Common on servers: databases, web servers, and services each run as their own account.
  • You still authenticate as yourself; only the command changes identity.

4. Check What You’re Allowed to Do

sudo -l lists your privileges:

sudo -l

Example:

$ sudo -l
Matching Defaults entries for alice on this host:
    env_reset, mail_badpass

User alice may run the following commands on this host:
    (root) NOPASSWD: /usr/bin/systemctl restart nginx
    (ALL, ALL)
  • The output tells you exactly which commands you can run and as whom.
  • NOPASSWD means that command runs without a password prompt.

5. Edit the sudoers Policy Safely

The rules live in /etc/sudoers, edited with visudo — never with a plain text editor:

sudo visudo

Example:

# Give the devs group full sudo, no password
%sudo   ALL=(ALL:ALL) ALL

# Let alice restart nginx without a password
alice ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginx
  • visudo checks syntax before saving — a broken sudoers file can lock everyone out of root.
  • Prefer drop-in files in /etc/sudoers.d/ over editing the main file.
  • Grant the narrowest command possible, not blanket ALL.

6. sudo vs su vs root Login

ApproachWhat it doesWhen to use
sudo cmdRun one command as rootEveryday admin tasks
sudo -iOpen a root shellExtended admin session
su -Switch to root with root’s passwordLegacy/last resort
root loginLog in directly as rootRarely; recovery only

Example:

sudo apt update     # one command, your password
sudo -i             # root shell, your password
su -                # root shell, ROOT's password (usually disabled)
  • On most modern systems the root account has no password at all — sudo is the only way in.
  • su is covered in detail on its own page.

Common Pitfalls

  • Running everything with sudo “just in case”. Unnecessary elevation is how accidents happen — sudo rm -rf / is the canonical disaster. Type the command without sudo first; add it only when the error says you need it.
  • Typo-ing the path after sudo. sudo runs whatever follows; a stray space or wrong filename still executes as root.
  • Confusing it with su. sudo asks for your password; su asks for the target’s password.
  • Forgetting sudo in a pipeline. In cat file | grep x, only cat runs as root if you write sudo cat file | grep x — the rest runs as you. Use sudo sh -c '...' if the whole pipeline needs root.
  • Leaving a root shell open. sudo -i shells are dangerous if left running; exit when done.
  • Editing /etc/sudoers with a normal editor. Always visudo — a syntax error can break every sudo session on the system.

PageWhy it’s related
suThe older way to switch users
idCheck your user and group identities
whoamiConfirm who you are before and after elevation
passwdpasswd with sudo changes another user’s password
/etc/passwdThe account database sudo’s policy references
chmodPermission errors are what sudo usually fixes

sudo Cheatsheet

CommandMeaning
sudo cmdRun one command as root
sudo -iOpen a root login shell
sudo -u user cmdRun a command as a specific user
sudo -u user -iOpen a shell as a specific user
sudo -lList what you’re allowed to run
sudo visudoSafely edit the sudoers policy
sudo sh -c 'cmd1; cmd2'Run a whole pipeline/command string as root
exitLeave a sudo -i root shell