Every account with sudo access can become root, so the list of those accounts is the list of people and programs that fully control the server. On most servers nobody has read that list since the day someone set the machine up. This guide shows how to produce it in five commands, how to spot the risky entries and how to remove access without locking yourself out.
We ran all commands in our lab on Ubuntu 24.04 in November 2026 with two test users, ravi and deploy. Outputs are real and trimmed to those users.
The short answer
getent group sudo
sudo grep -vE '^\s*(#|$)' /etc/sudoers
sudo ls -l /etc/sudoers.d
sudo grep -rn NOPASSWD /etc/sudoers /etc/sudoers.d
sudo -l -U username1. Members of the sudo group
On Ubuntu and Debian, membership of the sudo group gives full sudo access. On Red Hat, Rocky and AlmaLinux the group is wheel.
getent group sudosudo:x:27:ubuntu,raviTwo accounts here can run any command as root. Use getent and not cat /etc/group, because getent also shows members that come from LDAP or Active Directory.
2. Rules in the sudoers file
sudo grep -n '^root\|^%\|^@includedir' /etc/sudoers47:root ALL=(ALL:ALL) ALL
50:%admin ALL=(ALL) ALL
53:%sudo ALL=(ALL:ALL) ALL
57:@includedir /etc/sudoers.dA name with a % in front is a group. Line 50 gives the same rights to a group called admin, which is easy to miss if you only look at the sudo group. Check who is in every group named here. The last line pulls in every file from /etc/sudoers.d, and that is where most extra rights come from.
3. Drop-in files
sudo ls -l /etc/sudoers.d-r--r----- 1 root root 1068 Jan 29 2024 README
-r--r----- 1 root root 60 Nov 13 16:05 deployCloud images, installers and deployment tools put files here. Read every one. A file dated long after the server build, which nobody on the team remembers adding, needs an answer.
4. Rules that skip the password
sudo grep -rn NOPASSWD /etc/sudoers /etc/sudoers.d/etc/sudoers.d/deploy:1:deploy ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginxThis is the test rule we added: the deploy user may restart Nginx as root without a password and may do nothing else. A narrow rule with a full path, like this one, is how to use NOPASSWD. These forms are the dangerous ones:
NOPASSWD: ALL. Anyone who gets into that account, or into a script running as it, is root at once.- A rule for an editor, a pager or an interpreter, such as
vim,less,find,pythonorbash. Each of these can start a shell, so the rule is the same as ALL. GTFOBins lists which programs can do this. - A rule for a script that the user can edit. Check the script and its directory with
ls -l. If the user can write to either, the user decides what runs as root. - Wildcards in a command path or its arguments.
5. What one user may run
sudo -l -U ravi
sudo -l -U deployUser ravi may run the following commands on vm:
(ALL : ALL) ALL
User deploy may run the following commands on vm:
(ALL) NOPASSWD: /usr/bin/systemctl restart nginxsudo -l -U works out the final answer for a user from every file and group, so it is the check to trust.
Other ways to be root
sudo is one route. Check the other two:
awk -F: '$3==0{print $1}' /etc/passwd
sudo passwd -S rootroot
root L 2026-10-24 0 99999 7 -1Only root should have UID 0. The L in the second output means the root password is locked, so nobody can log in as root with a password or use su with one. That is the Ubuntu default and worth keeping.
Remove access safely
Keep a second root session open in another terminal while you change sudo rights. If you make a mistake, that session can still fix it.
To take a user out of the sudo group:
sudo gpasswd -d ravi sudoRemoving user ravi from group sudoThe change applies at the user’s next login. Afterwards:
sudo -l -U raviUser ravi is not allowed to run sudo on vm.To change a rule, never open the files in a normal editor. visudo checks the syntax before it saves:
sudo visudo -f /etc/sudoers.d/deployA syntax error in any sudoers file can stop sudo from working for everyone. This is what the checker printed when we left out a colon on purpose:
sudo visudo -c/etc/sudoers.d/broken:1:27: syntax error
deploy ALL=(ALL) NOPASSWD ALL
^~~On a healthy system the same command prints “parsed OK” for each file.
See everyone at once
This loop prints the final sudo rights of every normal user account:
for u in $(awk -F: '$3>=1000 && $3<65534{print $1}' /etc/passwd); do printf "%s: " "$u"; sudo -l -U "$u" | tail -1 | sed 's/^ *//'; doneubuntu: (ALL : ALL) ALL
deploy: (ALL) NOPASSWD: /usr/bin/systemctl restart nginx
ravi: User ravi is not allowed to run sudo on vm.Save the output with the date. Next quarter, run it again and compare.
Who used sudo
sudo records every command. On Ubuntu the entries are in the journal and in /var/log/auth.log:
sudo journalctl _COMM=sudo --since "7 days ago"
sudo grep 'sudo:' /var/log/auth.log | tail -n 20Each entry names the user, the terminal, the working directory and the command. Failed attempts appear with “incorrect password attempts” or “user NOT in sudoers”. These two commands are standard usage and we did not run them in our lab container, which has no system journal.
A short policy that works
- Give sudo to named people. Shared accounts hide who did what.
- Remove access the day someone leaves or changes role.
- Service accounts get one narrow NOPASSWD rule each, with full paths, and nothing else.
- Review the list every quarter and after every incident.
SSH Hardening Pack, ₹99: tested sshd_config drop-ins, a fail2ban jail, a guided install script that refuses to lock you out, a verify script and a printable checklist, for the door all of these accounts come in through. See the pack.
Related guides
- How to Find SUID Files and World-Writable Files on Linux
- How to Check If Your Linux Server Is Hacked: 10 Commands
- Who Logged In to My Linux Server?
- SSH Hardening Checklist for Ubuntu 24.04
Sources
Want a second pair of eyes on your servers? Our Linux server hardening audit reviews accounts, sudo rules, permissions and SSH settings. The free website safety check grades your site from the outside in 30 seconds.
