Site icon DesignLinux

How to Add User to Group in Linux Systems

How to Add User to Group in Linux

In Linux, we can set different users to different roles and responsibilities. Also we can set restriction for specific users and groups. In this tutorial we will show you how to add a user to a group in Linux systems. We will also explain you how to remove a user from a group and how to create, delete, and list groups.

User Groups in Linux

Linux groups are categories of users with pre-defined permissions. The basic purpose of group 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.

There are two types of groups in Linux operating systems:

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

Only root or users with sudo access can add a user to a group.

How to Add an Existing User to a Group

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

sudo usermod -a -G groupname username

For example, to add the user tecnstuff to the sudo group you would run the following command:

sudo usermod -a -G sudo tecnstuff

Ensure that you should use -a (append) option when adding a user to a new group. If the -a option not added, the user will be removed from any groups not listed after the -G option.

The command will not show any output on success. You will get message if user or group does not exists.

Add a User to Multiple Groups in One Command

When you have requirement to add an existing user to multiple secondary groups using one command, you should use usermod command followed by the -G option name of the group separated by , (commas):

sudo usermod -a -G group1,group2 username

How to Remove a User From a Group

If you want to remove any user from group, use the gpasswd command with the -d option.

For example, to remove the tecnstuff user from the sudo group, you would run:

sudo gpasswd -d tecnstuff sudo

Here, you have to replace tecnstuff with your username and sudo with your group.

How to Create a User Group

To create a new group, run the following command:

sudo groupadd GROUP_NAME

You should replace the GROUP_NAME with the name you want for your new group.

How to Delete a Group

You can delete an existing group, using the groupdel command followed by the group name:

sudo groupdel GROUP_NAME

How to Change a User’s Primary Group

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

sudo usermod -g GROUP_NAME USER_NAME

For example, to change the primary group of the user tecnstuff to devops, you would type:

sudo usermod -g devops tecnstuff

Create a New User and Assign Groups in One Command

The following example the useradd command will create a new user with name pintu with primary group users and secondary groups wheel and devops.

sudo useradd -g users -G wheel,devops pintu

List User Groups

To view the user information including all the groups of which user is a member of. You should use id command followed by the username:

id USER_NAME

If you will not pass specific username the system will show the information about the current logged-in user. To check the user tecnstuff:

id tecnstuff
uid=1000(tecnstuff) gid=100(users) groups=100(users),10(wheel),95(storage)

To list only secondary groups of a user, type:

groups tecnstuff
wheel storage users

Conclusion

In this tutorial, we have shown you how to add a user to a group in Linux systems.

If you have any question or suggestion, please leave a comment below.

Exit mobile version