sudo — running commands as another user
Grants temporary root rights via sudoers; edit the file only through visudo, or a mistake locks everyone out of sudo.
Cheat sheet
6 commands Printable version-
sudo apt updaterun a command as root -
sudo -u postgres psqlrun as another user -
sudo -iinteractive root shell -
sudo -lwhat I'm allowed to run -
sudoedit /etc/nginx/nginx.confsafely edit a root-owned file -
sudo visudo -f /etc/sudoers.d/10-xedit sudoers rules
Overview
sudo runs a command as another user (root by default), checking rights
against the sudoers rules. Better than sitting under root all the time:
targeted, logged, using your own user's password.
Options
7sudoers
Rights come from membership in the sudo group (Ubuntu) / wheel
(RHEL), or an explicit rule. Never edit sudoers directly — only
through visudo (it checks syntax, otherwise you can lock yourself out
of sudo):
Rule format and aliases:
NOPASSWD: — no password prompt (for automation; keep the scope narrow!).
Options and sudoedit
Useful Defaults (in /etc/sudoers.d/):
sudoedit edits a copy under your own name and atomically puts it back —
safer than sudo vim (which never runs the editor as root).
| Option | What it does | Example |
|---|---|---|
-u user |
Run the command as the given user instead of root. | sudo -u postgres psql |
-i |
Interactive root shell with root's own environment (login shell). | sudo -i |
-l |
List the commands the current user is allowed to run. | sudo -l |
-s |
A root shell without switching to the target user's environment. | sudo -s |
-k |
Reset the password cache — the next sudo call asks again. | sudo -k |
-E |
Preserve the calling user's environment variables, if the rule allows it. | sudo -E make install |
-v |
Refresh the password cache without running a command — useful before a long script. | sudo -v |
Nothing found. Try a shorter query.
Troubleshooting
5user is not in the sudoers file
The user isn't in the sudo group. As root: usermod -aG sudo alice
(needs a re-login, see users-groups).
sudo: a terminal is required to read the password
No TTY (over SSH without -t, or from a script). Options: NOPASSWD for
the needed command, ssh -t, or sudo -S (password from stdin).
NOPASSWD doesn't work for switching users
(root) NOPASSWD: ALL allows commands as root, but not sudo -u
other. For a passwordless switch, use an (other) rule or sudo runuser
-u other -- cmd.
The command works by hand but not through sudo (PATH/env)
env_reset/secure_path clean the environment. Call it by its full
path or configure secure_path; for variables, use sudo -E (if
allowed).
Broke sudoers, lost access
visudo won't let you save a file with an error. If you still lost
access, get in through a root console (recovery/hypervisor) and fix it.
Danger zone
NOPASSWD without a narrow command in the rule (NOPASSWD: ALL instead
of a specific path) grants full root access with no check at all — keep
the rule's scope as narrow as possible, especially for service accounts.
Edit sudoers only through visudo: it checks syntax before saving,
while a direct edit with a text editor can leave the system with no
working way to get root at all.
Terms
2- authorization
- Deciding which actions an established identity and its privileges permit. Also appears in: curl, HTTP
- root (superuser)
- The account with UID 0: the kernel skips most permission checks for it. Commands are usually run as root through sudo. Also appears in: Capabilities: root split into parts, PAM: login checks, password policy and lockout, Service hardening: User=, ProtectSystem, NoNewPrivileges, Docker and 71 more
See also
- visudo — edit sudoers and
/etc/sudoers.dfiles with checking. - permissions — The Unix permission model — read/write/execute, owner/group/others, special bits.
- users-groups — Accounts, groups, membership; useradd/usermod, /etc/passwd and /etc/group.
Found an inaccuracy? Report an error in the article