This article will show you how to add a user to a group in a Linux system. We will also learn to view any 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.
Types of Groups in Linux
There are two types of groups in Linux – Primary and Secondary.
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
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 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. 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
network, you would run the following command:
sudo usermod -a -G audio,video,network john
Change a User’s Primary Group
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,
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
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
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:
wheel input video docker john
If you omit the username, the command will print the information about the currently logged-in user.
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.