How to Add or Remove Users from Groups in Linux

Linux is a multi-user operating system. That means several users can log in and work on the system at the same time. It is best to add or remove users from the groups in Linux who are sharing system resources. 

Since Linux allows multiple users, it is never a good idea to share the credentials with the users. Instead, you can create accounts of people needing access to the machine. Furthermore, it is useful to divide them into groups as per their resource needs. 

In this article, we will discuss how to add or remove users from groups in Linux. It will also explain how to create a user and list the existing groups. 

Types of Groups

In Linux, groups are of two types: Primary user group and Secondary user group. The primary user group is applied when the user login to their account. The name of this group is the same as the account username. Whenever you create a new user, they will be assigned to the primary group. 

Whereas, the Secondary user group is the other group in which you can become a member. It is useful for sharing resources such as files and directories. By default, you are not a part of this group, unless the root user or a user with root privileges adds you. 

It is important to note that all the group related information in Linux is stored in the following files:

  • /etc/passwd – contains a one-line description for each user account.
  • /etc/shadow – contains the password information related to the user accounts in the encrypted format
  • /etc/group – defines the groups on the Linux system
  • /etc/default/useradd – this file contains the information for the default group

Always remember that you will not modify these files on your own. Instead, you will add or remove the user from groups in Linux by using various commands.

Add Users to the Groups

This section will discuss how you can add users to the group by using various commands. 

1. Check the User

Before you add users to the groups, it is important to check whether they exist in the system or not. To check the existence, you will use the id command with the -u flag, followed by the username of the user.

id -u username

If the user does not exist, it will give the error as shown in the image below:

check the user existence using id command

If the user exists, proceed to step 3. Otherwise, continue to step 2.

2. Create a New User and Add it to the Group

Now that we know that the user does not exist, we will create a new user and then try to add it to the group. To create a new user, execute the command given below:

sudo adduser thomas

This command will ask for the user’s password and some extra information. Press the “Enter” key to leave the fields empty.

add user to group in Linux

You have created the user successfully. Now, we will add this new user to an existing Secondary group. For this purpose, we will use the useradd command. The syntax of this command is shown below:

sudo adduser username {group-name}

If you want to add user “thomas” in “mygroup”, you will type:

sudo adduser thomas mygroup
add user to the group in Linux

This will add “thomas” in “mygroup”. To ensure that the user has been added successfully, you can check with the id command.

3. Add Existing User to the Group

If you want to add an existing user to the secondary group, you must first check its existing groups. To perform this, type the groups command in the Terminal, and you will see a similar output. 

add users to groups

After that, you will add the user to the group. For this purpose, you will use the usermod command as shown below: 

sudo usermod -aG mygroup sidrah

4. Add User to the Multiple Groups

You can also add a user to multiple groups at the same time. You will use the usermod command for this step as well. The only difference is that you will add multiple Secondary group names in one command, separated by a comma. For example:

sudo usermod -aG mygroup1, mygroup2 thomas

Remove Users From the Groups

Now that you have learned how to add users, you should know how to remove them. The first step is to list down the existing groups of a particular user. Then, remove the groups using the gpasswd command. Let’s look at each step in detail.

1. Remove Users From the Groups Using gpasswd Command

1. Firstly, find out the user’s groups by using the id command.

id thomas

2. Secondly, use the gpasswd command to remove the user from the group. 

sudo gpasswd -d thomas mygroup
remove users from groups

3. Similarly, you can also remove the user from multiple groups at the same time.

sudo gpasswd -d thomas mygroup1 mygroup2

However, the user will not see the effect immediately. Instead, it will be visible at the next login.

2. Remove Users From the Groups Manually

In addition, you can also remove the users from the group manually by editing the file /etc/group. The effects of this method will apply to the user upon reboot. This file is stored in the etc directory, and you can access it via root. 

remove users from groups

Navigate to the etc directory using the cd command. After that, type “ls” to view the file list. You will see a long list of several configuration files as shown below:

To edit the group file, type “nano” or “cat” with the filename. 

sudo cat /etc/group
add or remove users from groups

The cat command will only display the content of the file. However, with the nano command, you can edit the contents of the file. 

You will see a long list of all the users along with their groups. You can remove the username stated in front of the group name. After that, make sure to save the file by pressing Ctrl+X. 

In this tutorial, you have learned how to add or remove users from the groups in Linux. You have also learned how to create a new user and a new group. This task is basic but equally essential for Linux administrators as well as novice users. It is crucial for the security of the system.

Feel free to leave a comment if you have any questions.

Leave a Reply
Related Posts