How to list all users in a Linux group?
In order to be able to understand the command to list all the users that are present in a Linux group, you first must be able to print all the users present.
For that there are many different possible ways, I’ll make use of the compgen command. The compgen command is a Linux utility command that is used to list all the commands that can be executed in a Linux terminal, and when used with a -u flag we can simply print all the users present on Linux.
Consider the command shown below as reference −
compgen -u
Output
root daemon bin sys sync games man lp mail news uucp proxy www-data backup . . .
Now in order to print all the users in a Linux group we have two options available to us. We can either make use of the getent command or the members command.
Let’s first talk about the getent command which is used to get the entries from a database file and in our case we can use it with another command, i.e., group to simply print the users that belong to a group on Linux.
Let’s say we want to print all the users that belong to the sudo group, then in that case we will need to write the following command to the terminal −
getent group sudo
Output
sudo:x:17:immukul
The last entry in the above output is the name of the user that belongs to the sudo group.
Now let’s talk about the members’ command. The members command in Linux is a utility command that is used to print all the group members.
It should be noted that the members command is usually not installed in all the operating systems and you may need to install it on your own and the way to do that is just to type the command shown below on your Linux’s terminal −
sudo apt install members
Then the command to print the members that belong to a particular group is shown below −
members sudo
Output
immukul