Overview

We’re going to cover the steps how to add user to the group in Linux via terminal and how to remove a user from the group. This is also another essential skills a Linux user should know since this procedure is a part of user management practice.

This post is a part of our Linux user management post series. In one of our earlier posts, we covered the process how to create and delete users in Linux via terminal(you can check out the procedure here - post).

We’ll also cover how to create and delete groups. These actions do require to be logged in the Linux system either as a user with sudo privileges or as a root user.

We’ll be using commands “usermod” and “gpasswd” to perform the actions mentioned here.

How to add user to the group in Linux

To add user to group in Linux, use the “usermod –a –G groupname username” command followed the name of the group and the user.

sudo usermod -a -G groupname username

Add user to the group in Linux

There is no information that your command was successful, usermod command does not display any output. It warns you only if the user or group doesn’t exist. Always use the –a (append) option when adding a user to a new group. If you don’t, user will be removed from any other group, and only will be in the group you just added him.

Add user to multiple groups with one command

It is the same procedure as adding user to only one group, but this time you have to name more groups in just one command, for example:

sudo usermod –a –G groupname1,groupname2,groupname3 username

How to remove user from the group

Now we will show you how to remove a user from group. For this you are going to use command “gpasswd” with the –d option.

sudo gpasswd –d username groupname

This time command will tell you that user was removed from the group, like shown in picture

Add user to the group in Linux

And now you have successfully removed a user from a group.

How to add user to a sudo group

If you require to give a user the sudo privileges(super user permissions) for that you need to add user in a sudo group. For more info about sudo groups and adding user to sudo, check out the post where talk in more detail about procedures of adding user to sudo group.

For Debian/Ubuntu based systems run the command like this:

sudo usermod -a -G sudo username

For the RHEL/Centos/Rocky/Fedora systems:

sudo usermod -a -G wheel username

How to create a group in Linux

Command that you will use for this is:

groupadd –g groupdID groupUsername

-g groupID

The “-g groupID” parameter indicates that a group number will follow(will be assigned to the group and act as a ID number). This is the group number that will be assigned to this new group. The group number must be unique.

groupUsername

The name of the group you would like to add. Group names should be entered in lowercase and may contain underscores. It is recommended that you do not use the same group name more than once.

We will now create a group with its id number and a name with a command:

sudo groupadd –g 15000 testgroup

Add user to the group in Linux

Once you have done that just hit Enter and the group should be created, if there is no errors that means that everything Is fine and the group is created. Since new groups are added to the end of the system group file called “/etc/group” you can use tail command to check if your group is created. Just type “sudo tail /etc/group” after the system prompt to show the last few lines of the system group file.

sudo tail /etc/group

Add user to the group in Linux

As you can see group has been created.

How to delete a group

To remove a group we will use a command “groupdel” and type the name of the group you want to remove.

sudo groupdel groupname

And your group should be removed. To check if the group was removed you can use tail command like we did for checking if you group was created, but this time only your group will not be on the group list.

sudo tail /etc/group

add user to a group in linux

Summary

To summarize what we have covered in this post. We went through the steps how to add user to the group in Linux and how to remove user from the group. We also showcased steps how to create a group and how to delete a group in Linux too.

All these commands shown in today’s article can also apply for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, Fedora, and Arch Linux.

Thank you for your time.