How can I find out which users are in a group within Linux?

I prefer to use the getent command ...

Since getent uses the same name service as the system, getent will show all information, including that gained from network information sources such as LDAP.

So for a group, you should use the following ...

getent group name_of_group

where name_of_group is replaced with the group you want to look up. Note that this only returns supplementary group memberships, it doesn't include the users who have this group as their primary group.

There are a whole lot of other lookups that you can do ... passwd being another useful one, which you'll need to list primary groups.


You can use grep:

grep '^group_name_here:' /etc/group

This only lists supplementary group memberships, not the user who have this group as their primary group. And it only finds local groups, not groups from a network service such as LDAP.


Easier to do groups [username]

If you want to list all local users and their local groups you can do

cat /etc/passwd | awk -F':' '{ print $1}' | xargs -n1 groups

If you get "groups: command not found", it is likely you've edited your environmental path for the worse, to reset your path do PATH=$(getconf PATH)

Tags:

Users

Group