How can I find out which users are in a group within Linux?
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’
That is the groups command. It is unlikely that you do not have it on Linux, since it is part of coreutils.
11 Answers 11
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.
The other answers doesn’t apply if you are not administrator and the group info is stored in other server.
This could be really confusing probably because of primary/secondary difference. I think this should be avoided in favor of sudo lid -g
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.
This could be really confusing probably because of primary/secondary difference. I think this should be avoided in favor of sudo lid -g
This 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 or sssd . Use getent passwd for user info & getent group for group information — this will cover all modern Linux configurations.
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)
It works for a particular group if | grep
Instead of cat /etc/passwd , you should use gentent passwd so users in nis/ldap would still be listed. The only drawback is that it can take quite a while.
groupmems -g groupname -l
lists all users in the named group.
Note that groupmems is part of the shadow utils used on most Linux distros, however groupmems is currently absent from Debian and derivative (a bug now fixed but not included in any release yet (as of Nov 2016))
Also 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.
Despite 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).
This could be really confusing probably because of primary/secondary difference. I think this should be avoided in favor of sudo lid -g
groups command prints group memberships for a user. You can use lid command to list users in a group like:
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)
What’s more, on Ubuntu 20.04 LTS, lid is part of the id-utils package. After installation it turned out that this lid 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.
@LaryxDecidua id-utils manipulates id databases, it doesn’t work with files such as /etc/group or /etc/passwd . Its lid is not at all similar to libuser ’s.
I am surprised nobody mentioned
This command will give a list of groups the user is in.
Because — 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.
Even though , is it different from the actual question, everyone will find this too as a useful information , I bet !
cut -d: -f1,4 /etc/passwd | grep $(getent group | cut -d: -f3) | cut -d: -f1
I disagree. Because it reads users in /etc/passwd, this will not work with other nsswitch modules that access LDAP etc.
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.
Works nicely, especially if cut -d: -f1,4 /etc/passwd is replaced with getent passwd | cut -d: -f1,4 . As many people have pointed it out, getent will query non-local information sources.
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
It worked correctly on my system unlike answers involving getent or grep ‘^group_name_here:’ /etc/group
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 '';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.,
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.
How to find out what group a given user has?
This appears to be pretty useful as well. It has more verbose output than the ‘groups’ command, so if you need the group id/user id use this!
On Linux/OS X/Unix to display the groups to which you (or the optionally specified user) belong, use:
which is equivalent to groups [user] utility which has been obsoleted on Unix.
On OS X/Unix, the command id -p [user] is suggested for normal interactive.
Explanation on the parameters:
-G , —groups — print all group IDs
-n , —name — print a name instead of a number, for -ugG
-p — Make the output human-readable.
or just study /etc/groups (ok this does probably not work if it uses pam with ldap)
Below is the script which is integrated into ansible and generating dashboard in CSV format.
sh collection.sh #!/bin/bash HOSTNAME=`hostname -s` for i in `cat /etc/passwd| grep -vE "nologin|shutd|hal|sync|root|false"|awk -F':' '' | sed 's/[[:space:]]/,/g'`; do groups $i; done|sed s/\:/\,/g|tr -d ' '|sed -e "s/^/$HOSTNAME,/"> /tmp/"$HOSTNAME"_inventory.txt sudo cat /etc/sudoers| grep -v "^#"|awk ''|grep -v Defaults|sed '/^$/d;s/[[:blank:]]//g'>/tmp/"$HOSTNAME"_sudo.txt paste -d , /tmp/"$HOSTNAME"_inventory.txt /tmp/"$HOSTNAME"_sudo.txt|sed 's/,[[:blank:]]*$//g' >/tmp/"$HOSTNAME"_inventory_users.txt
My output stored in below text files.
cat /tmp/ANSIBLENODE_sudo.txt cat /tmp/ANSIBLENODE_inventory.txt cat /tmp/ANSIBLENODE_inventory_users.txt
How to list all users in a Linux group?
I’m new here, I found out that SF exists right after I posted the question. I agree it belongs either to SF or SO.
20 Answers 20
It is portable across both Linux and Solaris, and it works with local group/password files, NIS, and LDAP configurations.
Unfortunately, there is no good, portable way to do this that I know of. If you attempt to parse /etc/group, as others are suggesting, you will miss users who have that group as their primary group and anyone who has been added to that group via a mechanism other than UNIX flat files (i.e. LDAP, NIS, pam-pgsql, etc.).
If I absolutely had to do this myself, I’d probably do it in reverse: use id to get the groups of every user on the system (which will pull all sources visible to NSS), and use Perl or something similar to maintain a hash table for each group discovered noting the membership of that user.
Edit: Of course, this leaves you with a similar problem: how to get a list of every user on the system. Since my location uses only flat files and LDAP, I can just get a list from both locations, but that may or may not be true for your environment.
Edit 2: Someone in passing reminded me that getent passwd will return a list of all users on the system including ones from LDAP/NIS/etc., but getent group still will still miss users that are members only via the default group entry, so that inspired me to write this quick hack.
#!/usr/bin/perl -T # # Lists members of all groups, or optionally just the group # specified on the command line # # Copyright © 2010-2013 by Zed Pobre (zed@debian.org or zed@resonant.org) # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # use strict; use warnings; $ENV = "/usr/bin:/bin"; my $wantedgroup = shift; my %groupmembers; my $usertext = `getent passwd`; my @users = $usertext =~ /^([a-zA-Z0-9_-]+):/gm; foreach my $userid (@users) < my $usergrouptext = `id -Gn $userid`; my @grouplist = split(' ',$usergrouptext); foreach my $group (@grouplist) < $groupmembers-> = 1; > > if($wantedgroup) < print_group_members($wantedgroup); >else < foreach my $group (sort keys %groupmembers) < print "Group ",$group," has the following members:\n"; print_group_members($group); print "\n"; >> sub print_group_members < my ($group) = @_; return unless $group; foreach my $member (sort keys %<$groupmembers>) < print $member,"\n"; >>