I know I have existing groups and users but I'm not sure about their association. Is there an shell command I can use to list all users or all groups and a command to list all groups/users for a specified user/group?

So something like showusers would list all users, and showgroups -u thisuser would show all the groups that have thisuser in it.

  • there is no such command. You need to script it by your self. – Chris Feb 2 '12 at 13:25
  • how about cat /etc/passwd – Sudip Bhandari Jun 27 at 5:26
up vote 69 down vote accepted

All users:

$ getent passwd

All groups:

$ getent group

All groups with a specific user:

$ getent group | grep username
  • I found that there is a user named speech-dispatcher that belongs to group audio (based on groups speech-dispatcher). But it is not listed under getent group command! What is the problem? – PHP Learner Sep 15 '15 at 3:07
  • 3
    @PHPLearner If you have another question, please post a question, not a comment. – EEAA Sep 15 '15 at 3:08
  • +1 since this will also list users/groups not found in the conventional /etc/passwd & /etc/group files i.e. when a system is configured to use central directories such NIS and LDAP, or any other alternative user/group database, as long as that supports user/group enumeration. – HBruijn Nov 30 '16 at 11:57
  • This will not return all users and groups in an ldap or sssd config if enumeration is turned off. – Jens Timmerman Mar 21 at 13:46

List users and their groups:

for user in $(awk -F: '{print $1}' /etc/passwd); do groups $user; done

List groups and their users:

cat /etc/group | awk -F: '{print $1, $3, $4}' | while read group gid members; do
    members=$members,$(awk -F: "\$4 == $gid {print \",\" \$1}" /etc/passwd);
    echo "$group: $members" | sed 's/,,*/ /g';
done
  • 1
    While that would probably work, it seems a bit overly complicated, doesn't it, when there are perfectly good simple one-shot commands to do this? – EEAA Jan 31 '12 at 4:41
  • It certainly wouldn't get anything that lives in a centralized repository. And that's definitely information that you'd want to see. – Magellan Jan 31 '12 at 23:50
  • Excellent very helpful, it would be better to mention that they are seperate commands. – Mian Asbat Ahmad Feb 13 '17 at 13:30

List all users

cut -d':' -f 1 /etc/passwd

Or

awk -F ':' '{print $1}' /etc/passwd

While cat /etc/passwd shows all users (and a bunch of other stuff), cut -d ':' -f 1 is a simple way to split each line with ':' as a delimiter and extract just the first field (users). Pretty much the same as awk version.

List all groups

cut -d':' -f 1 /etc/group

Or

awk -F ':' '{print $1}' /etc/group

Guess what, very simmilar to listing users. Just parse /etc/group instead.

Another interesting way, maybe closer to what OP wanted, is compgen. Not sure about compatibility issues though.

compgen -u
compgen -g
  • 2
    Hi Elliot Baily, welcome to Server Fault! Please note that this question is over 5 years old, and already has a correct and accepted answer. Also note that your solution only works if users are stored in /etc/passwd; the accepted answer also works for other user databases (such as NIS or LDAP). If you want to answer old questions (which is perfectly fine!) you might want to have a look at the list of unanswered questions - plenty of questions looking for some love! – marcelm Sep 24 '17 at 19:25

If you dont care about remote users such as LDAP or NIS, to list users and their associated groups in a simple way:

cut -d: -f1 /etc/passwd | xargs groups

Output;

root : root
myuser : root www-data fuse 
anotheruser : anotheruser   cdrom floppy audio dip video plugdev scanner bluetooth netdev
  • 1
    This has the same problem as Chang's answer in that it ignores users/groups originating in databases such as LDAP, NIS, etc. – MadHatter Oct 19 '17 at 6:25
  • This very neatly outputs the information in an incredibly clear format though, so can still be a useful first step. It helped jog my memory regarding the syntax of /etc/group and /etc/passwd! – Chris Woods Nov 22 '17 at 21:55

for debian

cat /etc/passwd # show all users
cat /etc/group # show all groups
cat /etc/passwd | grep group # show all users with specified group
  • 2
    In contrast with the already accepted this does not list users/groups that originate from a remote user database such as LDAP, NIS etc.... – HBruijn Nov 30 '16 at 11:38

Like this:

sudo cat /etc/gshadow |grep group
sudo cat /etc/gshadow |grep username
  • 1
    No. /etc/gshadow doesn't contain group members (/etc/group does) and the accepted answer from 4.5 years ago is much more general anyway, as it also handles remote groups. – Sven Sep 2 '16 at 20:33

Use this command to get all the group and users in that particular group.

grep '
> ' /etc/group

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.