How to add a user to a group in Linux

In operating systems, the concept of users has existed for a long time. Not all users are the same, some are assigned privileges that the other users may not have since there can be multiple users on a system. If there are a lot of users co-existing on a system, it can become very tiresome and repetitive to assign certain privileges to each user one by one. Groups exist for situations such as these, where they can come in very handy. Groups can be made up of multiple users based to share common settings.

This article will provide guidance on how Users can be added to a group on Linux.

Learn to Add a User to a Group

To get information about the groups, you can simply run the command shown below which will show all the current existing groups in your system:

$ compgen -g

Method 1: Create a New Group

One way to add any user to a group is to simply create a new group and then add the user you want into the group. To create a new group, run the command shown below in the terminal:

$ sudo groupadd sampleGroup
how to handle linux files and direc...

This command is demonstrated below where “sampleGroup” can be any name that you want for the group.

The next step is to add an existing user to this group. For instance, the following command adds the “itslinuxfoss” to the group “sampleGroup”:

$ sudo usermod -a -G sampleGroup itslinuxfoss

Method 2: Add User to Multiple Groups

Another feature that Linux provides is that you can add a single user into multiple groups on the system. This method utilizes the “usermod” keyword.

The below-stated command will add the “itslinuxfoss” user to two groups “sampleGroup” and “sampleGroup2”:

$ sudo usermod -a -G sampleGroup,sampleGroup2 itslinuxfoss

Method 3: Shift a Users Group

If a user is already in a group and you want to change its group, you can again use the usermod command. The example command is provided below:

$ sudo usermod -g sampleGroup itslinuxfoss

If the primary group of the user is already the specified one, then this output will be shown otherwise the user will be shifted to the group.

Bonus-Tip: Check the Groups of a User

One can also check the groups of the user. Like, the command provided below provides the list of the users to which the user is added:

$ id itslinuxfoss

Conclusion

There are 3 distinct methods of adding a user to a group. The first method is to create a new group and add a user to it. Another method involves adding users to more than one group at once. The third is to take out the user from a certain group and move to another. This article has demonstrated various methods to add users to a group.