86

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.

2
  • 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 Jun 27 '18 at 5:26
117

All users:

$ getent passwd

All groups:

$ getent group

All groups with a specific user:

$ getent group | grep username
4
  • 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? Sep 15 '15 at 3:07
  • 4
    @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. Mar 21 '18 at 13:46
23

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
3
  • 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. Feb 13 '17 at 13:30
6

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
2
  • 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! Nov 22 '17 at 21:55
3

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
1
  • 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
0

Use getent passwd{1000..60000} to list only manually added users. See: https://linuxize.com/post/how-to-list-users-in-linux/

for user in $(getent passwd {1000..60000} |awk -F: '{print $1}');
do 
    groups $user; 
done
-1

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
1
  • 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
-3

Like this:

sudo cat /etc/gshadow |grep group
sudo cat /etc/gshadow |grep username
1
  • 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
-3

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 agree to our terms of service, privacy policy and cookie policy

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