- How can I find out which users are in a group within Linux?
- 11 Answers 11
- How to Check the User Group(s) an Ubuntu User Belongs To
- Search
- About This Site
- Latest Tutorials
- How to Know The Groups of a Linux User
- Check user group in Linux command line
- Find out groups of other users in Linux
- Bonus Tip: Get group information along with gid
- Bonus Tip 2: Get primary group of a user in Linux
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 Check the User Group(s) an Ubuntu User Belongs To
As a system administrator, you can create and manage groups for the user accounts on your Linux system. This way, you can assign administrative & configuration rights to Ubuntu users and files & folder access permissions to an entire group rather than a single user at a time. Sometimes you might need to know which user group a user belongs to verify or perform group management operations or to assign/de-assigning user rights. This article will describe the commands step-by-step that can be used to determine which group a user is a member of.
Open the Ubuntu Terminal through Ctrl+Alt+T or the Dash or connect to the Ubuntu system by SSH.
Enter the following command to see which group the current user belongs to:
This command lists all the groups that you belong to.
Enter the following command to check which group a particular user belongs to:
You can also use the following command to list the group members and their GIDs.
Here is an example for the ‘guest’ user:
The gid output represents the primary group assigned to a user.
Enter the exit command in order to close the terminal window in case you do not want to work further.
Through this simple procedure, you can perform a simple check of a user account’s group information.
Search
About This Site
Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.
Latest Tutorials
How to Know The Groups of a Linux User
This quick tip teaches you how to find the groups a Linux user belongs to in Linux command line.
Groups are the essential part of basic Linux filesystem security by design. If you know about the file permissions in Linux, you already know that groups play a huge role in limiting and allowing access of files to the desired users only.
The idea is to collect users in a group based on their roles. This way, you can easily set permissions for the intended groups of user. For example, users in sudo groups can run commands with superuser privileges while other users cannot.
Now that might make you curious about knowing which groups you belong to and this is exactly what I am going to show you in this quick tutorial.
Check user group in Linux command line
To find out which groups your user account belongs to, simply use this command:
This will show all the groups you belong to.
[email protected]:~$ groups abhishek adm cdrom sudo dip plugdev lpadmin sambashare kvm
As you can see, the user abhishek belongs to groups abhishek, sudo, adm and several other groups.
I am using Ubuntu in this tutorial and Ubuntu creates a group with the same name as the user. This is why you see user abhishek belonging to group abhishek.
Find out groups of other users in Linux
You just learned to see the groups you belong to. What about checking the groups of other users on your system?
You probably already know how to list users in Linux. When you know the username, you can find which group it belongs to by using the groups command in this way:
Obviously, you’ll have to replace the user_name in the above command with the name of the other user.
[email protected]:~$ groups prakash prakash : prakash sudo
You can also check groups of more than one users at a time by
groups user_1 user_2 user_3
The output will display the groups information for each user in separate rows:
[email protected]:~$ groups abhishek prakash abhishek adm cdrom sudo dip plugdev lpadmin sambashare kvm prakash : prakash sudo
Bonus Tip: Get group information along with gid
You can also get group information of a user with id command. The additional benefit of the id command is that it also displays the uid of the user and gid of the groups. Read this article to know more about UID in Linux.
The user name is optional and by default, it will show the information about your user account.
uid=1000(abhishek) gid=1000(abhishek) groups=1000(abhishek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare),127(kvm)
You can see that getting the group information of a user is a simple task. It could come in handy in many situations and I would let you experience them on your own.
Bonus Tip 2: Get primary group of a user in Linux
Every user has a default or primary group. You can check the primary group of a user with id command in the following fashion:
You can change the primary and secondary group of a user with the usermod command.
I hope this quick little tip helped you to list user groups in Linux. You may also want to read about checking the members of a group in Linux.
If you have questions or suggestions, please feel free to use the comment section.