In Linux operating systems, groups allow collections of users permissions to specified resources.
For someone to access the resources on a system, one must add a user to the corresponding group in Linux.
Adding users to group is a task worth learning. It can help you manage your system better as well as provide security by being able to control which users can read, alter, and execute commands.
Check out this simple tutorial on how to add users to group.
Linux Users and Groups
The Linux operating system allows for multiple users to use the system at one time. Users, logging in with their username, and the group(s) they belong to, determine access levels and permissions.
Files and directories are managed by a user and a group. Only the root user with sudo access can determine the extent of the permissions.
The root user is the only user who can add users to a group.
Groups in Linux are a collection of users. There are two categories: primary and secondary.
A primary group is created when a user is added. This information is stored in /etc/group and an additional entry for the user, can often be found in /etc/passwd.
You must become part of a secondary group in order to inherit access to a file made by another user.
Every user can belong to exactly one primary group, and to as many secondary groups as necessary.
How to log in as Root User in Linux
“Add user to a group” is an administrative function only available for root users. In other words, you have to become one to perform this function.
Use the special command, su which stands for “super-user” or “switch user” to login. Enter the password if you have it once prompted:
You can also use sudo command, which stands for “super-user do”:
Adding an Existing User to a Linux Group
Now that you’ve successfully logged in as a root user account, you can now add an existing user to the group using the usermod command:
sudo usermod -a -G group_to_add username
-a flag – prompts the usermod command to add a user to a group. If you fail to use -a, you cannot add a user to the groups listed after -G successfully.
-G flag – indicates which secondary group you want the new user to belong.
-g flag – used to change a user’s primary group.
Let’s say you want to add existing user “newuser” to the group “mail”. You can do so with the following usermod command:
sudo usermod -aG mail newuser
There will be no output once you execute the command. You will however notice a change in privileges once you access the files associated to the Linux groups.
In any case, if the group name does not exist, it will display a prompt stating the same.
If you wish to add a user on multiple groups, “mail and sysadmin”, separate the group names with a comma (,) as follows:
sudo usermod -aG mail,sysadmin newuser
Creating a New User in Linux, & Add User to Group in One Command
You might encounter cases where you want to create a new user and right then and there, make them part of a group.
You can easily do this with the useradd command. The following example will add the user, along with their primary group (same as the user name), and then also add them to a secondary group.
sudo useradd -G secondary_group username
For example, you want to make a new user, “rick”, and add him to the primary group “developers”, and secondary group “mail”. This is the command you would type in:
sudo useradd -g developers -G mail rick
To add a new user on multiple Linux groups, separate the user group names with a comma as follows:
sudo useradd -g developers -G mail,demo,news rick
How to Check a User’s Groups
To check whether or not you were able to successfully add a user to a group in Linux, you can do so with the user id command:
Let’s say for example we want to check the username “rick” in groups:
It should display an output of:
uid=501(rick) gid=20(developers) groups=20(developers),30(mail),67(news),...
This information states that rick’s primary group is developers. It should display the user’s long list of secondary groups, along with the primary after “groups”.
If you just want the user’s supplementary or secondary groups to be displayed, use the groups command:
groups rick Output: rick : developers mail news admin demo
If you don’t specify a username, the command will execute for the current logged in user’s groups data.
How to Remove a User From a Group
If you wish to remove a user from a group, simply use the gpasswd command along with the -d option, which means you want to delete them. The command should look like this:
sudo gpasswd -d username groupname
For example, if you want to delete “rick” from the group “developers”:
sudo gpasswd -d rick developers
Again, there will be no output displayed.
How to Create a New Group
If you want to make a new group, use the groupadd command and your assigned groupname:
sudo groupadd groupname
For example, to make a new group called “content”, you may do so with the following command:
sudo groupadd content
How to Delete a Group
To delete an existing group, use the groupdel command followed by the group name you wish to delete:
sudo groupdel groupname
For example, if you want to delete the group, “developers”, you may do so with the following command:
sudo groupdel developers
No outputs will be displayed for creating new groups and deleting groups once the command is executed.
How to Change a User’s Primary Group
To change a user primary group, enter the following on the command line:
sudo usermod -g groupname username
For this example, we changed the primary group of the user rick to developers:
sudo usermod -g developers rick
Learning how to add a user to a group is not at all difficult. It just takes some practice and experience.
The idea of groups makes it easy to manage users in Linux.
Do note that these commands may be applied to any Linux distributions which include Ubuntu, CentOS, RHEL, Debian, and Linux Mint.
We hope this tutorial on how to add a user to a group, and other useful user and group commands has been of great help. If you have any questions, don’t hesitate to leave us a comment below.