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

Here's a quick guide to adding users to groups, all from the command line on the Linux platform.

Add User to Group in Linux

In this article we will show you how to add a user to a group in Linux system. We will also learn how to view the groups and group ID of any user account.

Linux user and group is an essential part of Linux security. As you know, Linux is a multi-user operating system. Adding users to group is a task worth learning. Groups allow certain 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 simply 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.

Prymary Group

A primary group is created when a user is added. It is the group that applied to user when login. 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 specific 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 Secondary Group

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

usermod [OPTIONS] GROUPNAME USERNAME

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. 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 group. 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 (,). 

So, to add a user ‘john‘ to groups ‘audo‘, ‘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 were able to successfully add a user to a group in Linux, 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)

From the output above, we see that the primary group of the user ‘john‘ is ‘john‘ and it 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 usermod command in Linux, consult its manual page. And of course feel free to leave a comment if you have any questions.

Leave a Reply

Your email address will not be published.

Latest from Linux Knowledge