Linux Add User to Group: Step-by-step Guide 2021

In this tutorial, we will learn how to add a user to a group. First, let’s see what is group and its types.

What is a group in linux?

A group is a collection of users. This is helpful specially while assigning the permissions to the users. We can set the permissions at the group level rather than assigning them to each user individually.

Types of groups:

  • Primary Group:

By default, each user is a member of one primary group. The name of the primary group is the same as the username. This type of groups is created automatically when you create a user. E.g. If you create a user “linux”, its primary group will be “linux”.

In the below gif, the highlighted text shows the primary group of the newly created user “linux”

Linux Add User to Group

The user can’t be added in more than one primary group. Also, we can’t remove the user from the primary group.

  • Secondary Group:

These groups are helpful when you want to assign permissions to some resource-like file to a set of users. Like the primary group, it is not compulsory to add users to secondary groups. These groups can be created manually when needed using the command “groupadd” (please refer to the gif below).

groupadd groupname

Linux Add User to Group 1

We can add one user to a maximum of 16 secondary groups.

How to add a user in a secondary group?:

In the last section, we saw that a user can’t be added in more than one primary group, but we can add it in multiple secondary groups. We can do that using a command called “usermod”.

This is how we can use this command:

usermod -a -G list_of_secondary_groups username

Options used in this command are:

-a (Append): To append the user to the secondary GROUPS mentioned by the -G option without removing the user from other groups
-G (Groups): Accept the list of secondary groups
username: Username to be added to the mentioned secondary groups
list_of_secondary_groups: List of the secondary groups to which the user with “username” is to be added. You need to provide minimum one group.

Now, how to check the list of groups to which the user belongs? We have “groups” or “id” commands for that.
You can use these commands like this:

groups username or id username

Below image shows the difference between these two:

Linux Add User to Group 2

Let’s see how this works through a gif illustration. As you know, we have already created a user called “linux” and a secondary group “secondary”. Now, we’ll add the user “linux” to the group “secondary”.

Linux Add User to Group

How to change the user’s primary group?:

As you know, one user can be a member of only one primary group. We can’t add it to multiple primary groups, but we can change the user’s primary group. Here is a command for that:

usermod -g groupname username

Below gif shows how we can change the primary group of the user “linux”. We’ll create one secondary group called “newgroup”, and then change the primary group of “linux” to “newgroup”.

Linux Add User to Group 3

How to remove a user from a secondary group?:

To remove user from the secondary group, please use “gpasswd” command like this:

gpasswd -d username groupname

We’ll remove user “linux” from the secondary group “secondary”. Let’s see how through below gif:

Linux Add User to Group 4

Please note that we can’t remove the user from primary group.

How to delete a secondary group?

Just like “groupadd” command, we have “groupdel” command to delete a secondary group. You can use it like this:

groupel groupname

Here’s how we can delete the group “secondary”:

Linux Add User to Group 6

Note: Before deleting the group, you will need to remove users from the group; otherwise you may encounter an error while deleting the group.