In Linux, a group is a unit that is useful to share files/directories within the local system or from the local to the remote system. It can contain multiple users that are known as its group members. Each group member has the same permissions and privileges that can be easily managed by using the right commands.
This post illustrates possible methods to list the members of a group in Linux having the following outcomes:
- Method 1: Using the “getent” Command
- Method 2: Using the “members” Command
- Method 3: Using the “groupmems” Command
- Method 4: Using the “libuser-lid” Command
- Method 5: Using the “/etc/group” File
Method 1: Using the “getent” Command
The “getent” is a built-in Linux command to show the details of the database having entries “passwd”, “group”, “services”, “network”, and “protocols”. It is known as the common method to get user details in Linux.
The “getent” command displays the group databases configured in /etc/nsswitch.conf file.
Execute the “getent” command with the specified group to check its group members:
$ getent group sudo
The output displays four entries in one line i.e group_nmae, password(encrypted), group_id, and group_list(group_member “itslinuxfoss”).
Here the “sudo” group member is only one “itslinuxfoss”.
Method 2: Using the “members” Command
As the “member” command name clearly tells that it is beneficial for listing the members of the group.
It is an external command so first install it in the Linux system as per distributions using default package managers:
$ sudo yum install members #For Fedora $ sudo dnf install members #For CentOS/RHEL $ sudo apt install members #For Ubuntu/Debian-Based
The “members” utility has been successfully installed in current Linux distro Ubuntu 22.04.
Simply enter the “member” command with the targeted group name to display its members’ list in this way:
$ member scanner
It displayed only the group member “saned” of the “scanner” group.
Method 3: Using the “groupmems” Command
The “groupmems” is another command line tool that allows Linux users to manage their group membership without having superuser privileges.
However, it needs the superuser privileges to access the “/etc/gshadow” file for getting the group members by following the “-l(list)” flag alongside the group name:
$ sudo groupmems -g sambashare -l
The “sambashare” group is associated with the group member “itslinuxfoss”.
Method 4: Using the “libuser-lid” Command
Another external “lid” command is beneficial for getting group details. It is just like the “groupmems”, and the “members” command that displays members of a group in the Linux operating system.
Install the libuser Command Support
It is not pre-installed in the Linux operating system but can be easily installed using the below-mentioned commands:
$ sudo yum install libuser #For Fedora $ sudo dnf install libuser #For CentOS/RHEL $ sudo apt install libuser #For Ubuntu/Debian-Based
When the installation is completed, run “libuser-lid” with the “-g(group)” flag to get the group members of “adm” group with superuser privileges:
$ sudo libuser-lid -g adm
The output shows the “adm” group members list along with their IDs.
Method 5: Using the “/etc/group” File
The “/etc/group” is a text file that contains the user’s group information. It contains group information in four entities that are described here:
- Group_name: Represents the user group name.
- Password: Shows the encrypted password. It is usually blank in case of no password.
- Group_ID: Identifies the group id.
- Group List: Displays the group members separated by commas.
It generally displays all group details however to get the specified group members list use it with the “grep” command:
$ grep sudo /etc/group
The user “itslinuxfoss” is a member of the “sudo” group.
Linux and its distributions offer the “getent”, “groupmems”, “members”, and “libuser_lid” command line utilities for listing the group members of a group. In addition, this task can also be performed by accessing the “/etc/group” file using the “cat(for all)”, and “grep(for particular)” commands. This guide has provided a detailed view to list the members of a group in Linux.