Find out Which Groups a User Belongs to in Ubuntu 20.04 - How to do it ?

A group is a collection of user accounts that have the same set of permission and access rights on the files, folders, and configurations. When a new user is created in Linux OS, it is automatically added to a group with the same name as the user. A single user can be a part of single or multiple groups.

Sometimes, we need to find out which groups a user belongs to. It helps to understand what type of permissions users have and troubleshoot when things are not working properly. 

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Ubuntu Server related tasks.

In this context, we shall look into how to find out which groups a user belongs to in Ubuntu.


How to Find Which Groups a Linux User Belongs To on Ubuntu Systems?

To begin, open the Terminal by pressing the Ctrl+Alt+T shortcut. 

Then in order to check the groups the current user account belongs to, type groups in the Terminal and press Enter:

$ groups

This command will show the groups to which the currently logged-in user belongs to.

To find groups information for a specific user other than the current logged in user, type groups followed by that username as shown below:

$ groups <user_name>

For example, to find which groups a user named "linuxapt" belongs to, the command would be:

$ groups linuxapt

Alternatively, you can also use the id command to find out which groups a current logged in user belongs to:

$ id

It will show all the groups along with their group IDs to which the currently logged-in user belongs to.

Also, To find groups information for a user other than the current logged in user, type id followed by the username:

$ id <user_name>


How to List All Groups in Linux ?

If you want to list all groups that exist in your system, issue the following command in Terminal:

$ cat /etc/group

This command will list all the groups that exist in your system.

Also, listing all groups, you can also list all users on your system.


How to List All Members of a Group on Ubuntu ?

If you want to list all members of a group, issue the following command in the Terminal:

$ getent group <group_name>

This command will print the group name followed by all its members. 

For example, to print all the members of the "sudo" group, the command would be:

$ getent group sudo


How to add User in a Group on Ubuntu ?

If you want to add a user to a group, use the following command syntax:

$ sudo usermod -aG <group_name> <user_name>

For instance, to add an already existing user "linuxapt" to the "sudo" group, the command would be:

$ sudo usermod -aG sudo linuxapt


How to remove User from a Group on Ubuntu ?

To remove a user from a group, use the following command syntax:

$ sudo gpasswd -d <user_name> <group_name>

For instance, to remove the user "linuxapt" from the "sudo" group, the command would be:

$ sudo gpasswd -d linuxapt sudo


[Need urgent assistance in fixing missing packages on Ubuntu Linux Systems? We are available to help you today. ]

This article covers how you can easily find out which groups a user belongs to in Ubuntu. Also we shared how to list all groups in a system, list members of a group, add and remove a user from the group. For information about adding/removing users in a system, visit our guide on how to add and remove users on Ubuntu .

Adding a user to an existing group is one of the typical tasks of a Linux administrator.

A group is a collection of users. 

The main purpose of the group is to define a set of privileges to their members within the group.


To find out if a user has sudo access is by checking if the said user is member of the sudo group. 

If you see the group 'sudo' in the output, the user is a member of the sudo group and it should have sudo access.

In order to list groups on Linux, you have to execute the "cat" command on the "/etc/group" file. 

When executing this command, you will be presented with the list of groups available on your system.


To login as Sudo on Ubuntu:

1. Open a terminal Window. Press Ctrl + Alt + T to open the terminal on Ubuntu.

2. To become root user type: sudo -i. sudo -s.

3. When promoted provide your password.

4. After successful login, the $ prompt would change to # to indicate that you logged in as root user on Ubuntu.


What is /etc/passwd file?

"/etc/passwd" is a text file containing every user information that is required to login to the Linux system. It maintains useful information about users such as username, password, user ID, group ID, user information, home directory and shell.


To find out which groups a user belongs to in Linux:

1. groups: Show All Members of a Group.

2. id: Print user and group information for the specified username.

3. lid or libuser-lid: It display user's groups or group's users.

4. getent: Get entries from Name Service Switch libraries.

5. compgen: compgen is bash built-in command and it will show all available commands for the user.

6. members: List members of a group.

7. /etc/group file: Also, we can grep the corresponding user’s groups from the /etc/group file.


What is Wheel Group in Linux?

The wheel group is a special user group used on some Unix systems, mostly BSD systems, to control access to the su or sudo command, which allows a user to masquerade as another user (usually the super user). Debian-like operating systems create a group called sudo with purpose similar to that of a wheel group.


Types of groups in Linux:

1. Primary Group - The primary group is the main group associated with the user account. Each user must be a member of a single primary group.

2. Secondary Group - The secondary or supplementary group is used to grant additional rights to the user. Each user can become a member of multiple secondary groups.

Related Posts