Read in 4 minutes

last updated 

How to Add User to Group in Linux

This tutorial will show you how to add user to a group in Linux systems.

Linux groups are organization units which are used to organize and administer user accounts in Linux. The main purpose of groups is to define a set of privileges such as reading, writing, or executing permission for a given resource that can be shared among the users within the group.

In Linux, to add a user to a group you can use the usermod command. You will need to be logged in as root or as a user with sudo access to be able to execute the command.

Linux Groups

There are two types of groups in Linux operating systems:

  • Primary group – When a user creates a file, the file’s group is set to the user’s primary group. In most cases the name of the group is the same as the name of the user. The information about the user’s primary group is stored in the /etc/passwd file.

  • Secondary or supplementary group - Useful when you want to grant certain file permissions to a set of users which are members of the group. For example if you add a specific user to the docker group, the user will inherit the access rights from the group and it will be able to run docker commands.

Each user can belong to exactly one primary group and zero or more secondary groups.

How to Add an Existing User to a Group

To add an existing user to a secondary group, use the usermod command followed by the -G option and the name of the group:

sudo useradd -a -G groupname username

For example if your username is linuxize and you want to add the user to a group named sudo you’ll need to run the following command:

sudo usermod -a -G sudo linuxize

Always use the -a (append) switch when adding a user to a new group. If you omit the -a switch the user will be removed from any groups not listed after the -G switch.

On success the usermod command will not display any output. It will warn you only if the user or group doesn’t exist.

How to Add an Existing User to Multiple Groups in one Command

If you want to add an existing user to multiple secondary groups in one command, use the usermod command followed by the -G option name of the group separated by , (commas):

sudo usermod -a -G group1,group2 username
Advertisement

How to Remove a User From aGroup

If you want to remove a user from a group, use the gpasswd command, In the following example we are removing a user named username from a group named groupname:

sudo gpasswd -d username groupname

How to Create or Delete a Group

To create a new group you can use the groupadd command followed by the group name:

sudo groupadd groupname 

To create a group use the groupdel command followed by the group name:

sudo groupdel groupname

How to Change a User’s Primary Group

To change a user primary group, use the usermod command followed by the -g option:

sudo useradd -g groupname username

For example if your username is linuxize and you want to change the user primary group to developers, you’ll need to run the following command:

sudo usermod -g developers linuxize

How to Create a New User and Assign Groups in One Command

The following command will create a new user named nathan with primary group users and secondary groups wheel and developers.

useradd -g users -G wheel,developers nathan

Display User Groups

To display a complete user information including the group membership use the id command followed by the username:

sudo id username

If you omit the username the command will print the information about the currently logged in user. Let’s check the user linuxize:

id linuxize
uid=1000(linuxize) gid=100(users) groups=100(users),10(wheel),95(storage),98(power),990(libvirt),993(docker),999(kvm)

From the output above, we see that the primary group of this user is users and it belongs to wheel, storage, libvirt, docker and kvm supplementary groups.

You can also use the groups command to display the user’s supplementary groups:

groups linuxize
wheel storage power users libvirt docker kvm

Same as with the id command if you omit the username the groups command will print the currently logged in user’s groups.

Conclusion

In this tutorial, you learned the type of user’s groups in your Linux system and how to add a user to a group. The same commands apply for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian and Linux Mint.

Feel free to leave a comment if you have any questions.