Show all users and their groups/vice versa

232,535

Solution 1

All users:

$ getent passwd

All groups:

$ getent group

All groups with a specific user:

$ getent group | grep username

Solution 2

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

Solution 3

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

Solution 4

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
Share:
232,535

Related videos on Youtube

Steve Robbins
Author by

Steve Robbins

Updated on September 18, 2022

Comments

  • Steve Robbins
    Steve Robbins over 1 year

    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.

    • Chris
      Chris over 12 years
      there is no such command. You need to script it by your self.
    • Sudip Bhandari
      Sudip Bhandari almost 6 years
      how about cat /etc/passwd
  • EEAA
    EEAA over 12 years
    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?
  • Magellan
    Magellan over 12 years
    It certainly wouldn't get anything that lives in a centralized repository. And that's definitely information that you'd want to see.
  • PHP Learner
    PHP Learner over 8 years
    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?
  • EEAA
    EEAA over 8 years
    @PHPLearner If you have another question, please post a question, not a comment.
  • FooBee
    FooBee over 7 years
    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.
  • HBruijn
    HBruijn over 7 years
    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
    HBruijn over 7 years
    +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.
  • Mian Asbat Ahmad
    Mian Asbat Ahmad over 7 years
    Excellent very helpful, it would be better to mention that they are seperate commands.
  • marcelm
    marcelm over 6 years
    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!
  • MadHatter
    MadHatter over 6 years
    This has the same problem as Chang's answer in that it ignores users/groups originating in databases such as LDAP, NIS, etc.
  • itzforu
    itzforu over 6 years
    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!