How To Add or Remove a User From a Group in Linux


This guide shows how to add a group, add a user to a group, and remove a user from a group in a Linux system. It is a starter guide to creating and managing groups in Linux for system administrators or DevOps engineers.

What is a group in Linux?

In Linux, a group is a collection of system users. A user account can be associated with more than one group. Groups allow a system administrator to easily deal with multiple users simultaneously, especially when managing permissions.

For example, all users permitted to invoke the “sudo” command can be added to the sudo group in Debian and derivatives and the “wheel” group in RedHat Enterprise Linux and its derivatives. All information about groups is stored in the /etc/group file.

Note: Before you can add a user to a group, ensure that the group exists on the system. Also, most system groups required by applications or services are usually automatically created during the package installation. For example, the docker group is automatically created on Debian and Ubuntu systems after installing the Docker tools.

Create a New Group in Linux

To create a new group, for example, called “fossguides” use the groupadd command like this:

$sudo groupadd fossguides
$sudo addgroup fossguides
Add a new group in Linux

If the fossguides group already exists on the system, you will see the error shown in the following screenshot. Simply ignore it and proceed to the next step of adding your user to the group.

Add a User to Group in Linux

To add a user to a group, run the command below, remember to replace the group name (fossgudies) and username (fossman):

$sudo usermod -aG fossguides fossman
$ sudo gpasswd -a  fossman  fossguides 
Add a user to a group in Linux

To add the currently logged-on user to a group, you can pick the username using the $USER environment variable as follows:

$sudo usermod -aG fossguides $USER
$ sudo gpasswd -a  $USER fossguides

To log in to your new group, use the newgrp command as follows. Alternatively, log out and log in again to start a new session with all your groups:

$newgrp fossguides
Log into a new group and check user groups
Display Members of a Group

To display who is a member of a group, use the getent command:

$getent group fossguides
Check members of a group
Remove a User from a Group

To remove a user from a group, run the following command, replace fossman and fossguides with your username and group name respectively:

$sudo gpasswd -d fossman  fossguides
Final Remarks

In this guide, I have shown you one of the important aspects of managing users and permissions in Linux systems, using groups. Remember, the feedback form is right below if you have any questions or comments.

You may also like...

Leave a Reply

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

Page Contents