Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

An essential part of Linux administration is the management of users and groups. And needing to add a user to more than one group at a time is not uncommon. Thus, in this tutorial, we’ll use the Linux command-line environment to perform this task. The commands in this tutorial will work in the Bash shell of any major Linux distribution.

2. Adding a New User With Several Groups

Before modifying an already existing user, it’s worth mentioning that we can create a new user while simultaneously adding them to several groups.

We can add a new user with the command useradd. Thus, let’s see an example where we create a new user with several groups:

# useradd john -G work,home,school

In the example above we added a new user, john, while also adding him to three preexisting groups: work, home, and school.

3. Verifying

With the help of the groups command, we can verify which groups the user is part of.

Let’s try it with our user, john:

$ groups john
john : john work home school

We can see, by the example above, that it’s very straightforward. The groups command shows us only the names of the groups in which john is included.

Another useful tool is the id command, which also shows uid and gids:

$ id john
uid=1001(john) gid=1004(john) groups=1004(john),1001(work),1002(home),1003(school)

In the example above, the id command shows us that the user john is in the desired groups, and it shows us the user’s uid and all of the groups’ gids.

4. usermod

To add a user to groups in Linux, we use the usermod command. It already supports adding a user to several groups, and it’s usage is as simple as listing all the intended groups.

Let’s try an example:

# usermod -G work,home,school john

We added the user john to groups work, home, and school in the example above. The groups list must be comma-separated and must not contain spaces between them. Option -G will add the user to the selected groups, exclusively, while removing the user from the ones not listed in the command.

We can use option -a in addition to -G. The argument -a stands for ‐‐append, which means we’re adding the user to supplementary groups in addition to the ones they already belong to.

Let’s run an example for the case above:

# usermod -a -G work,home,school john

Now, the user will be added to the listed groups but also kept in the ones it already belonged to.

We can then verify if the modification was effective:

# groups john
john : john work home school

In the example above we checked, with the command groups, that john was added to the selected groups.

5. Editing /etc/group

Linux stores the basic group definitions within the /etc/group system configuration file.

We can, therefore, edit that file and modify it per our needs.

This is the most obvious way to perform such a task: opening /etc/group with our preferred text editor and adding the desired user to the group.

Let’s open it with the text editor Vim:

# vim /etc/group

The command above opens the file with Vim. Now, let’s add the username to the rows that represent the groups we want to modify:

  1 root:x:0:
  2 bin:x:1:
  3 daemon:x:2:
  4 sys:x:3:
  5 adm:x:4:
  6 tty:x:5:
  7 disk:x:6:
  8 work:x:1001:john
  9 home:x:1002:john
 10 school:x:1003:michael,john
 11 users:x:100:
 12 nogroup:x:65534:
 13 john:x:1000
~                                                         
~                                                         
~                                                         
~                                                         
"/etc/group" 13L, 129B                       1,1         All

In the snippet above, rows 8, 9, and 10 were where the groups were listed, so we just had to append the username, john, to those rows.

There was already a username in row 10, so we added a comma before the new username.

Now, after we save and exit, the changes will be effective the next time john signs into the system.

Verifying afterwards, we have:

$ groups john
john : john work home school

Above we checked, with the command groups, that our modification to the system file was effective.

6. Multiple Users and Multiple Groups

Since we learned how to add one user to multiple groups, we can stretch a little further and add multiple users to multiple groups. We can perform that task using the same tools presented in this tutorial, with little to no further effort. Let’s take a look.

6.1. usermod and for Loop

Adding multiple users to a group is not native to usermod, but we can take advantage of the Bash environment, which offers us very helpful resources such as the for loop.

Using the same tool, usermod, and the same structure of arguments, we can repeat the command as many times as we need:

# for u in {john,martha,francis}; do usermod -aG work,home,school $u; done

The strings we listed within braces in the example above expand to be iterated over. So, for each username in the comma-separated string, the command runs once. Each time, we keep the username string in the auxiliary variable u.

And we can confirm with:

$ groups john martha francis
john : john work home school
martha : martha work home school
francis : francis work home school

With the command groups, above, we verified that our modifications were processed. Note that we can do so with several users at once.

7. Conclusion

In this article, we saw how to add a user to several groups using useradd and usermod. We also explored another option in case we don’t have usermod at hand. Finally, we went further and performed the same tasks for multiple users.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!