Tuesday, June 4, 2019

List sudo or super users in Linux


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.


No comments:

Post a Comment