How to Add User to Group in Linux

How to Add User to Group in Linux

In this tutorial, we will learn how to add a Linux user to any group, and we will also learn how to remove the Linux user from a group.

Linux Groups

In Linux operating system, every user has their group. The main purpose of the group is to define the privileges such as executing, reading, and writing on the resources (Files and Directories)  which is shared among the group member.

Linux operating have two types of groups:

  • The Primary group :- When a user creates a files or directory,  User’s primary group is set on the group of the Files or Directory which Linux user has created. We can check the user and Group details from these two files /etc/passwd and /etc/group.
  • Secondary or supplementary group :- When we want to allow Linux user to grant access on some files. We simply add the user to the group which is assigned to the files or directories. For example, we have 2 Linux user A and B, If we add A user in B’s group then A user can access all the files and directories which comes user the B’s group.

In Linux operating system, every user must have a primary group and also have zero or multiple secondary groups.

Only Root and Sudo users can add and delete users from the groups.

Read Also: How to Setup or Configure Git Username and Email Address

Add a User to a Group

To add the user to a secondary group, use the following command.

sudo usermod -a -G Group_name User_name

For example, we want to add the linuxpanda user to the ubuntu group. Run the usermod with -a and -G attribute.

sudo usermod -a -G ubuntu linuxpanda

Add User to Multiple Groups

To add a user in multiple groups, use the usermod command with -a and -G attribute followed by the group names (use comma to separate the group names).

sudo usermod -a -G group1,group2 User_name

Remove a User From a Group

To remove the user from any group, use the gpasswd command with -d attribute

sudo gpasswd -d username groupname

For example, we want to remove linuxpanda users from ubuntu group

sudo gpasswd -d linuxpanda ubuntu

Create a Group

To create a group, use the groupadd command.

sudo groupadd groupname

For example, we want to create a group named as linuxpanda. Run the following command.

sudo groupadd linuxpanda

Delete a Group

To delete a group in Linux operating system, use groupdel command.

sudo groupdel groupname

For example, we want to delete a group named as linuxpanda. Run the following command.

sudo groupdel linuxpanda

Change a User’s Primary Group

If we want to change the primary group of a Linux user, use usermod command with -g attribute.

sudo usermod -g groupname username

For example, we want to change the primary group of  linuxpanda users. Run the following command.

sudo usermod -g www-data linuxpanda

Create a New User and Assign Groups

We can create the user and assign the group with a single command.

sudo useradd -g primary_group -G Group1,Group2 User_name

For example, we want to create a Linux user named as linuxpanda with linux_group as a primary group and add this user in two groups, www-data, and ubuntu.

sudo useradd -g linux_group -G www-data and ubuntu linuxpanda

Display User Groups

If we want to check all the information of a user, like groups in which the user is added.

Use Id a command followed by the Linux Username.

id username

If we want to check the information of the current login user. Simply use the id command without any username.

For example, we want to check the information of the linuxpanda user.

id linuxpanda
uid=1003(linuxpanda) gid=1004(linuxpanda) groups=1004(dev_ftp),33(www-data),1002(developer)

From the output shown above, we can see the username (UID), primary group (GID), and secondary groups.

If you only want to see the secondary groups of a user, use the groups command as follows:

groups linuxpanda
linuxpanda : linuxpanda www-data developer

If we want to check the secondary group’s information of the current login user. Simply use the groups command without any username.

Conclusion

In this tutorial, we have learned how to add and delete the user from a group. These are the default command which is pre-installed in all the Linux operating system. We can use this tutorial for any Linux opening system like CentOS, Ubuntu, etc.

If you guys have any queries related to this tutorial, then let me know in the comments section.