I want a command to list all users who have root privileges i.e. sudo ?
Suppose I'm a sudoer user. How could I know all other sudoer users?
I want a command to list all users who have root privileges i.e. sudo ?
Suppose I'm a sudoer user. How could I know all other sudoer users?
If you just need to list the sudoers listed in the
sudo
group, I think that the best way to do it would be to run this command (which should be computationally lighter than any of the other commands in this answer):Also as suggested in the comments by muru, the format of the entries in
/etc/group
can be easily handled bycut
:Also again as suggested in the comments by muru, one can use
getent
in place ofgrep
:Any of these commands will print all the users listed in the
sudo
group in/etc/group
(if any).Command #1 breakdown:
grep
: Prints all the lines matching a regex in a file-P
: makesgrep
match Perl-style regexeso
: makesgrep
print only the matched string'^sudo.+:\K.*$'
: makesgrep
match the regex between the quotesRegex #1 breakdown:
^
: start of line.+
: one or more characters\K
: discard the previous match.*
: zero or more characters$
: end of lineCommand #2 breakdown:
grep
: Prints all the lines matching a regex in a file'^sudo.+:\K.*$'
: makesgrep
match the regex between the quotescut
: Prints only a specified section of each line in a file-d:
: makescut
interpret:
as a field delimiter-f4
: makescut
print only the fourth fieldRegex #2 breakdown:
^
: start of line.*
: zero or more characters$
: end of lineAs it stated here I consider the simpliest way to discover with
-l
&-U
options together, just typeusers
it will list e.g.:John
then:If the user has
sudo
access, it will print the level ofsudo
access for that particular user:If the user don't have sudo access, it will print that a user is not allowed to run
sudo
on localhost:Expanding on the
sudo -l -U
test, one can usegetent passwd
to determine the users who can usesudo
. Usinggetent
allows us to access users who may not be present in thepasswd
file, such as LDAP users:sudo -U
does not return a non-zero exit value that we could take advantage of, so we are reduced to grepping the output.As it has already been stated, the answer can be found on Unix & Linux Stack Exchange:
The only difference is that the group in Ubuntu is not
wheel
, butsudo
(oradmin
in older versions of Ubuntu). So the command becomes:Command:
Output:
Tom, Stacy are the users with sudo privileges.
On most Unix-like systems, that have the sudo command, and have a sudo configuration file; running visudo as root:
or
will allow an administrator to inspect and amend the privileges of groups that can use the sudo command.
On Debian based Unix-like systems, like Ubuntu, the groups 4 and 27 generally have access rights to the sudo privileges.
Group 4 is the administrator group (adm) and group 27 is the sudo gid.
To see what users are currently assigned to these groups cat the /etc/group file as shown below:
A sample output, on Ubuntu (but not Redhat based, Oracle Solaris/Solaris based, or BSD based systems) would yield this:
As we can tell, youruser is the administrator of the system, and member of group 4 (adm). But youruser and mybrother are both members of group 27, which is the gid (group identification) number of group sudo. So mybrother can also attain root privileges (super user).
Many linux systems like Fedora and Slackware, incorporate the wheel group gid=10. Which allows administrator privileges when the sudo command is applied. On BSD based systems (e.g. FreeBSD), the root user is a member of the wheel group which is gid 0.
Also by using the id command any user can find the group information of another known user to the system.
For Example:
Sample output
This command returns a list of users with sudo rights:
Output is (e.g.):
If only the user name to be displayed, then this command:
I was stumped about how the
vagrant
user can usesudo
even without being mentioned in/etc/sudoers
nor in/etc/group
nor found withgetent
.Turns out
sudo
also reads entries from all files under/etc/sudoers.d/
. So if you haven't looked through that directory, you may not realize how many users actually havesudo
access.This kind of
sudo
access can be detected by JoKeR's answer usingsudo -l -U vagrant
but is not detected by any of the other answers here which all rely on eithergetent
or/etc/group
.Based on answers from @muru and @kos, but with some optimizations.
The
getent group sudo
command lists only users who have the sudo group. The rest of the commands test whether the user is actually capable of running as root. There are also other additions to improve the formatting of the output.COMMAND
OUTPUT
Thanks! =D
[Refs.: https://askubuntu.com/a/611607/134723 , https://askubuntu.com/a/611646/134723 , https://unix.stackexchange.com/a/136798/61742 , https://unix.stackexchange.com/a/26639/61742 ]