TExcelTAD
Home › Linux Server Administration › sudo & sudoers

sudo & sudoers

sudo lets a user run a command as another user (usually root), according to rules in /etc/sudoers and the files in /etc/sudoers.d/. Users & Groups showed how to use sudo; on a server you write the rules, and the goal is the opposite of convenience: every account gets exactly the root powers it needs, and every use is logged.

Reading a rule

text
deploy  ALL = (root)  NOPASSWD: /usr/bin/systemctl restart inventory.service
└─who   └─on which host  └─as whom   └─options  └─which command(s)
Part Examples
who deploy (a user), %ops (a group), ADMINS (a User_Alias)
host almost always ALL; meaningful only when one sudoers file is shared by many hosts
as whom (root), (ALL), (ALL:ALL) = any user and any group, (postgres)
tags NOPASSWD:, NOEXEC:, SETENV:; without NOPASSWD the user types their own password
command a full path, optionally with exact arguments; ALL means anything

The distribution default gives the admin group everything: %sudo ALL=(ALL:ALL) ALL on Debian/Ubuntu, %wheel ALL=(ALL) ALL on RHEL. Fine for the humans who administer the box, never for automation.

Always through visudo

A syntax error in sudoers means nobody can use sudo anymore, including you, the one who has to fix it. So never edit these files with a plain editor:

  • sudo visudo edits /etc/sudoers, sudo visudo -f /etc/sudoers.d/60-deploy edits a drop-in; both check the syntax before saving.
  • Files written by automation are checked with visudo -cf <file> before they are moved into place (Ansible's template module has validate: visudo -cf %s for exactly this).
  • Drop-in names must not contain a . or end in ~ (those files are silently skipped), and the files should be mode 0440, owned by root.

A rule set for a deploy user, checked like a CI job would:

bash · try it yourself
cat > 60-deploy <<'EOF'
# Deploy automation: restart the app, reload the proxy, nothing else.
Cmnd_Alias APP_CTL = /usr/bin/systemctl restart inventory.service, \
                     /usr/bin/systemctl reload nginx.service
Defaults:deploy !requiretty, log_output
deploy ALL=(root) NOPASSWD: APP_CTL

# Humans on call: everything, with their password.
%oncall ALL=(ALL:ALL) ALL
EOF
visudo -cf 60-deploy
echo "exit code: $?"
60-deploy: parsed OK
exit code: 0

Now the mistakes visudo is there to catch:

bash · try it yourself
printf 'deploy ALL=(root) NOPASSWD: systemctl restart app\n' > bad-path
printf 'Defaults timestamp_timout=5\n' > bad-default
printf 'deploy ALL=(root) NOPASSWD: APP_CTL\n' > bad-alias
for f in bad-path bad-default bad-alias; do
  echo "== $f"
  visudo -cf "$f"; echo "exit code: $?"
done
echo "== bad-alias, strict"
visudo -c -s -f bad-alias; echo "exit code: $?"
== bad-path
bad-path:1:29: expected a fully-qualified path name
deploy ALL=(root) NOPASSWD: systemctl restart app
                            ^~~~~~~~~
exit code: 1
== bad-default
bad-default:1:27: unknown defaults entry "timestamp_timout"
exit code: 1
== bad-alias
bad-alias:1:36: Cmnd_Alias "APP_CTL" referenced but not defined
bad-alias: parsed OK
exit code: 0
== bad-alias, strict
bad-alias:1:36: Cmnd_Alias "APP_CTL" referenced but not defined
exit code: 1

Commands must be full paths (otherwise a user could put their own systemctl first in PATH), unknown Defaults are errors, and an alias that is never defined is only a warning unless you check with -s (strict). Use -s in CI.

Least privilege that actually holds

A rule is only as narrow as the command it allows. Some commands are a root shell in disguise:

Allowed command Why it is equivalent to ALL
/usr/bin/vim, less, more, man :!sh or !sh starts a shell as root
/usr/bin/systemctl (any arguments) systemctl edit opens an editor; a unit with ExecStart=/bin/sh runs anything
/usr/bin/tar, find, rsync, awk, python3, bash script.sh options like find -exec, tar --checkpoint-action, or simply running code
/usr/bin/chown, chmod, cp, tee on arbitrary paths overwrite /etc/sudoers or /etc/shadow
a script the user can write to they edit the script, sudo runs it

So: list exact commands with their arguments, use sudoedit (it edits a copy as the user and writes it back as root) instead of allowing an editor, keep allowed scripts root-owned and not writable by anyone else, and avoid wildcards in arguments: in sudoers * also matches spaces, so /usr/bin/systemctl restart app* allows systemctl restart app ssh.service, and any extra options the command understands. GTFOBins (gtfobins.github.io) lists how hundreds of common binaries escape to a shell.

Seeing and logging what sudo allows

On a live server (sample session):

bash
$ sudo -l -U deploy
User deploy may run the following commands on web1:
    (root) NOPASSWD: /usr/bin/systemctl restart inventory.service, /usr/bin/systemctl reload nginx.service
$ sudo -u deploy sudo systemctl restart inventory.service    # allowed, no password
$ sudo -u deploy sudo systemctl stop nginx
Sorry, user deploy is not allowed to execute '/usr/bin/systemctl stop nginx' as root on web1.
$ journalctl _COMM=sudo -n 2 --no-pager -o cat
  deploy : TTY=pts/1 ; PWD=/home/deploy ; USER=root ; COMMAND=/usr/bin/systemctl restart inventory.service
  deploy : command not allowed ; TTY=pts/1 ; PWD=/home/deploy ; USER=root ; COMMAND=/usr/bin/systemctl stop nginx

Every sudo call is logged with the user, the directory and the full command. Defaults log_output additionally records the session's output (replay it with sudoreplay). Ship these logs off the server: an attacker who becomes root can edit local ones.

Go further: Linux Privilege Escalation: A Read-Only Audit shows which sudo rules and SUID files let a user become root.

Test yourself

Why must commands in sudoers be written as full paths?

  1. sudo can't search PATH
  2. Otherwise the user could run a different program with the same name from a directory earlier in their PATH
  3. To make the file shorter
  4. visudo only accepts absolute paths for readability

Which rule effectively gives the deploy user a root shell?

  1. deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart inventory.service
  2. deploy ALL=(root) NOPASSWD: /usr/bin/less /var/log/syslog
  3. deploy ALL=(postgres) /usr/bin/psql
  4. None of these

A file /etc/sudoers.d/deploy.conf has no effect. What is the likely reason?

  1. It needs NOPASSWD
  2. Files whose names contain a dot are skipped by the include directive
  3. sudoers.d is not read on Ubuntu
  4. It must be called 99-deploy