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 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:
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:
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