Guides / Linux servers

sudo and user accounts on a Linux server: who can become root

beginnerUpdated 26 Nov 20261135 words

On a small server, root access is often shared in the simplest way: one admin account, or root itself, and a password everyone knows. Linux has everything needed to do better without extra software. Named accounts, narrow sudo rules and a few file checks keep root access limited to the people who should have it.

One account per person

Give every person their own account and their own SSH key. The logs then name who logged in and who ran which command with sudo, and removing one person does not mean changing a shared password.

sudo adduser alice                 # Debian, Ubuntu (asks for a password)
sudo useradd -m alice && sudo passwd alice   # RHEL, Rocky, Alma
adduser alice                      # Alpine, as root

Who can use sudo

The distributions give sudo rights to a group:

Distribution Group Line in /etc/sudoers
Debian, Ubuntu sudo %sudo ALL=(ALL:ALL) ALL
RHEL, Rocky, Alma wheel %wheel ALL=(ALL) ALL
Alpine wheel, after you add the rule none by default

Add a person to the group, and list who is in it:

sudo usermod -aG sudo alice        # Debian, Ubuntu
sudo usermod -aG wheel alice       # RHEL, Rocky, Alma
getent group sudo wheel

On Alpine, sudo is in the community repository; doas, a smaller alternative, is in main. With sudo, add the group rule as a file:

apk add sudo
echo '%wheel ALL=(ALL:ALL) ALL' > /etc/sudoers.d/wheel
chmod 440 /etc/sudoers.d/wheel
addgroup alice wheel

Group membership is not the only way to sudo rights. To see every rule that applies to one user:

sudo -l -U alice

Edit sudoers safely

A syntax error in a sudoers file can take sudo away from everyone. Always edit with visudo, which checks the file before it saves:

sudo visudo -f /etc/sudoers.d/deploy
sudo visudo -c

visudo -c checks all files and prints parsed OK for each, or the file, line and column of an error, such as /etc/sudoers.d/broken:2:6: syntax error.

Two rules for files in /etc/sudoers.d/:

  • No dot in the name. sudo skips files whose names contain a . or end in ~. A rule in /etc/sudoers.d/deploy.conf is silently ignored; /etc/sudoers.d/deploy works. We checked this on Debian 12, Debian 13 and Ubuntu 24.04: with the .conf name, sudo -l -U deploy reported no rights.
  • Mode 0440, owner root. sudo refuses files that others can write.

NOPASSWD

NOPASSWD lets a user run commands as root without typing their password. It is convenient, and it means that anyone who gets a shell as that user (a stolen SSH key, a compromised process running as that user) becomes root at once. With a password prompt, they also need the password.

  • For people, keep the password prompt. sudo remembers a successful prompt for 5 minutes by default (timestamp_timeout).
  • For automation, allow only the exact command, with its arguments:
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp.service

With arguments in the rule, sudo allows only that exact command line. Avoid rules for commands that can start a shell or write any file, such as editors, less, find, tar, rsync or a shell script that the user can edit; each of them leads to a root shell.

Most cloud images set up their default user (such as ubuntu, debian or rocky) with cloud-init, and the default cloud-init configuration gives that user ALL=(ALL) NOPASSWD:ALL in /etc/sudoers.d/90-cloud-init-users. Find such rules with:

sudo grep -rn NOPASSWD /etc/sudoers /etc/sudoers.d/

No second root

Any account with UID 0 is root, whatever its name. Only root should have it:

awk -F: '$3 == 0 { print $1 }' /etc/passwd

A second name with UID 0 (such as toor) is a known way to hide a backdoor. If you find one you did not create, treat the server as compromised. If it is an old admin alias, remove it. userdel refuses at first, because processes of UID 0 are running; -f removes the entry anyway. Never add -r, which would delete the home directory, often /root:

sudo userdel -f toor

No empty passwords

An empty password field in /etc/shadow means "no password needed". SSH refuses such logins by default, but local logins and su go through PAM, and the default PAM setup of Debian, Ubuntu and RHEL allows empty passwords (nullok). List them:

sudo awk -F: '$2 == "" { print $1 }' /etc/shadow

Set a password (sudo passwd <name>) or lock the account (sudo passwd -l <name>). passwd -S <name> shows the state: P for a usable password, L for locked, NP for no password (RHEL prints LK and Password locked. for a locked account). A * or ! in the shadow field, as for system accounts such as www-data, is fine: it means no password login.

When someone leaves

Locking the password is not enough: passwd -l stops password logins, but SSH keys keep working. Lock and expire the account, which stops key logins too:

sudo usermod -L -e 1 alice
sudo pkill -KILL -u alice          # end running sessions

-e 1 sets the expiry date to 2 January 1970. Then remove their keys from every authorized_keys they could use, rotate secrets they knew, and delete the account later with sudo userdel -r alice when you no longer need the files.

Permissions of the account files

The account files need their default modes. These are the values on fresh installations:

File Debian, Ubuntu RHEL, Rocky, Alma Alpine
/etc/passwd, /etc/group 644 root:root 644 root:root 644 root:root
/etc/shadow 640 root:shadow 000 root:root 640 root:shadow
/etc/gshadow 640 root:shadow 000 root:root (does not exist)
/etc/sudoers 440 root:root 440 root:root 440 root:root

The explainer for the permissions check has the commands to restore them.

Who used sudo

sudo logs every command with the user and the working directory:

sudo journalctl _COMM=sudo --since today      # systemd distributions
sudo grep sudo /var/log/auth.log              # Debian, Ubuntu with rsyslog
sudo grep sudo /var/log/secure                # RHEL, Rocky, Alma with rsyslog

Patchbrief checks the account files and the sudo rules on each server every day and reports a second UID 0, empty passwords and NOPASSWD rules.

Sources

Check your servers every day

The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.

Start free