sudo and user accounts on a Linux server: who can become root
On many small servers, several people share one admin account, or root itself, with a password everyone knows. Named accounts, sudo rules and a few checks of the account files limit root access to the people who should have it, with tools that every distribution ships.
One account per person
Give every person their own account and SSH key. The logs then name who logged in and who ran which command with sudo, and when one person leaves, you lock one account instead of 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
Rules in sudoers can also name a user directly. 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. 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.
Files in /etc/sudoers.d/:
- sudo skips files whose names contain a
.or end in~. It ignores a rule in/etc/sudoers.d/deploy.confwithout a warning;/etc/sudoers.d/deployworks. In a test on Debian 12, Debian 13 and Ubuntu 24.04,sudo -l -U deploylisted none of the rules of the.conffile. - The mode must be 0440 and the owner root. sudo refuses files that others can write.
NOPASSWD
NOPASSWD lets a user run commands as root without typing their password. Anyone who gets a shell as that
user (through a stolen SSH key, or a compromised process running as that user) then becomes root at once.
With a password prompt, they also need the password.
- For people, keep the password prompt. By default sudo remembers a successful prompt for 5 minutes
(
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 lets the account log in without a password. 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: the account cannot log in with a password.
When someone leaves
passwd -l stops password logins, but SSH keys keep their access. 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 with sudo userdel -r alice once you no longer need the
files.
Permissions of the account files
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
Vulnpocalypse 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
- sudoers(5):
@includedirand skipped file names,NOPASSWD,timestamp_timeoutdefault of 5 minutes,use_pty - usermod(8):
-Land-e; locking the password does not lock the account - userdel(8):
-fand-r - GTFOBins: programs that lead to a shell when allowed through sudo
- cloud-init: default configuration template:
sudo: ["ALL=(ALL) NOPASSWD:ALL"]for the default user; distros/__init__.py:/etc/sudoers.d/90-cloud-init-users
Check your servers every day
The Vulnpocalypse collector reports the packages and settings of a server once a day, and the digest lists what to fix, most urgent first.
Vulnpocalypse