How to List Users in a Group in Linux
- List Users in a Group in Linux
-
Read Contents of the
/etc/group
in Linux -
Use the
groups
Command in Linux -
Use the
lid
Command in Linux -
Read Contents of the
/etc/passwd
in Linux -
Use the
getent
Command in Linux -
Use the
groupmems
Command in Linux -
Use the
id
Command 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:
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:
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:
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:
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:
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:
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:
Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.
LinkedIn