How To Find All Sudo Users In Your Linux System

The other day a fellow Linux user asked me how to find the number of super users (the users who have sudo access) do I have in my Linux server? Good question, but, I didn’t have the answer. LOL! So, I did a quick google search. To my surprise, it’s not that difficult. It’s just a single-line command. How come I didn’t know this simple trick already? For those wondering how to find all sudo users or super users in your Linux system, read on.
You might argue a good Linux administrator must know how many super users and normal users are there in a system he is managing. You’re right! However, there are chances that sometimes we need to give a temporary sudo access to a normal user to install a software or do certain administrative task on his/her own. Over time, we might forget to revoke the sudo access. So, it is good practice to know how many super users are in your system from time to time. If there are any forgotten or unwanted sudo access, you can simply revoke them.
Find All Sudo Users In A Linux System
Let us first list all users in the system. To do so, run:
$ awk -F':' '{ print $1}' /etc/passwd
Sample output from my Ubuntu system:
root daemon bin sys sync games man lp mail news uucp proxy www-data backup list irc gnats nobody systemd-timesync systemd-network systemd-resolve systemd-bus-proxy syslog _apt lxd messagebus uuidd dnsmasq sshd sk senthil kumar ostechnix
You can also use the following command to list all users:
$ compgen -u
Among all users, let us only find the sudo or super users in our Linux system.
$ grep '^sudo:.*$' /etc/group | cut -d: -f4 sk,ostechnix
Also, you can use “getent” command instead of “grep” to get the same result.
$ getent group sudo | cut -d: -f4 sk,ostechnix
As you see in the above output, “sk” and “ostechnix” are the sudo users in my system.
In the above examples, we listed all sudo users. You might want to know whether a certain user has sudo privilege or not.
To do so, run:
$ sudo -l -U sk
Sample output:
Matching Defaults entries for sk on ubuntuserver: env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin User sk may run the following commands on ubuntuserver: (ALL : ALL) ALL
As you see, the user named “sk” can perform all commands. So, he is in the sudo group. Let us check another user.
$ sudo -l -U senthil
Sample output:
User senthil is not allowed to run sudo on ubuntuserver.
Oops! The user “senthil” is not allowed to run sudo, so he is just a normal user.
We can also find if an user has sudo access by running the following command:
$ sudo -nv
If you get nothing as output, the user still has sudo access.
If you see an output like below, then the user doesn’t has sudo access.
$ sudo -nv Sorry, user senthil may not run sudo on ubuntuserver.
Suggested read:
- How To Grant And Remove Sudo Privileges To Users On Ubuntu
- How To Run Particular Commands Without Sudo Password In Linux
- How to force users to use root password instead of their own password when using sudo
And, that’s all for now. Hope you find this useful. Happy weekend!
Cheers!!
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Facebook | Twitter | Google Plus | LinkedIn | RSS feeds
Have a Good day!!
Thanks a lot! “compgen -u” did it for me!