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

Configurare noua (How To)

Situatie

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.

Solutie

Pasi de urmat

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

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>

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

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

Tip solutie

Permanent

Voteaza

(5 din 9 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?