Letâs be honest, Linux is tough! Even the smallest tasks require so much effort. If youâre a hardcore Linux user and you plan to stay the same for years to come, you need to have a complete understanding of the platform. If youâre reading this blog, then your current problem is finding how to add users to a group on Linux.
Changing the group a user is assigned to is pretty easy. If you donât know how to, just keep reading.
User accounts can be assigned to one or more groups on Linux. You can configure file permissions and other types of access by group.
If youâre having trouble in adding a user to a new group or a secondary group on Linux, then all you need to do is write a few lines of code. Donât worry, weâll share the code with you.
Letâs get started.
How to Add a User to a Group in Linux
There are 7 techniques weâll be teaching you that you can use to add a user to a group in Linux.
1. Add a New Group
Create a new group on your system and then add the user into the group. Use the command âgroupaddâ and replace ânew_groupâ with the name of the group you want to create.
To run this command, youâll need to use sudo as well. If youâre using Linux Distributions that donât use sudo, youâll need to run the âsuâ command to get the permissions required before running the command.
The final command will look something like this:
Sudo groupadd mynewgroup
2. Add an Existing User Account to a Group
If you want to add an existing user to a group, you can run this command on your device. Use the usermod command and replace the examplegroup with the name of the group. Hereâs how the command will look like:
Usermod -a -G examplegroup exampleusername
If you want to add the user Chris to the Group red, use the following command.
Usermod -a -G red chris
3. Change The Userâs Primary Group
A user account can be part of multiple groups, but thereâs always a âprimary groupâ. A userâs login process and files and folders are all stored in the primary group.
To add a user to other group, you can change the userâs primary group. To change a userâs primary group, youâll need to run the âusermodâ command. When you run the command, replace âexamplegroupâ with the name of the group you want to be the primary one. Also, replace the âexampleusernameâ with the name of the account user.
Hereâs how the code will come out to be:
âusermod -g groupname usernameâ
Do keep in mind that weâve used the lowercase -g. This is used when you assign a primary group. When you use an uppercase -G, you assign a new secondary group.
4. View the Groups a User Account is Assigned To
You can view all the groups the current user account is assigned to and then you can change the group. Run the groups command. Youâll see a list of groups.
If you want to see all the numerical IDs associated with each group, run the âidâ command instead.
To view the groups that a user is assigned to, run the âgroupsâ command and specify the name of the user account. Hereâs what the command looks like âgroups exampleusernameâ.
You can view the numerical IDs associated with each group by running the id command and specifying a username. Hereâs how the command will look like âid exampleusernameâ.
The first group in the groups list or shown after âgid=â in the id list is the user accountâs primary group. The other groups are secondary groups.
5. Create New User and Assign a Group
You could also create a new user account that has access to a specific resource or directory, like a new FTP user. You can choose the group the user will be assigned to when creating the user account.
Use this command to do so, âuseradd -G examplegroup exampleusernameâ.
For example, to create a new user account named chris, and assigning the account to the FTP group, youâd have to run the command âuseradd -G ftp chrisâ.
And ofcourse, youâll have to aassign a password to the user afterwards. To do so, weâll have to run a command, such as âpasswd chrisâ.
6. Add a User to Multiple Groups
While assigning the secondary groups to user account, you can assign a number of groups at once. How? You can just separate the groups with a comma.
Take a look at this code: âusermod -a -G group1,group2,group3 exampleusername.â
Letâs say you want to add a user named Chris to groups red, blue, and green, youâd run this code – âusermod -a -G ftp,sudo,example geekâ.
You can add as many groups as you want, just separate the groups with a comma.
7. View All Groups on the System
If you want to check all the groups on the system, you can use the âgetentâ command. The command looks like: âgetent groupâ.
This command will also give you a birdâs eye view of which user accounts are members of which groups.