To list
To list all local users you can use:
cut -d: -f1 /etc/passwd
To list all users capable of authenticating (in some way), including non-local, see this reply.
Some more useful user-management commands (also limited to local users):
To add
To add a new user you can use:
sudo adduser new_username
or:
sudo useradd new_username
See also: What is the difference between adduser and useradd?
To remove/delete
To remove/delete a user, first you can use:
sudo userdel username
Then you may want to delete the home directory for the deleted user account :
sudo rm -r /home/username
Please use with caution the above command!
To modify
To modify the username of a user:
usermod -l new_username old_username
To change the password for a user:
sudo passwd username
To change the shell for a user:
sudo chsh username
To change the details for a user (for example real name):
sudo chfn username
To add a user to the sudo
group:
adduser username sudo
or
usermod -aG sudo username
And, of course, see also: man adduser
, man useradd
, man userdel
... and so on.
Best Answer
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 line