When you create a user in Linux, a group with the same name as the user name is created automatically. This group is assigned as a primary group to the user. A user can be a member of other groups as well, which are known as secondary groups.
In this tutorial, we learn how to add a user to a group in Linux.
Understanding Linux Groups
In the Linux operating system, a group is a collection of users. Linux users can be members of one or more groups. Linux group helps to set privileges such as read, write and execute permission for files and directories that can be shared with users in the group.
When a user is added to a group, all the privileges of that group are automatically applied to the user. For example, all members of the sudo group in Ubuntu get elevated permissions to run root commands.
Linux has two types of user groups: primary and secondary group.
Primary Group
A Primary group is a group created automatically when a user is created, with the same name of the user. The created user will be the only member of this group. So every Linux user has at least one primary group.
Files owned by a user’s primary group cannot be accessed by other users. In case required you can change the primary group of a user using the usermod command. But very rarely we required to change the primary group in Linux.
You can find the user's primary group information in /etc/passwd file. You may use less, more, or cat command to read the contents of the file.
Secondary Group
It is a supplementary group. Users can be members of multiple secondary groups in Linux. Secondary groups are useful when we need to grant certain privileges to the group members.
You can list secondary group information in the /etc/group file.
Add User to Group
You can add user to a group either while creating a new user or an existing user. You require sudo permission to run these commands because superuser access is required to change user account settings. So for adding a user to a group, you need sudo command or root access.
Add an Existing User to a Secondary Group
You can add an existing user account to a group using the usermod command. Use groupadd command to create a new group.
The following example to adds an existing user named bob
to the group named adm
:
sudo usermod -aG adm bob
How to Add a User to the docker Group
You can read a detailed understanding of how to add a User to the docker group in our other article.
Add a New User to a Secondary Group
You can add a user using the useradd command. While creating the user itself you can add the user to the group.
The following command creates a new user named devuser
and add to the developers
group:
sudo useradd -G developers devuser
When you add a user to existing groups, make sure the groups exist before running the command.
Add Multiple Users to a Group
Use the gpasswd -M command to add multiple users to a particular group in a single command:
sudo gpasswd -M bob,tom adm
This example shows that we added users bob
and tom
to the adm
group. We also verified the group membership of both the users using the id command.
Add a User to Multiple Groups
To add a user to multiple groups, you can use the same usermod command, add group names separated by comma followed by the username. You can add a user to multiple groups using a single command.
In the following example, we add the existing user named tom
to sambashare
and lpadmin
group:
sudo usermod -aG sambashare,lpadmin tom
List Groups
To confirm a user account is successfully added to a group, you can use any of the following commands:
Id Command
The id command shows the UID, group ID, and group memberships of a user. The following command shows the group membership of the user named bob
:
id bob
Groups Command
Use groups command to list all the groups to which the currently logged in user belongs:
groups
To list group information of a specific user, type:
groups tom
/etc/group
The /etc/group file contains information about all the groups on your Linux system. You can check this file to verify the group membership of a user:
cat /etc/group | grep bob
Delete an Existing Group
To delete an existing group from your Linux system, use the groupdel command followed by the group name:
sudo groupdel developers
When you delete a group, its members are not deleted. Only the access privileges provided by the deleted group are revoked.
Conclusion
In this tutorial, we learned how to add a user to a group in Linux. The commands we used should work on all Linux distributions, including Ubuntu, Debian, Fedora, CentOS, and Red Hat.
Comments