Show all users and their groups/vice versa
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
Related videos on Youtube
Steve Robbins
Updated on September 18, 2022Comments
-
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, andshowgroups -u thisuser
would show all the groups that havethisuser
in it.-
Chris over 12 yearsthere is no such command. You need to script it by your self.
-
Sudip Bhandari almost 6 yearshow about
cat /etc/passwd
-
-
EEAA over 12 yearsWhile 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 over 12 yearsIt certainly wouldn't get anything that lives in a centralized repository. And that's definitely information that you'd want to see.
-
PHP Learner over 8 yearsI found that there is a user named
speech-dispatcher
that belongs to group audio (based ongroups speech-dispatcher
). But it is not listed undergetent group
command! What is the problem? -
EEAA over 8 years@PHPLearner If you have another question, please post a question, not a comment.
-
FooBee over 7 yearsNo.
/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 over 7 yearsIn contrast with the already accepted this does not list users/groups that originate from a remote user database such as LDAP, NIS etc....
-
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 over 7 yearsExcellent very helpful, it would be better to mention that they are seperate commands.
-
marcelm over 6 yearsHi 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 over 6 yearsThis has the same problem as Chang's answer in that it ignores users/groups originating in databases such as LDAP, NIS, etc.
-
itzforu over 6 yearsThis 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!