Changing the group a user is associated with is a really easy task, but not everybody knows the commands. Especially to add a user to a secondary group as well. We will walk through all the scenarios for you. In this article, we are going to talk about How to Add User to Group on Linux – Tutorial. Let’s begin!
User accounts can be assigned to one or more groups on Linux actually. You can also configure file permissions and other privileges by the group. Such as, on Ubuntu, only users in the sudo group can use the
sudo the command to gain elevated permissions.
Linux groups are organization units that are used to organize and administer user accounts in Linux as well. The primary purpose of groups is to define a set of privileges actually. For example reading, writing, or executing permission for a given resource that can be shared between the users within the group.
There are actually two types of groups in Linux operating systems:
- The primary group – When a user creates a file, the file’s group is set to the user’s primary group actually. Mostly, the name of the group is the same as the name of the user. The information about the user’s primary group is actually stored in the
- Secondary or supplementary group – Useful when you want to grant many file permissions to a set of users who are members of the group. Such as, if you add a specific user to the
docker group, then the user will inherit the access rights from the group, and also be able to run docker commands.
Each and every user can belong to exactly one primary group and zero or more secondary groups.
Note that only root or users with
sudo access can add a user to a group.
Add a New Group
If you want to create a new group on your system, then use the
groupadd command following command, replacing the new_group along with the name of the group you want to create. You will need to use sudo with this command as well. (Or, on Linux distributions that do not use
sudo, you will need to run the
su command on its own in order to gain elevated permissions before running the command).
sudo groupadd mynewgroup
Add an Existing User Account to a Group
If you want to add an existing user account to a group on your system, then use the
usermod command, replacing
examplegroup with the name of the group. That you want to add the user to and
exampleusername with the name of the user, you want to add as well.
usermod -a -G examplegroup exampleusername
Such as, to add the user
geek to the group
sudo , use the following command:
usermod -a -G sudo geek
Change a User Primary Group
While a user account can be part of multiple groups, one of the groups is always the “primary group” and the others are “secondary groups” actually. The user’s login process and files and folders the user creates will also be assigned to the primary group.
If you want to change the primary group a user is assigned to, then run the
usermod command, replacing
examplegroup with the name of the group. That you want to be the primary and
exampleusernamewith the name of the user account.
usermod -g groupname username
Now note the
-g here. When you use a lowercase g, then you assign a primary group. When you use an uppercase
-G , as above, then you assign a new secondary group as well.
View the Groups a User Account is Assigned To
In order to view the groups the current user account is assigned to. You need to run the
groups command. You will then see a list of groups.
To view the numerical IDs associated with each group, then run the
id command instead:
If you want to view the groups another user account is assigned to. Then run the
groups command and specfy the name of the user account.
You can also view the numerical IDs associated with each and every group by running the
id command and specifying a username.
The first group in the
groups list or the group shown after “gid=” in the
id list is actually the user account’s primary group. The other groups are the secondary groups as well. So, in the screenshot below, the user account’s primary group is
Create a New User and Assign a Group in One Command
You guys may sometimes want to create a new user account that has access to a particular resource or directory, such as a new FTP user. You can also specify the groups a user account will be assigned to when creating the user account with the
useradd command, like so:
useradd -G examplegroup exampleusername
Such as, to create a new user account named jsmith and also assign that account to the ftp group, you’d run:
useradd -G ftp jsmith
You’ll want to assign a password for that user afterward, obviuosly:
Add a User to Multiple Groups
When assigning the secondary groups to a user account. you can easily assign multiple groups all at once by separating the list with a comma.
usermod -a -G group1,group2,group3 exampleusername
Such as, to add the user named geek to the ftp, sudo, and example groups, that you’d run:
usermod -a -G ftp,sudo,example geek
You can specify as many groups as you like—just separate them all with a comma now.
View All Groups on the System
If you want to view a list of all of the groups on your system, then you can use the
This output will also show you which user accounts are members of which groups actually. So, in the screenshot below, we can see that the accounts Syslog and Chris are members of the adm group as well.
That should cover everything you need to know about adding users to groups on Linux.
How to Remove a User From a Group
In order to remove a user from a group, you need to use the
gpasswd command wit the
In the following example, we are now removing the user
username from the group
sudo gpasswd -d username groupname
How to Delete a Group
In order to delete an existing group, use the
groupdel a command followed by the group name:
sudo groupdel groupname
Alright, That was all Folks! I hope you guys like this article and also find it helpful to you. Give us your feedback on it. Also if you guys have further queries and issues related to this article. Then let us know in the comments section below. We will get back to you shortly.
Have a Great Day!
Also See: Compariosn Between WPS Office vs LibreOffice [Office Suite For Linux]