In Linux, a group is a collection of users. The main purpose of the groups is to define a set of privileges like read, write, or execute permission for a given resource that can be shared among the users within the group.
Users can be added to an existing group to utilize the privileges it grants.
A single user can be associated with several groups. In Linux, groups can be classified into two types that are primary and secondary groups. Primary groups are created during a user account creation while the secondary group can be one or more groups a user belongs to.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers configure group in Ubuntu Linux OS.
In this context, we shall look into adding a user to multiple groups, removing a user from a group, changing a user's primary group, creating, removing, and listing groups on Ubuntu 20.04 LTS.
Note that you will need sudo privileges or root access in order to add or remove a user from a group in Linux.
When a new user is created, it is automatically added to a group of the same name.
For instance, after creating a user named "linuxapt", the system will automatically create a new group named "linuxapt_Group". Then this new user "linuxapt" will be added to this new group named "linuxapt_Group".
If you want add a user to some other groups, the command is as follows:
$ sudo usermod -a -G <group_name> <user_name>
For instance, the below command will add an already existing user named "linuxapt" to a secondary group named "linuxapt_Group":
$ sudo usermod -a -G linuxapt_Group linuxapt
Enter the sudo password, after which a new user will be created and assigned a group.
If you want to add an existing user to multiple secondary groups, you can do so using a single command as follows:
$ sudo usermod -a -G <group_name1,group_name2,group_name3> <user_name>
For instance, the below command will add an existing user named "linuxapt" to three groups named "sudo", "linuxapt_Group", and "admin":
$ sudo usermod -a -G sudo,linuxapt_Group,admin linuxapt
If you need to create a new user and add it in a secondary group, here is the command to do so:
$ sudo useradd -G <group_name> <user_name>
For instance, the below command will create a new user named "linuxapt" and add it to the already existing group named "linuxapt_Group":
$ sudo useradd -G linuxapt_Group linuxapt
Enter the sudo password, after which a new user will be created and assigned a group.
To change the primary group of a user, the command is as follows:
$ sudo usermod -g <group_name> <user_name>
For instance, to change a user's named "linuxapt" primary group to "linuxapt_Group", the command would be:
$ sudo usermod -g linuxapt_Group linuxapt
After adding a user to a specific group, you can confirm it as follows:
$ groups <user_name>
It will show you the groups to which the user specified as <user_name> is added to.
For instance, the below command shows that a user "linuxapt" is added to two groups "linuxapt_Group1" and "linuxapt_Group2".
For removing a user from a specific group, the syntax is as follows:
$ sudo gpasswd -d <user_name> <group_name>
For removing a user named "linuxapt" from the group named "linuxapt_Group", command would be:
$ sudo gpasswd -d linuxapt linuxapt_Group
If you need to create a new group in Linux, you can do so by using the below command in Terminal:
$ sudo groupadd group_name
Make sure to replace group_name with the group name you want to create.
If you need to remove a group from the Linux system, the syntax is as follows:
$ sudo groupdel <group_name>
For instance, to remove a group named "linuxapt_Group", the command would be:
$ sudo groupdel linuxapt_Group
If you want to view all the groups on the Linux system, the command is as follows:
$ getent group
This command will list all the groups on your system along with the user accounts which are member of these groups.
For instance, the following output shows the list of groups along with their members like you can see users "linuxapt1" and "linuxapt2" are members of group "linuxapt_Group".
This article covers how to add a user to a group on Ubuntu Linux System. If you want to learn about adding or removing a user on Linux OS, you can follow our guide on How to Add and Remove Users on Linux .
User accounts can be assigned to one or more groups on Linux. You can configure file permissions and other privileges by group. For example, on Ubuntu, only users in the sudo group can use the sudo command to gain elevated permissions.
To Add a New Group on Ubuntu Linux:
If you want to create a new group on your system, use the groupadd command following command, replacing new_group with the name of the group you want to create:
$ sudo groupadd mynewgroup
To Add an Existing User Account to a Group:
To add an existing user account to a group on your system, use the usermod command, replacing examplegroup with the name of the group you want to add the user to andexampleusername with the name of the user you want to add:
$ usermod -a -G examplegroup exampleusername