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 username

1. 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 sudo
sudo:x:27:ubuntu,ravi

Two 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/sudoers
47:root	ALL=(ALL:ALL) ALL
50:%admin ALL=(ALL) ALL
53:%sudo	ALL=(ALL:ALL) ALL
57:@includedir /etc/sudoers.d

A 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 deploy

Cloud 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 nginx

This 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, python or bash. 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 deploy
User 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 nginx

sudo -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 root
root
root L 2026-10-24 0 99999 7 -1

Only 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 sudo
Removing user ravi from group sudo

The change applies at the user’s next login. Afterwards:

sudo -l -U ravi
User 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/deploy

A 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/^ *//'; done
ubuntu: (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 20

Each 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

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.

Post author avatar

Bhaskar Soni

Bhaskar Soni is a Security Engineer and the founder of Youngster Company, based in Ahmedabad, Gujarat. He works hands-on with Linux server hardening, vulnerability assessment and penetration testing (VAPT), ISO 27001 compliance and OSINT investigations. He writes practical, tested guides for IT professionals and Indian businesses, and every article on this site is written or reviewed by him.

Leave a Reply