How to List Users in a Group in Linux

Yahya Irmak Feb 02, 2024
  1. List Users in a Group in Linux
  2. Read Contents of the /etc/group in Linux
  3. Use the groups Command in Linux
  4. Use the lid Command in Linux
  5. Read Contents of the /etc/passwd in Linux
  6. Use the getent Command in Linux
  7. Use the groupmems Command in Linux
  8. Use the id Command in Linux
How to List Users in a Group in Linux

Groups can be created in the Linux operating system to set read, write, and execute privileges for specific users.

Users can be added or removed from groups. This article will explain how to list users in a group in Linux.

List Users in a Group in Linux

Each Linux user is also a group member created under their name. In addition, they can be members of various authority groups.

Read Contents of the /etc/group in Linux

The /etc/group contains a list of all groups on the system. With the grep command, we can get the users from the group we want from the list.

grep "^sudo" /etc/group

Output:

grep etc group

Use the groups Command in Linux

The groups command lists the groups in the system. If you want to record the groups that a particular user is a member of, pass the username as a parameter.

groups test

Output:

groups

Use the lid Command in Linux

The lid command is included in the libuser library and displays the user’s groups or group’s users.

The libuser tool can be installed on Linux distributions with the following commands.

The codes using the Ubuntu / Debian are:

sudo apt-get install libuser

The codes using the CentOS / Fedora are:

sudo yum install libuser

By default, this command displays the current user’s groups. To view groups of a specific user, give the user’s name as a parameter.

Or use the -g flag to view members belonging to a group and then type the group name.

sudo libuser-lid -g sudo

Output:

libuser-lid

Read Contents of the /etc/passwd in Linux

The /etc/passwd contains a list of all users on the system. We can pass each user in this file as a parameter to the groups command and list the group members we want with the grep command.

cat /etc/passwd | awk -F':' '{ print $1}' | xargs -n1 groups | grep sudo

Output:

etc passwd

Use the getent Command in Linux

The getent command gets entries from Name Service Switch libraries.

The group parameter lists the groups in the system when used with the group parameter to list users belonging to a particular group.

getent group sudo

Output:

getent

Use the groupmems Command in Linux

The groupmems command lists and alters the memberships of the groups. Use the -g flag to pass the group name as a parameter and the -l flag to list the results.

sudo groupmems -g sudo -l

Output:

groupmems

Use the id Command in Linux

The id command displays the groups a user is a member of. If used without parameters, the current user’s information is displayed.

Display the information of a different user. The user’s name must be given as a parameter.

id genel

Output:

id

Author: Yahya Irmak
Yahya Irmak avatar Yahya Irmak avatar

Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.

LinkedIn

Related Article - Linux User