Add User to Group in Linux, How to Do It (with Examples)

This article will show you how to add a user to a group in a Linux system. We will also learn to view user account's groups and group ID.

Linux user and group is an essential part of Linux security. As you know, Linux is a multi-user operating system. Therefore, adding users to a group is a task worth learning.

Groups allow specific user permissions to be available for group members.

The primary purpose of groups is to define a set of privileges such as reading, writing, or executing permission for a given resource that can be shared among the users within the group.

Instead of managing permissions for each user account, you can add a user to a group in Linux to grant the appropriate permissions.

Related: Remove User in Linux Using the Command Line

Types of Groups in Linux

There are two types of groups in Linux – Primary and Secondary.

Primary Group

A primary group is created when a user is added. It is the group that applies to the user when login in. Any files the user creates are automatically added to that group. A user can only belong to one primary group at a time.

Secondary (Supplementary) Group

A user can belong to any number of secondary groups. A secondary group is any group(s) a user is a member of other than the primary group.

The main reason to create a secondary group is to allow the specific permission to limited users. For example, if you add a particular user to the sudo group in Linux, the user will inherit the groupโ€™s access rights and be able to run sudo commands.

How to Add User to Group in Linux

Only root or users with sudo access can add a user to a group.

Add an Existing User to the Secondary Group

Adding users to a group in Linux is a straightforward process. To add an existing user account to a group, use the usermod command. The syntax is:

usermod [OPTIONS] GROUPNAME USERNAMECode language: CSS (css)

For example, to add the user john to the sudo group, you would run the following command:

sudo usermod -a -G sudo john

Keep in mind that there will be no output once you execute the command.

Letโ€™s break down this syntax. First, the usermod command uses the -a (append) and -G (group) options to append the user to a particular group.

Add an Existing User to Multiple Groups

As you already know, in Linux, a user only can be added to one primary group, but it can be added to any number of the secondary groups.

So, if you want to add a user to multiple groups, you can use the same command as above, but you should separate the group names to which you want to add the user with a comma (,). 

To add a user john to groups audio, video, and network, you would run the following command:

sudo usermod -a -G audio,video,network john

Change a Userโ€™s Primary Group

The usermod command will allow you to change a user’s primary group. The -g option controls the primary group. When you use a lowercase g, you assign a primary group.

For example, to assign a primary group web to a user, john:

sudo usermod -g web john

How to Check a Userโ€™s Group

To check whether you could add a user to a group in Linux successfully, you can use the id command. It gives you the ability to see all the groups to which a user has access.

For example, to obtain information about a user, john, pass the username as an argument to the id command:

id john
uid=1000(john) gid=1000(john) groups=1000(john),970(docker),986(video),993(input),998(wheel)

The output above shows that the user johnโ€™s primary group is john and belongs to docker, video, input, and wheel supplementary groups.

Another method to display the groups a user belongs to is to use the groups command. Pass the username as an argument to the command:

groups john
wheel input video docker john

If you omit the username, the command will print the information about the currently logged-in user.

Conclusion

Linux groups are organization units that are used to organize and administer user accounts in Linux. Here we have seen how to use the usermod command to add users to groups in Linux.

For more about the usermod command in Linux, consult its manual page. And, of course, feel free to leave a comment if you have any questions.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%