How to add a user to a group in Linux

In this tutorial, we will see how to add a user to a group in the Linux system. For those of you who don’t know, let’s start with what are Linux groups.

What are Linux Groups?

Linux Groups help in allotting specific access or privileges such as reading, writing, or executing permissions, to all the members of the group. All the members of a group exercise the same sort of permissions allotted to the group. So groups allow in managing permissions of the users.

There are mainly two types of groups in Linux systems:

Primary group

The primary group is a set of logged-in users. Generally, it has same the name as the login name of the user. The primary group allows private group membership and security features. Files stored or processes cannot be accessed by other group members. When a user creates a file, the file’s group is set to the user’s primary group.

Secondary group – Useful when you want to grant certain file permission to users. Members of the group inherit the read, write and execute privileges for that group. This is used when you want to allow someone with access to a certain group.

Note: Each user can belong to only one primary group. But there is no limit on the number of secondary groups, a user can be in any number of the secondary groups even zero.

How to Add a user to a Group

To add a user to a group in Linux you need to have sudo acess or you need to be root.

Use this command to add an existing user to a secondary group. Just execute the command below while replacing the group name and username.

sudo usermod -a -G group_name username

OR you can also use the command below to get the same result.

sudo adduser username group_name

But if you are using usermod command do remember to type -a(append). If in case you forgot to add that, the user will be removed from any other group he/she is already a part of and only will be added to the group you mentioned in the above command.

How to add a user to multiple groups in a single command

You might want to add a user to multiple groups, this command is the easiest way to do so.

sudo usermod -a -G group_name1 , group_name2 username

In this command replace the group name with the groups you want to add a user to. And don’t forget to type -a.

How to create a new user

So if you are looking to create a new user and also want to add it into groups then execute the command below.

sudo useradd -G group_name new_username

The above command lets you create a new user on the go as well as adds the user to the group you want to add. Now we just need to assign a password to this new user account.

sudo passwd new_username

How to remove a user from a group

Now if you are done and now want to remove the user from the group, then execute the command below.

sudo gpasswd -d username group_name

This will remove the user from group.

Change a users Primary group

All the above commands were related to secondary groups. So if you want to change a user’s primary group then execute the command below.

sudo usermod -g group_name username

Here the lower case -g specifies the primary group.

How to view a users information

If you want to see complete user information like all the groups a user is a member of then execute the command below.

id username

How to Delete a Group

If you don’t want a group anymore and need to delete it, then execute the command below.

sudo groupdel group_name

Conclusion

In this tutorial, we saw how can we add a user to a group in Linux. Along with it, we have seen how to remove it from the group, how to create a group, and how to delete a group.

These commands are same for any Linux distribution, like Ubuntu, Linux Mint, Debian, RHEL. I hope I am able to help you out. And If you have any questions feel free to ask in comments. I would love to answer that.

Also read this Is it possible to replace Android with Linux on a smartphone.

1 thought on “How to add a user to a group in Linux”

Leave a Reply

Share via
Copy link
Powered by Social Snap