Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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.

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.

Syntax:

$ sudo useradd <username>

When a new user is created, a new group with the same name is created and the user is added to it. This group is called the primary group of the user. For example, we create a user named “demoUser3” in the following screenshot from the terminal:

sudo groupadd demo_group
sudo useradd demo_user
sudo groupmod -a -U demo_user demo_group
groups demo_user

Creating a group, a user, and adding the user to the group.

Creating a group, a user, and adding the user to the group.

We use the groups command (which we will explain in detail in the next section) to see the groups of the user and notice that it is in a group named “demoUser3” as expected. We can add the user in other groups using the command –

$ sudo groupmod -a -U <username> <group-name>

Where do we want to add the specified username to the specified group-name? When the user is added to a group in this way, it is called a secondary group of the user. In other words, all the groups to which the user belongs except his/her primary group are called his/her secondary groups.

A user can be present in one primary group and as many secondary groups as needed. All the group-related information is present in the/etc/group file.

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 user liveuser belongs.

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

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 username.

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.

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.

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.

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.

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.

Frequently Asked Question

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

To check which groups a user belongs to in Linux, you can use the groups command followed by the username.

For example:

groups username

This command will display a list of groups to which the specified user belongs.

2. What is the command to list all groups a user is a member of in Linux?

The command to list all groups a user is a member of in Linux is:

id -Gn username

The id command with the -Gn option provides a space-separated list of group names for the specified user.

3. Can I check group memberships for multiple users simultaneously in Linux?

Yes, you can check group memberships for multiple users simultaneously by providing multiple usernames to the groups or id command.

For example:

groups username1 username2

This command will display the groups for both `username1` and `username2`.

4. How to find out the primary group of a user in Linux?

To find out the primary group of a user in Linux, you can use the `id` command with the `-gn` option.

For example:

id -gn username

This will output the primary group name for the specified user.

5. Are there any graphical tools or applications for checking user group information in Linux?

Yes, there are graphical tools available for checking user group information in Linux. One such tool is “Users and Groups” or “User Accounts” in the system settings of desktop environments like GNOME or KDE. Alternatively, tools like `gpasswd` provide a graphical interface for managing user groups.

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.



Last Updated : 05 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads