In Linux, When we create a user it automatically gets added to the primary group with the same name as the user name. However, sometimes we may need to add users in some different groups for providing some kind of accessibility. So, if you are someone who wants to learn how to add a user to a group in Linux then you are in the perfect place. Here, in this tutorial blog, we will learn 3 ways of adding a user to a group.
In Unix-like operating systems such as Linux, we can organize users in groups in order to manage user access to the system resources. Like, Regular users need superuser privileges to make changes in system configuration files or to install & uninstall packages.
No matter if you are a system administrator or a regular Linux user at some point you may need to know about user and group management in Linux. Before learning how to add a user to a group, it is helpful to know how to list all the groups in Linux.
Table of Contents
usermod is a Linux command which is used to modify user information on a system. But we can use it for adding a user to a group also. In order to do so, we will have to
-aG flag with the
usermod command followed by the group name.
Check the syntax:
sudo usermod -aG group_name user_name
Here, Replace the group_name and user_name placeholders with the actual username and group name.
For example, Suppose, We want to add user john to the sudo group to provide superuser privileges. So, we will have to run the following command-
sudo usermod -aG sudo john
Here, in command, we are using
sudo twice. We are using first
sudo because in order to add a user to the group we need superuser privileges and for the second time we have placed “Sudo” because we are adding the user “john” to the
In short, the first
sudo for superuser privileges, and the second is for adding “john” to “sudo” group.
You can use the
gpasswd to manage group passwords and membership in Linux. Using this command you can add one or multiple users to a group. As well as, you can also remove users from groups using the
gpasswd command is mainly used for adding, removing, or modifying group passwords. However, we can also use it for adding user to a Linux group.
Here is the syntax:
sudo gpasswd -a user_name group_name
We already have explained about
gpasswd command and
-a is a flag that we can use with
gpasswd for adding users to groups. And then we will place the username and group name. In the syntax, we have used
group_name words as a placeholder only you will have to replace these placeholders with the actual user name and group name.
Also, you can see, we have used
sudo before the command; you will have to use it if you don’t log in as a root user.
For example, if you want to add the user “jack” to the “admin” group then you will have to run the following given command:
sudo gpasswd -a jack admin
Note: The changes will take effect after logging out and logging in back again.
Add User to a Group Using
adduser command is mainly used for adding users to Linux systems. But can also be used by administrators and regular users with Sudo privileges to manage other users on the system. Adding a user to a group is also a part of user management.
adduser user_name group_name or sudo adduser user_name group_name
As we already have mentioned we can add a user to a group using
adduser command in Linux. Here, from the syntax, you will have to replace the placeholders user_name and group_name with valid user and group name.
It is also possible to add user in multiple groups by specifying group names separated by command.
Let’s have an example:
sudo adduser jack admin, dips
Here, in the example, “jack” is the user name, and “admin” and “dips” are groups.
If you want to change the primary group of the user then you will have to
-g flag followed by the group name.
sudo adduser jack -g dips
Here, by using the -g flag we are making group “dips” the primary group of user “jack”.
Note: The -g flag may not work in some Linux distributions.
Primary Group: When we create a user on a Linux system a primary group gets created automatically with the same name as the user name. Like, if you create a user “jack” a group will also be created with the same name “jack” and it will be the primary group for “jack”.
In this tutorial blog, we learned how to add users to single or multiple groups using different commands. We covered commands like
adduser. You are free to use any of these commands there are not many differences. Also, we explained, In order to add a user in multiple groups you can specify user names separated by a comma.
If you are getting any difficulty then please comment down below. We love comments and we would definitely come up with the best solution possible.