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. In this post, we will show how to find out which groups a user belongs to in Ubuntu.
Note: The commands mentioned here have been tested on Ubuntu 20.04 LTS. These commands also applies for other Linux distributions.
How to Find Which Groups a Linux User Belongs To
First, 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:
It will show the groups to which the currently logged-in user belongs to. For example, the following output is showing the name of the groups to which the currently logged-in user “kbuzdar” is a member of.
To find groups information for a specific user other than the current logged in user, type groups followed by that username:
$ groups <user_name>
For example, to find which groups a user named “test” belongs to, the command would be:
$ groups test
Alternatively, you can also use the id command to find out which groups a current logged in user belongs to:
It will show all the groups along with their group IDs to which the currently logged-in user belongs to.
To find groups information for a user other than the current logged in user, type id followed by the username:
$ id <user_name>
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.
Likewise listing all groups, you can also list all users on your system.
List All Members of a Group
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
The following output shows the “sudo” group has two members: “kbuzdar” and “ummara”.
Adding User in a Group
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 “ummara” to the “sudo” group, the command would be:
$ sudo usermod -aG sudo ummara
Removing User from a Group
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 “ummara” from the “sudo” group, the command would be:
$ sudo gpasswd -d ummara sudo
By following the procedure shared in this post, you can easily find out which groups a user belongs to in Ubuntu. We have also 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 post on how to add and remove users on Ubuntu.