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.
sudois a privilege escalator, not a login. Use it for the one command that needs it, not as a way of life. If everything needssudo, 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
sudoin 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 -isimulates a full login as root: root’s environment, root’s home directory.exit(orCtrl+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.
NOPASSWDmeans 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
visudochecks 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
| Approach | What it does | When to use |
|---|---|---|
sudo cmd | Run one command as root | Everyday admin tasks |
sudo -i | Open a root shell | Extended admin session |
su - | Switch to root with root’s password | Legacy/last resort |
| root login | Log in directly as root | Rarely; 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 —
sudois the only way in. suis 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.sudoruns whatever follows; a stray space or wrong filename still executes as root. - Confusing it with
su.sudoasks for your password;suasks for the target’s password. - Forgetting
sudoin a pipeline. Incat file | grep x, onlycatruns as root if you writesudo cat file | grep x— the rest runs as you. Usesudo sh -c '...'if the whole pipeline needs root. - Leaving a root shell open.
sudo -ishells are dangerous if left running; exit when done. - Editing
/etc/sudoerswith a normal editor. Alwaysvisudo— a syntax error can break every sudo session on the system.
Related Pages
| Page | Why it’s related |
|---|---|
| su | The older way to switch users |
| id | Check your user and group identities |
| whoami | Confirm who you are before and after elevation |
| passwd | passwd with sudo changes another user’s password |
| /etc/passwd | The account database sudo’s policy references |
| chmod | Permission errors are what sudo usually fixes |
sudo Cheatsheet
| Command | Meaning |
|---|---|
sudo cmd | Run one command as root |
sudo -i | Open a root login shell |
sudo -u user cmd | Run a command as a specific user |
sudo -u user -i | Open a shell as a specific user |
sudo -l | List what you’re allowed to run |
sudo visudo | Safely edit the sudoers policy |
sudo sh -c 'cmd1; cmd2' | Run a whole pipeline/command string as root |
exit | Leave a sudo -i root shell |