Add or Remove User From Group in Linux

Linux is a multi-user opertating system. Linux also utilizes groups, which contain a set of user accounts. This makes it easier to assign correct permissions on various files throughout the system.

For example, you could grant read and write access to the office group for a certain file, which means anyone in that group can read or write to the file. This is much more convenient than assigning file permissions to individual user accounts.

In this guide, you’ll learn how to add a user to a group and remove a user from a group in Linux.

What Groups Are on My System?

In order to add a user to a group, the group needs to already exist.

To check what groups are already in your system, view the /etc/group file. This will also show which user accounts are in each group.

$ cat /etc/group

Lines from the /etc/group fle

You can also view the /etc/passwd file to see what users are on your system.

$ cat /etc/passwd

Create a New Group

If you don’t already have the needed group on your system, type the following command to add a group. In this example, we add the group office.

$ sudo groupadd office

Add Existing User to Group

Assuming that your user and group are already added to the system, you can use the usermod command to add a user to a group. In this example, we add user bob to group office.

$ sudo usermod -a -G office bob

You can verify the user is now in the group by using the groups command.

$ groups bob

Running the groups command to see what groups a user is in

Create New User and Add to Group

You can also add a brand new user to a group upon creation of the user account. The following example adds new user david to the system while also putting the user in group office.

$ sudo useradd -G office david

Add Multiple Users to a Group

If you need to add a user to multiple groups at the same time, that can be done with the gpasswd command and -m option. For this example, we add users laura, richard, and daniel to group office.

$ sudo gpasswd -m laura, richard, daniel office

Remove a User From a Group

The deluser command can be used to remove a user from a group in Linux. In this example, we remove user james from the office group.

$ sudo deluser james office

Removing a user from a group

Delete a Group

If you no longer need a particular group on your system, you can remove it with the delgroup command. In this example, we delete the office group.

$ sudo delgroup office

Deleting a group from the Linux system

Leave a Comment

Your email address will not be published. Required fields are marked *