How to check the groups a user belongs to in Linux?

Below are the methods through which we can check the groups a user belongs to in Linux Operating System:

Method 1: The “groups” command

To see the list of groups (both primary and secondary) to which a user belongs, we can use the groups command. The command is a part of ‘GNU coreutils’, hence no installation is required. One can open the terminal and start using it. It is distributed under the ‘GPLv3+software license.

Open the terminal and type in the following command to see if it working:

$ groups --version

This outputs the version information on the terminal.

Syntax:

$ sudo groups [<username>]

This means that the command can be used with or without providing a username. If the username is provided, it lists all the groups to which the specified username belongs to. If the username is not provided, it lists all the groups to which the active/current user belongs to.

Example 1: Using groups command with a username

$ groups liveuser

The following screenshot shows that “liveuser” is present in the “liveuser” & “wheel” groups –

Groups to which the user liveuser belongs.

Looking list of groups for some other users says “demoUser1” –

$ groups demoUser1

The following screenshot shows that “demoUser1” is present in “demoUser1“, “DemoGroup” & “DemoGroup2” groups:

Using groups command with username, example 2

Example 2: Using groups command without a username

$ groups

The following screenshot shows the output:

Using groups command without a username.

See that the output is the same as the output for “liveuser” as expected (because liveuser is the active user).

Method 2: The “id” command

This is our second alternative. The id command is created to retrieve the id details of a user. It comes pre-installed with Linux and can be used on the terminal right away. No installation is needed. We can leverage it to retrieve the groups a user belongs to using the following syntax:

Syntax:

id -G -n <username>

  • -G: The -G flag tells to retrieve all the group IDs of the user and
  • -n: The -n flag tells to output the names of groups (otherwise it would output group IDs).

For example, the following screenshot shows retrieving the groups of “demoUser1” using the id command:

id -G -n demoUser1

Using the id command to retrieve the groups a user belongs to.

Method 3: The “/etc/group” file

This is our third alternative. As stated earlier, the /etc/group file contains all the group information, hence, obviously, we can retrieve all the group-related information from it including checking the groups a user belongs to.

1. Entry format in /etc/group file

Each line contains the information for a separate group. Each line has the following format:

Group-Name:Password:Group-id:Usernames belonging to this group separated by comma or user-list

2. Manually searching /etc/group file

Use one of the following commands as per your convenience to output the contents of the/etc/group file on the screen:

1. less etc/group
2. more etc/group
3. cat etc/group

Now list all the groups where group-name is the same as the username (this is the primary group of the user) or the user list contains the username (these are the secondary groups of the user). Here is an example screenshot of the output of entries on the screen –

Contents of /etc/group file.

However, this process is tedious and very inefficient as the file is large. We address this in the next section.

Example 1: Using the grep command for searching

Even the grep command is pre-installed hence no installation is required. grep command is used for pattern matching in strings. We use it here to print only those lines from the /etc/group file where our concerned username appears, using this Syntax:

Syntax:

$ grep -w <username> /etc/group

The -w flag is used here to direct it to output only those lines that contain the username. Here is a sample screenshot where we obtain the groups “demoUser1” belongs to:

grep -w demoUser1 /etc/group

Using the grep command to conveniently retrieve group information from the/etc/group file.

Now just read the group names of all these lines to get the list of groups the username belongs to. For the above screenshot, it is [“demoUser1”, “DemoGroup”, “DemoGroup”].

Method 4: The “getent” command

This is our fourth alternative. The getent command is used to get the entries of many important files in a Linux system like password files, network files, etc. including /etc/group files. So we can write the following command to get the entries of the /etc/group file on the screen and then search manually as we did in the last section:

Syntax:

$ getent group

But we encounter the same problem – searching manually. And the solution is the same too – use the grep command! We just pipe out the output of getent command to the grep command directing grep to output only those lines where the concerned username appears. Here is the syntax:

Syntax:

$ getent group|grep -w <username>

Here is a screenshot using the above approach to get the groups to which “demoUser1” belongs to

getent group|grep -w demoUser1

Using intent and grep commands together to retrieve the groups to which the user belongs.

Now just read the group names from all the lines as done earlier to get the list of groups to which the username belongs.

How to Check the Groups a User Belongs to in Linux?

This article shows how to check the groups a user belongs to in Linux operating systems. We introduce some concepts related to the topic and then describe how to do so using the groups command available on the terminal.

Similar Reads

Groups in Linux

All Linux operating systems are designed as multi-user operating systems. This means that they provide the capabilities and related tools to create and handle multiple users within a system. One such tool is user groups. A user group is simply a collection of users. It is handy when a system administrator wants to deal with multiple users simultaneously (especially for handling permissions). We define a rule for the group and it automatically applies to all its member users....

How to check the groups a user belongs to in Linux?

Below are the methods through which we can check the groups a user belongs to in Linux Operating System:...

Frequently Asked Question

1. How do I check which groups a user belongs to in Linux?...

Conclusion

In Linux, determining the groups to which a user belongs is a straightforward process that can be accomplished using the groups command or the id -Gn command. These commands provide a quick way to retrieve a list of all the groups associated with a specific user. This information is crucial for system administrators and users alike as it helps in managing file and directory permissions, allowing or restricting access to resources, and ensuring the security and integrity of the system....