I've recently been creating new users and assigning them to certain groups. I was wondering if there is a command that shows all the users assigned to a certain group? I have tried using the 'groups' command however whenever I use this it says 'groups: not found'
11 Answers
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.

- 829,060

- 2,812
-
3The other answers doesn't apply if you are not administrator and the group info is stored in other server. – Andrés Alcarraz Feb 04 '18 at 20:37
-
2This could be really confusing probably because of primary/secondary difference. I think this should be avoided in favor of
sudo lid -g {group}
.I have a system where this answer lists 8 users in a group whereassudo lid -g {group}
lists 10. – Dima Korobskiy Jul 25 '18 at 15:36 -
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.

- 544,893

- 2,003
- 19
- 12
-
12
-
3This could be really confusing probably because of primary/secondary difference. I think this should be avoided in favor of
sudo lid -g {group}
.I have a system where this answer lists 8 users in a group whereassudo lid -g {group}
lists 10. – Dima Korobskiy Jul 25 '18 at 15:35 -
2
-
2This should NOT be the accepted answer. Modern Linux installations have multiple sources for user/group information - not just local
/etc/passwd
and/etc/group
- e.g.nsswitch
orsssd
. Usegetent passwd
for user info &getent group
for group information - this will cover all modern Linux configurations. – colm.anseo Mar 22 '22 at 19:36
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)

- 829,060

- 695
-
3It works for a particular group if
| grep {group}
is added and gives the correct answer unlikegetent group name_of_group
orgrep '^group_name_here:' /etc/group
– Dima Korobskiy Jul 25 '18 at 15:39 -
2Instead of
cat /etc/passwd
, you should usegentent passwd
so users in nis/ldap would still be listed. The only drawback is that it can take quite a while. – Brian Minton Jun 05 '19 at 21:13
groupmems -g groupname -l
lists all users in the named group.

- 10,519

- 181
-
Note that
groupmems
is part of the shadow utils used on most Linux distros, howevergroupmems
is currently absent from Debian and derivative (a bug now fixed but not included in any release yet (as of Nov 2016)) – Stéphane Chazelas Nov 06 '16 at 22:14 -
5Also note that
groupmems
only deals with groups in/etc/group
(not the ones in LDAP or other user database) and requires superuser privileges as it tries to open /etc/gshadow. – Stéphane Chazelas Nov 06 '16 at 22:28 -
3Despite the caveats mentioned above, this command is ideal for certain situations because it doesn't require additional parsing of the output (i.e.
cut
and friends). – bonh Oct 23 '17 at 14:55 -
2This could be really confusing probably because of primary/secondary difference. I think this should be avoided in favor of
sudo lid -g {group}
. I have a system where this answer lists 8 users in a group whereassudo lid -g {group}
lists 10. – Dima Korobskiy Jul 25 '18 at 15:42
groups
command prints group memberships for a user. You can use lid
command to list users in a group like:
# lid -g <groupname>
Update: On Debian based distributions the command name differs as libuser-lid
. Both commands are provided by libuser
package as @chris-down mentioned.
$ sudo libuser-lid -g lpadmin
kadir(uid=xxxx)

- 264
- 1
- 6
-
10
lid
is part of libuser, which is not installed by default on many distributions. – Chris Down Nov 06 '15 at 11:57 -
3What's more, on Ubuntu 20.04 LTS,
lid
is part of theid-utils
package. After installation it turned out that thislid
does not support the-g
option. I understand that Kadir answered 6 years ago, but maybe it's time to update the information given here. – András Aszódi May 06 '21 at 13:53 -
@LaryxDecidua
id-utils
manipulates id databases, it doesn’t work with files such as/etc/group
or/etc/passwd
. Itslid
is not at all similar tolibuser
’s. – Stephen Kitt Jun 05 '22 at 16:34
Works like a charm:
cut -d: -f1,4 /etc/passwd | grep $(getent group <groupname> | cut -d: -f3) | cut -d: -f1

- 191
-
Unlike the accepted answer of @ARG, this command lists the users having
as their primary group – Bhavik Mar 07 '17 at 05:28 -
-
3I disagree. Because it reads users in /etc/passwd, this will not work with other nsswitch modules that access LDAP etc. – Ivan Vučica Oct 25 '17 at 11:00
-
Didn't work correctly for me: I got 4 members in a group whereas
sudo lid -g
lists 8. @Bhavik The accepted answer is not correct either. – Dima Korobskiy Jul 25 '18 at 15:54 -
Works nicely, especially if
cut -d: -f1,4 /etc/passwd
is replaced withgetent passwd | cut -d: -f1,4
. As many people have pointed it out,getent
will query non-local information sources. – András Aszódi May 06 '21 at 15:15
Some will tell you to install libuser (for 'lid') or members (for 'members'). But building upon the answer https://unix.stackexchange.com/a/349648/77959 which handled this issue with login group membership I found another group not being covered by that script. So - here's the best of both approaches combined:
#!/bin/bash
if [ $# -eq 1 ]; then
gid=`getent group "$1"|cut -d: -f3`
list_a=`cut -d: -f1,4 /etc/passwd | grep ":$gid$" | cut -d: -f1`
list_b=`getent group "$1"|cut -d: -f4|sed 's/,/\n/g'`
echo -e "$list_a\n$list_b"|grep -v "^$"|sort|uniq
else
echo "pass me a group to find the members of"
fi

- 149

- 356
-
It worked correctly on my system unlike answers involving
getent
or grep'^group_name_here:' /etc/group
– Dima Korobskiy Jul 25 '18 at 15:58
I am surprised nobody mentioned
id <user>
This command will give a list of groups the user is in.

- 429
- 3
- 5
-
14Because - contrary to the title - the questioner wanted to know the users within a given group, not the groups of a given user, as detailed in the question. I now rephrased the title to match the contents. – Dubu Nov 09 '15 at 10:10
-
-
2Even though , is it different from the actual question, everyone will find this too as a useful information , I bet ! – Arun Feb 27 '21 at 05:02
OP phrased the question to exclude the possibility of using the groups command. Since that is part of coreutils on Linux, either (a) it was removed, or (b) OP is mistyping the name.
OP could have used groups
like this, for instance:
for name in $(cut -d: -f1 /etc/passwd);do groups $name|grep -w sudo|awk '{print $1;}';done
One suggested answer just grep's for the group name in /etc/group
. Sometimes that works as intended.
A slightly better use of grep takes into account the syntax of /etc/group
:
group_name:password:GID:user_list
so that only the part before the first colon is a valid group-name. A plain grep without regard to syntax can (and will) pick up misleading matches from the file. Use regular expressions to make the grep match exactly what is needed:
grep -E '^users:' /etc/group |sed -e 's/^.*://'
or using a shell variable:
grep -E '^'$groupname':' /etc/group |sed -e 's/^.*://'
However, that only lists those not in a default group. To add those, you need to take into account the password file, e.g., by extracting the group-id number from /etc/group
, and printing the users whose default group matches from /etc/passwd
, e.g.,
grp=$(awk -F: '$1 ~ /^users$/ {print $3; }' </etc/group)
awk -F: '$4 ~ /^'$grp'$/ { print $1; }' </etc/passwd
You could do the same thing using just grep and sed, but it is more work than using awk.
Another suggested answer proposed using getent
, which also is likely to be on a Linux machine (with Debian, it is part of GNU libc). However a quick check of that shows it providing only the /etc/group
content.
I (like most) do not have libusers
or lid
installed, so I cannot comment on whether it satisfies OP's conditions.
There is also the id
program, which gives group information. Someone might expand on that as a possible answer.

- 76,765
-
Or just
sed -n "s/^$groupname:.*://p" /etc/group
but that could still report wrong results if the group name contains RE operators (.
for instance is not uncommon in group names). – Stéphane Chazelas Nov 06 '16 at 21:54 -
2GNU
getent
will also query LDAP/NIS... though possibly not when enumeration is explicitly disabled for the group database. – Stéphane Chazelas Nov 06 '16 at 21:56 -
Note that
groups
would not help as it lists the groups a given user is member of as opposed to the list of members of a given group. – Stéphane Chazelas Nov 06 '16 at 22:01
function members {
echo "$(getent group $1 | cut -d: -f1,2,3):$(getent passwd | cut -d: -f1,4 | grep $(getent group $1 | cut -d: -f3) | cut -d: -f1 | paste -sd ','):$(getent group $1 | cut -d: -f4)"
}
Lists primary and secondary members separated by a ":"

- 1,637

- 11
This modification of user3717722 approach will list groupmembers in an NIS database:
ypcat passwd | cut -d: -f1,4 | grep $(getent group <groupname> | cut -d: -f3) | cut -d: -f1
groups
command. It is unlikely that you do not have it on Linux, since it is part of coreutils. – Thomas Dickey Nov 06 '15 at 11:50