Open In App

How to Remove Users from Groups in Linux?

Last Updated : 15 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Groups in Linux are an important part of organizing the system’s access control. Creating separate groups for separate types of roles of users allows the administrator to manage the access control of the Linux system efficiently.

It is an essential skill to understand how to add, remove, and update users in a group from Linux. In this article, we shall learn various methods for removing a user from a group in Linux using different command line tools. We shall use Kali Linux for demonstration in this article.

Pre-requisites

  • A Linux machine with root privileges.
  • Basic understanding of Linux terminal and commands.

Method 1: Using the `deluser` command

The deluser is a Linux command for deleting users from systems/groups. The syntax of the command is :

deluser <username> <groupname>

Step 1: Check the groups of the user (optional)

First, we shall verify the groups of the user for demonstration purposes. This can be done with the groups command.

groups <username>

In this article, we shall use the ‘dummy’ user as an example and delete it from different groups.

How to remove a user from a group in linux?

Checking groups of the user dummy

As we can see the user belongs to three groups. In the next step, we shall remove the dummy user from users group.

Step 2: Removing the user from a group

We can modify the userdel command for this purpose:

deluser dummy users

Output:

Picture3

Deleting dummy from users group

As we can see in the response message, the user is removed from the group.

Step 3: Verifying the deletion of the user from the group.

We can verify the result from the same method as used in Step 1.

groups dummy
How to remove a user from a group in linux?

Verification of user deletion from group

As we can verify, the user is removed from the group users.

Method 2: Using the gpasswd command

There is another command, the gpasswd command, to remove users from a group. The syntax of the command is simple:

gpasswd [options...] <groupname>

Now, to delete a user from a group, we use the -d option followed by the username.

gpasswd -d <username> <groupname>

Step 1: Deleting a user from a group using the user command.

We shall use the same user ‘dummy’ as in the previous method. But, this time we shall delete the user from the ‘video’ group. The command we need is modified as follows:

gpasswd -d dummy video

We can recheck the existing groups using the same method as in Method 1:

Picture5

Deleting a user from the group using gpasswd command

As we can see, the user was a member of the video group and then, we deleted it using gpasswd.

Step 2: Verifying the result (Optional)

We can again verify using the groups command as before:

groups dummy
Picture6

Verifying the results

As we can verify here, the dummy user is no longer a member of the video group.

Method 3: Editing the /etc/group file

In this method, we shall edit the /etc/group file, which contains information regarding the users, to remove a specific user from the group.

Step 1: Open the file

We can open the file using any editor with root/sudo permissions but, for this tutorial we are using the nano editor.

nano /etc/group

This will open the file. Then, you need to navigate to the line beginning with your group’s name. For example, we are removing the dummy from the sudo group so, we shall edit the line beginning with ‘sudo’.

2Picture2

Editing the sudo group from /etc/group file to remove dummy user

Step 2: Removing the dummy from the sudo group

Now we need to edit the line containing sudo group and remove the user mentioned in the list of users seperated by ‘,’. The syntax of this file is:

<groupname>:x:<GID>:[users...]

The last column/field contains the usernames seperated by a ‘,’. So, in order to remove a user from the group, we need to delete that user’s name from this field in group file. In our case the user is dummy so, we shall remove its name.

The file should look like this after edition:

3Picture3

Edited file.

Now, press Ctrl+S for saving the file and Ctrl+X for exiting the editor. The dummy user is now successfully removed from the sudo group.

Step 3: Verification

We can verify the same using the same verification as previous methods:

groups dummy

This should not show sudo anymore.

4Picture4

Group of dummy user

As we can see, dummy is no longer a member of sudo group.

Conclusion

In this article, we learned three different methods for removing a user from a group in linux. First we used the deluser command and removed a user from a group. Then, we saw how to use the gpasswd command for the same purpose. Lastly, we used a more direct approach and edited the /etc/group file to remove users from a group.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads