Several users in a group can have the same or different set of privileges. The users can be added or removed by the root user. In this tutorial, the different scenarios of how to add a user to the group will be explained.
First, discuss let’s discuss the types of groups in Linux. The groups are classified into two categories as discussed:
- Primary Group
This group is created on installation of the operating system, which has a unique group ID and user ID.
- Secondary Group
The user creates the secondary group, which can set the privileges accordingly and add/remove the users.
The user root directory has a /etc/group file, which contains information about all the user groups.
The tutorial discusses the below topics:
- How to Add a User to Group in Linux
- Creating a Group in Linux
- Adding an Existing User to a Group
- Adding a User to the Sudo Group
- Adding User to Multiple Groups
- Adding Multiple Users to a Group
- Creating a New User and adding to Group/Groups
- Changing the Primary Group for Users
- Removing a User from a Group
- How to Delete a Group
- Error Cannot Add User to Group
A user is a member of several groups in the system, and every user is a member of one primary group and other secondary groups. We can add the user to a group in several ways; let’s start with creating a group in Linux.
The “groupadd” command is utilized to create a group in Linux. The syntax for creating a new group is written below:
$ sudo groupadd GroupName
- a => Represents to add/append a user to the group
- G => The G option shows a group command.
Let’s create a group named “testgroup” by running the below command in the terminal:
$ sudo groupadd TestGroup
The group “TestGroup” is added to the system. To verify the group has been added to the system, use the below command in the terminal:
$ getent group TestGroup
The output shows the group name “TestGroup” with group ID “1002”, which verifies the group has been successfully added to the system. The output will be blank in case of the group does not exist.
Note: For adding a new user, execute the “sudo useradd UserName” command.
Adding an already created user to the Linux “usermod” command is utilized. The general syntax of the command to add a user to the group is shown below:
$ sudo usermod -a -G GroupName UserName
For adding an existing user, “ubuntu” to a group “TestGroup”, run the below command in the terminal:
$ sudo usermod -a -G TestGroup ubuntu
To check the users in a group “TestGroup”, use the below command:
$ grep TestGroup /etc/group
The output shows the user “ubuntu” is successfully added to the group “TestGroup”.
If you want to check all the groups in the system, use the following command:
$ getent group
The sudo user has root privileges (Access to the root files) and can add or remove a user from the group. The general syntax of the command to add a user to the sudo group is given below:
$ sudo usermod -a -G sudo GroupName
To add a user “ubuntu” to the sudo group, utilize the below command:
$ sudo usermod -a -G sudo ubuntu
The user “ubuntu” is added to the sudo group of the system.
Note: To check the sudo group of a user, use the “groups UserName” command; the first group in the list will be its sudo group. For checking the sudo group of the user “ubuntu”, use the following command:
$ groups ubuntu
The first group, “ubuntu” in the above output list is the sudo group of the user.
We can add a single user to several groups simultaneously using a single command using the comma “,” separator within the groups as mentioned below:
$ usermod -a -G Group1,Group2,Group3 UserName
To add a user, “Johnson”, to groups “TestGroup”, “CollegeGroup”, and “SecondClass”, the following command will be used:
$ usermod -a -G TestGroup,CollegeGroup,SecondClass Johnson
Sometimes we require to add multiple users to a group. We can add multiple users to a group in Linux at once using a single command written below:
$ gpasswd -M User1, User2, User3 GroupName
To add two users, “ubuntu” and “Johnson” to a collaborative group, “TestGroup”, the following command will be utilized:
$ gpasswd -M Johnson,ubuntu2 TestGroup
In the previous methods, we added existing users to the groups, but we can also create a new user and add to a group or several groups with the help of a single command. The “G” option of the useradd command allows us to create a new user if it does not exist and add it to the group as shown below:
$ sudo useradd -G GroupName UserName
To create a new user, “Maxwell” and directly add to the group “TestGroup”, run the below command in the terminal:
Note: You must have sudo permissions to create a new user.
$ sudo useradd -G TestGroup Maxwell
Every user is a member of a primary group, which in most cases, is the same as the user’s name. For changing the primary group naming “TestGroup” for user “Maxwell”, use this command:
$ usermod -g GroupName UserName
To change the primary group for user “Maxwell” to the group “TestGroup”, you can use the following command:
$ sudo usermod -g TestGroup Maxwell
For deleting a user from a group, the “gpasswd” command is used as shown:
$ sudo gpasswd -d UserName GroupName
To remove a user “Maxwell” from the group “TestGroup”, run the following command in the terminal:
$ sudo gpasswd -d Maxwell TestGroup
If you want to delete a group from the system in Linux, in that case, the “groupdel” command is used:
$ sudo groupdel GroupName
For removing the group “TestGroup” from the system, execute the below command:
$ sudo groupdel TestGroup
Sometimes while adding a user to a particular group, you encounter the error “Cannot Add User to Group”, which can be resolved by restarting the system.
To add a user to a group in Linux, utilize the “sudo usermod -a -G GroupName UserName” commands in the terminal. We can add a user to several groups using the “usermod -a -G Group1,Group2,Group3 UserName” command and add multiple users to the group execute the “gpasswd -M User1,User2,User3 GroupName” command.