How to List All Users of a Group in Linux
Wondering which users are members of a specific group? This tutorial shows you three simple ways to list all the users of a group in Linux command line.
I have already shown you to list all the users in Linux command line. This quick tip is related and yet different from that.
I presume that you are a bit familiar with the concept of groups and users in Linux. There are several groups and a variety of users in a Linux system. A group can have multiple members while a user can be a member of several groups.
You can check which groups a particular user belongs to and you can also find all the users of a group.
List all users of a group in Linux
In this quick tutorial, I’ll show you different ways to list users in a group in Linux command line.
1. List members of a group in Linux using /etc/group file
The group information is contained in the file /etc/group. You can view the content of this file and look for the information about the members.
Normally, this file has entry in the following format:
Here’s the explanation of the fields:
- adm is the group name
- x represents password field (you won’t see password in clear text of course)
- 4 is the Group ID aka GID
- syslog and abhishek are the users belonging to the group adm
grep '^group_name:.*$' /etc/group | cut -d: -f4
The above command looks for all the lines starting with the specified group name and then the cut command extract the fourth column separated with : delimiter. The result is just the name of the group members.
grep '^adm:.*$' /etc/group | cut -d: -f4 syslog,abhishek
2. List group members in Linux with getent command
getent is a multipurpose command that is used to query from database files in the /etc directory. So you can use it to query the /etc/group file and get the users of the specified group in the following manner:
getent group group_name
This will display the line matching the group name and in here you can see the members of the group:
getent group sudo sudo:x:27:abhishek
3. List users in a group using ‘members’ command
There is a tiny command line tool that simplifies the process of listing all the members of a specific group.
The members command is usually not installed in all the systems so you have to install it on your own.
On Debian/Ubuntu based systems, you can install it using the following command:
sudo apt install members
If the command is not found in Ubuntu, you should enable the universe repository and try it again.
Once you have the command installed, you can run it like this:
For example, if you want to check which users have sudo access, you can use the members command like this:
And the output will list all the users of the sudo group.
members sudo abhishek
See, it was absolutely simple to get the users belonging to a group. You learned three ways to do it.
Which method did you like the most? Or, do you use some other way to list group members in Linux? Why not share it with us here?