how to list members of an user group in linux

In Linux, as with most operating system a user is someone who can login and use the computer. Having separate accounts for each of the individual users allows you to manage the users better in terms of security. Users and Groups are concepts in Linux (and other OS-es) that are primarily used for permission-ing and to control access to files, directories and peripherals.

In order to make the management of security better, the users can be grouped into “groups” called groups or user groups. User Groups are thus a collection of users or user accounts that share the same permissions and security profile while accessing the system. As everything in Linux is a file, you can assign different permissions each and every resource with fine granularity by using the user, group and global levels.

How to List all Groups

In order to list all the groups that are currently active in the system, you can use the cat command to print out the /etc/group file. This system file lists all the groups that are available or have been created.

bash$ cat /etc/group

You can also use the getent command to list the contents of the group database. The getent may not be available depending on your distro, in which case check your distro documentation on how to install it.

bash$ getent group

The first field in the output is the name of the group. If you just want the name of the group then you can pipe the output to cut command to print just the first field.

bash$ getent group | cut -d: -f1

On a side note, if you just want to get a list of groups that a particular user is a member of, then you could just use the group command. It takes an argument which is the username. If no argument is given then the groups for the current user is printed out.

bash$ groups <username>

How to List Members of a Group

Sometimes, you want to see a list of all users who are part of a particular group. Unfortunately there is no straight forward to do this in most distros. There are some utilities such as members and groupmems that might be available to you, depending on the distro. You can also check your distro repository to see if these packages are available.

We will see the options you have if these utilities are not available to you. You should be able to use these right from the command line and should work with almost all Linux distros.

If you know the name of the group then there are a couple different ways you can extract the name of users who are members of the group. This is somewhat similar to the commands that we discussed in the previous section, but differ in the fields or values that are extracted.

bash$ getent group <groupname> | cut -d: -f4

or

bash$ grep "^<groupname>" /etc/group | cut -d: -f4

Either of the above command will print out a comma separated list of users who are part of the group which is specified. As you might already know, substitute the actual name of the group (eg. wheel or audio) in the above example where it says “<groupname>”.

You can also just list all the users across all user groups if you are just interested in the users. Users can be a member of multiple groups and they inherit the security profile of each of the group that they are a member of. Once you have the user groups defined, you can then add or delete users from these user groups to provide them with different set of permissions.