Ubuntu

How to Add User to Group in Linux

Add User to Group in Linux

Groups make it easy to give users the same security and access privileges for modifying a file or directory. A single user can be associated with several groups. In Linux, groups can be classified into two types that are primary and secondary groups. Primary groups are created during a user account creation while the secondary group can be one or more groups a user belongs to.

In this post, we are going to describe how to add a user to a group in Linux OS. This post will also cover adding a user to multiple groups, removing a user from a group, changing a user’s primary group, creating, removing, and listing groups.

Note: The procedure explained here has been tested on Ubuntu 20.04 LTS. However, the same procedure is also valid for other Linux distributions.

Prerequisite

You will need sudo privileges or root access in order to add or remove a user from a group in Linux.

How to Add an Existing User to a Group

When a new user is created, it is automatically added to a group of the same name. For instance, after creating a user named “tin”, the system will automatically create a new group named “tin”. Then this new user “tin” will be added to this new group named “tin”.

If you want add a user to some other groups, the command is as follows:

$ sudo usermod -a -G <group_name> <user_name>

For instance, the below command will add an already existing user named “tin” to a secondary group named “samba”.

$ sudo usermod -a -G samba tin

Enter the sudo password, after which a new user will be created and assigned a group.

add user to group

How to Add an Existing User to Multiple Groups

If you want to add an existing user to multiple secondary groups, you can do so using a single command as follows:

$ sudo usermod -a -G <group_name1,group_name2,group_name3> <user_name>

For instance, the below command will add an existing user named “tin” to three groups named “sudo”, “samba”, and “admin”.

$ sudo usermod -a -G sudo,samba,admin tin

add user to multiple groups

Create a New User and Add in a Group

If you need to create a new user and add it in a secondary group, here is the command to do so:

$ sudo useradd -G <group_name> <user_name>

For instance, the below command will create a new user named “titin” and add it to the already existing group named “samba”.

$ sudo useradd -G samba tintin

Enter the sudo password, after which a new user will be created and assigned a group.

create and add user in a group

How to Change the Primary Group of a User

To change the primary group of a user, the command is as follows:

$ sudo usermod -g <group_name> <user_name>

For instance, to change a user’s named “tin” primary group to “samba”, the command would be:

$ sudo usermod -g samba tin

change the user's primary group

View the Groups a User is added To

After adding a user to a specific group, you can confirm it as follows:

$ groups <user_name>

It will show you the groups to which the user specified as <user_name> is added to. For instance, the below command shows that a user “tin” is added to two groups “tin” and “samba”. View the Groups a User belongs to

How to Remove a User from a Group

For removing a user from a specific group, the syntax is as follows:

$ sudo gpasswd -d <user_name> <group_name>

For removing a user named “tin” from the group named “samba”, command would be:

$ sudo gpasswd -d tin samba

remove a user from a group

How to Create a New Group

If you need to create a new group in Linux, you can do so by using the below command in Terminal:

$ sudo groupadd group_name

Make sure to replace group_name with the group name you want to create.

How to Remove a Group

If you need to remove a group from the Linux system, the syntax is as follows:

$ sudo groupdel <group_name>

For instance, to remove a group named “samba”, the command would be:

$ sudo groupdel samba

View All Groups on the System

If you want to view all the groups on the Linux system, the command is as follows:

$ getent group

This command will list all the groups on your system along with the user accounts which are member of these groups.

For instance, the following output shows the list of groups along with their members like you can see users “syslog” and “kbuzdar” are members of group “adm”.

view all groups in Linux

This was all about how to add a user to a group in Linux. If you want to learn about adding or removing a user on Linux OS, you can follow our guide on How to Add and Remove Users on Linux.

Similar Posts