← Full reference

sudo

Running commands as root or another user; rules in /etc/sudoers and visudo

sudo runs a single command as another user — root by default. Who may run what is described in /etc/sudoers and the files in /etc/sudoers.d/. The password asked for is that of whoever calls sudo, not root's. Once entered, it is remembered for a few minutes (15 on Ubuntu).

Syntax

sudo [options] command [arguments…]
sudo -i | -s                  # a shell as root
sudo -l                       # what am I allowed to do
sudoedit file                 # safely edit a file as root
visudo [-c] [-f file]         # edit or check sudoers

As whom and in which environment

OptionWhat it does
-u alicerun as alice instead of root (-u '#1001' by UID also works)
-g groupwith the given primary group
-ithe target user's login shell: their environment, their home directory, their profile; sudo -i command — run through that shell
-sa shell as root, but in the current directory and without a login profile
-Ekeep all environment variables (if the policy allows it)
--preserve-env=VAR1,VAR2keep only the listed variables
-D directorychange to the directory before running (needs the runcwd permission in sudoers)
-brun the command in the background

Password and session

OptionWhat it does
-ndon't ask for a password: if one is needed — fail immediately (for scripts)
-Sread the password from stdin rather than the terminal
-vextend the remembered password without running anything
-kforget the password: the next sudo will ask again
-Kremove the remembered password entirely and exit

Viewing permissions

CommandWhat it shows
sudo -lyour rules: lines (as_whom) [NOPASSWD:] commands and the active Defaults
sudo -l -U aliceanother user's rules (needs root)
sudo -l commandwhether a specific command is allowed: prints its full path or nothing (code 1)

Rules in sudoers

A rule line: who host=(as_user:group) [NOPASSWD:] commands. The host is almost always ALL.

RuleMeaning
alice ALL=(ALL:ALL) ALLalice can do anything, with a password
%devs ALL=(ALL) ALLthe same for all members of the devs group (% — a group)
alice ALL=(ALL) NOPASSWD: ALLanything, and without a password
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginxone command with exactly these arguments
alice ALL=(root) /usr/bin/id """" instead of arguments — the command only without arguments
alice ALL=(root) /usr/bin/tail -n * /var/log/** in arguments — a wildcard
alice ALL=(root) /usr/bin/ls, !/usr/bin/ls /root! — an exception from what is allowed
alice ALL=(www-data) /usr/bin/phponly as www-data
alice ALL=(root) sudoedit /etc/hostsedit a file via sudoedit

Commands are written with full paths: visudo won't accept ls without a path. To find the path: command -v systemctl. Ubuntu already has the rule %sudo ALL=(ALL:ALL) ALL, so the easiest way to grant full rights is via the sudo group.

Aliases and Defaults

EntryWhat it does
Cmnd_Alias LOGS = /usr/bin/journalctl, /usr/bin/tail -n * /var/log/*a named list of commands — then alice ALL=(root) LOGS
User_Alias OPS = alice, boba named list of users
Runas_Alias APPS = www-data, postgresas whom: alice ALL=(APPS) ALL
Defaults secure_path="…"PATH for all commands under sudo
Defaults:alice timestamp_timeout=5how many minutes to remember the password (for alice); 0 — always ask
Defaults env_keep += "HTTP_PROXY"which variables not to reset
@includedir /etc/sudoers.dread files from the directory (already present on Ubuntu)

visudo

CommandWhat it does
visudoopen /etc/sudoers in an editor, check the syntax before saving
visudo -f /etc/sudoers.d/namethe same for a separate file
visudo -ccheck all sudoers files, including their permissions
visudo -cf filecheck a single file without installing it

The editor comes from SUDO_EDITOR, VISUAL or EDITOR; for example, EDITOR=vim visudo.

Examples

# run a command as root and as another user
sudo apt-get update
sudo -u www-data ls -l /srv/app

# what the current user is allowed to do
sudo -l

# is a specific command allowed
sudo -l /usr/bin/systemctl

# a root shell with root's environment
sudo -i

# give a user full rights via the sudo group (requires logging in again)
usermod -aG sudo alice

# your own rule — as a separate file via visudo
visudo -f /etc/sudoers.d/deploy

# or create the file with a script and always check the syntax
echo 'deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart nginx' > /etc/sudoers.d/deploy
chmod 440 /etc/sudoers.d/deploy
visudo -c

# check a draft without installing it
visudo -cf ./sudoers-draft

# write to a protected file: redirection is done by the shell, not by sudo
echo '127.0.0.1 example.local' | sudo tee -a /etc/hosts

# edit a system file with your own editor without running the editor as root
sudoedit /etc/hosts

# pass one environment variable
sudo --preserve-env=HTTP_PROXY apt-get update

Pitfalls

  • sudo echo text > /etc/file doesn't work. The > redirection is done by your shell — still without root rights, hence "Permission denied". Use | sudo tee file (tee -a — append) or sudo sh -c 'echo text > file'.
  • Edit only via visudo — it checks the syntax before saving. A line with an error in sudoers is skipped: the rule silently won't work, and sudo will print "syntax error" on every run. After editing with a script, always run visudo -c.
  • Names in /etc/sudoers.d/. Files whose name contains a dot (deploy.conf) or ends in ~ are ignored. Permissions must be 0440, otherwise visudo -c complains, and sudo skips a world-writable file.
  • Rule order matters: when several lines match, the last one wins. A rule with a password below a NOPASSWD rule for the same command will ask for the password again.
  • Arguments in a rule are an exact match. The rule /usr/bin/systemctl restart nginx won't allow systemctl restart nginx.service. And a command without arguments in a rule allows any arguments.
  • Shell builtins (cd, export, source) can't be run via sudo — they're not programs. For them — sudo -i or sudo sh -c '…'.
  • The environment is reset. PATH is replaced with secure_path from sudoers, HOME becomes the target user's home directory, most variables are lost. A script from ~/bin may "not be found" under sudo.
  • Dangerous permissions. A rule for /usr/bin/tail lets someone read /etc/shadow; for vim, less, find, tar — start a root shell from them. For editing files grant sudoedit, not an editor.
  • A new sudo group membership takes effect only in the user's new session.

See also

useradd · su · id · chmod · redirection · tee · env