How to Add a User to a Group in Linux?

Today, we’ll go over the steps to add a user to a group in Linux. In Linux users can belong to one or many groups. These groups make it easy to give permissions to a collection of users in one go. Furthermore, users can be added to groups, removed from groups, we can create new groups and delete existing groups.

1. Listing all existing groups

To list all existing groups in a Linux system use the command:

less /etc/group
Groups in Linux
/etc/group files

The /etc/group files contains information about all the groups.

2. Add a user to a group in Linux using adduser command

To add a user to a group use the command:

sudo adduser [username] [groupname] 
Adduser
Adduser Command to add a user to a group in Linux

3. Add a user to a group using usermod command

Alternatively you can also use usermod command to add a user to a group:

$ sudo usermod -a -G [groupname] [username]
Usermod Command
Usermod command to add your user to a group in Linux

Note that the order in which username and group name appear in usermod command and adduser command is different.

-a flag is necessary to specify that the user is to be appended to the mentioned list of groups. Without the -a flag, the user is added only to the mentioned group and is removed from all the other groups. In case the group doesn’t exist, the command will give an error.

4. Listing all the groups of an existing user

For a particular user, you can list the groups that the user is a part of using groups command. Make sure that you are logged in as the user you want to get the list of groups for.

$ groups 
Groups Command

5. List all members of a group

You can list all the members of a particular group using the getent command. It is a command that is used to query from files in the /etc directory.

$ getent group [group_name]
Group Members

6. Add a User to Multiple Groups at Once

You can add a user to multiple groups at once. To do so use the command:

$ sudo usermod -a -G [groupname1],[groupname2] [username]

This command will add the user to all the groups mentioned. Make sure that there is no space before and after the comma separating the group names.

7. Remove a user from a group

To remove a user from a group use the gpasswd command with ‘d‘ flag:

$ sudo gpasswd -d jayant masters
Remove From Group

8. Creating new groups

To add new groups to the Linux system use the following command:

$ sudo groupadd [groupname]
Groupadd

This command will create a new group with no users in it. We will see how to add users to a group in a bit.

9. Deleting a Group

To delete a group in linux use the command:

$ sudo groupdel groupname

Be careful while deleting a group. It is better to check the members of a group before deleting. However, deleting a group doesn’t delete the members of a group.

Conclusion

We saw how users are added to groups in Linux. Along with that we also saw how groups can be created and deleted. Groups are convenient to organise users into a collection. Giving permission to a group means granting the permission to all the members of the group.

We have a very comprehensive article on Linux user administration. If you need further details on managing multi-user systems, have a look there.