Read in 4 minutes

last updated 

How to List Groups in Linux

In Linux, a group is a collection of users. The main purpose of the groups is to define a set of privileges like read, write, or execute permission for a given resource that can be shared among the users within the group. Users can be added to an existing group to utilize the privileges it grants.

This tutorial explains how to show all groups a user is a member of. We will also explain how to list all members of a group.

Linux Groups

There are two types of groups that a user can belong to:

  • Primary or login group – is the group that is assigned to the files that are created by the user. Usually, the name of the primary group is the same as the name of the user. Each user must belong to exactly one primary group.

  • Secondary or supplementary group - used to grant certain privileges to a set of users. A user can be a member of zero or more secondary groups.

List all Groups a User is a Member of

There are multiple ways to find out the groups a user belongs to.

The primary user’s group is stored in the /etc/passwd file and the supplementary groups, if any, are listed in the /etc/group file.

One way to find the user’s groups is to list the contents of those files using cat, less or grep. Another easier option is to use a command whose purpose is to provide information about the system’s users and groups.

Using the groups command

The most memorable command to list all groups a user is a member of is the groups command. When executed without an argument the command will print a list of all groups the currently logged in user belongs to:

groups

The first group is the primary group.

john adm cdrom sudo dip plugdev lpadmin sambashare

To get a list of all groups a specific user belongs to, provide the username to the groups command as an argument:

groups linuxize

Same as before the first group is the primary group.

linuxize : linuxize sudo

Using the id command

The id command prints information about the specified user and its groups. If the username is omitted it shows information for the current user.

For example to get information about the user linuxize you would type:

id linuxize

The command will show the username (uid), the user’s primary group (gid), and the user’s secondary groups (groups)

uid=1001(linuxize) gid=1001(linuxize) groups=1001(linuxize),27(sudo)

To print only the names without the user and group IDs use the -n option. Option -g will print only the primary group and -G all groups.

The following command will print the names of the groups the current user is a member of:

id -nG
john adm cdrom sudo dip plugdev lpadmin sambashare

List All Members of a Group

To list all members of a group, use the getent group command followed by the group name.

For example, to find out the members of a group with the name developers you would use the following command:

getent group developers

If the group exists the command will print the group and all its members:

developers:x:126:frank,mary

If there is no output that means the group doesn’t exist.

List All Groups

To view all groups present on the system simply open the /etc/group file. Each line in this file represents information for one group.

less /etc/group

Another option is to use the getent command which displays entries from databases configured in /etc/nsswitch.conf file including the group database which we can use to query a list of all groups.

To get a list of all groups, type the following command:

getent group

The output is the same as when displaying the content of the /etc/group file. If you are using LDAP for user authentication the getent will display all groups from both /etc/group file and LDAP database.

You can also use awk or cut to print only the first field containing the name of the group:

getent group | awk -F: '{ print $1}'
getent group | cut -d: -f1

Conclusion

In this tutorial, you learned how to find the groups a user is a member of. The same commands apply for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian and Linux Mint.

Feel free to leave a comment if you have any questions.