How to Remove a User from a Group in Linux

Linux is a powerful and flexible operating system that is widely used in server environments. In these environments, it is common for multiple users to work on the same system and even belong to the same user groups. However, there may come a time when you need to remove a user from a group, either because they no longer require access or due to security reasons. In this article, we'll look at how to remove a user from a group in Linux.

What are User Groups in Linux?

User groups are an essential feature of Linux operating systems. A user group is a collection of user accounts that share a common set of permissions and access rights to resources such as files, directories, and devices. Users in the same group can share files and directories without having to individually grant permissions. User groups are created to make managing permissions easier and more secure.

How to Remove a User from a Group in Linux?

There are several ways to remove a user from a group in Linux. Here are the most common methods

Method 1: Using the gpasswd Command

The gpasswd command is a simple and efficient way to manage user groups. To remove a user from a group using this command, follow these steps

Step 1 Open a terminal window.

Step 2 Type the following command to remove a user from a group

sudo gpasswd -d username groupname

For example, to remove the user "johndoe" from the group "developers," type the following command

sudo gpasswd -d johndoe developers

Step 3 Press Enter, and the user will be removed from the group.

Method 2: Using the usermod Command

The usermod command is a powerful tool that allows you to modify user accounts in various ways, including adding and removing users from groups. To remove a user from a group using this command, you must specify all the groups the user should remain in, excluding the one you want to remove them from

Step 1 First, check which groups the user belongs to

groups username

Step 2 Use usermod with the -G flag to set the user's groups, excluding the one you want to remove

sudo usermod -G group1,group2,group3 username

Important Note: The usermod -G command sets the complete list of supplementary groups for the user. Any group not listed will be removed. Use this method carefully to avoid accidentally removing the user from other needed groups.

Method 3: Editing the /etc/group File

The /etc/group file is the configuration file that stores all the user groups on the system. You can edit this file manually to remove a user from a group. However, this method is not recommended for beginners, as it requires some knowledge of the Linux command line and file editing. To remove a user from a group using this method, follow these steps

Step 1 Open a terminal window.

Step 2 Type the following command to open the /etc/group file in a text editor

sudo nano /etc/group

Step 3 Find the line containing the group you want to remove the user from and edit it. The format of each line in the /etc/group file is as follows

groupname:x:GID:user1,user2,user3

Replace "groupname" with the name of the group and "user1,user2,user3" with the list of users in the group. To remove a user, simply delete their username from the list.

Step 4 Save the changes to the /etc/group file and exit the text editor.

Examples

Let's take some examples to understand how to remove a user from a group in Linux

Example 1: Using the gpasswd Command

Suppose we want to remove the user "johndoe" from the group "developers." We can use the gpasswd command to accomplish this

sudo gpasswd -d johndoe developers

The user "johndoe" will be removed from the group "developers."

Example 2: Verifying Current Groups

Before removing a user from groups, it's good practice to check their current group membership

groups johndoe

Sample output

johndoe : johndoe developers docker sudo

To remove "johndoe" from the "docker" group while keeping them in "developers" and "sudo"

sudo usermod -G developers,sudo johndoe

Example 3: Editing the /etc/group File

Suppose we want to remove the user "bobdoe" from the group "engineering." Open the /etc/group file

sudo nano /etc/group

Find the line containing the group "engineering" and edit it to remove "bobdoe" from the list of users. Change this line

engineering:x:1000:alice,bobdoe,carl,dave

To this

engineering:x:1000:alice,carl,dave

Save the changes to the /etc/group file and exit the text editor. The user "bobdoe" will be removed from the group "engineering."

Verification

After removing a user from a group, verify the change was successful

groups username

Or check the specific group membership

getent group groupname

Best Practices for Removing Users from Groups

When removing a user from a group, follow these best practices to ensure the process is carried out safely and efficiently

  • Verify that the user no longer needs access to the group Before removing a user from a group, verify that they no longer need access to the resources associated with that group.

  • Use gpasswd for single group removal The gpasswd -d command is the safest method for removing a user from a single group without affecting other group memberships.

  • Document the changes Make a note of the user and group involved, as well as the reason for the change.

  • Test the changes After removing a user from a group, test to ensure that the user no longer has access to the resources associated with the group.

  • Regular maintenance Regularly review and update user groups as part of system administration to maintain security.

Conclusion

Removing a user from a group in Linux is a straightforward process with multiple methods available. The gpasswd -d command is the most reliable method for removing users from individual groups, while usermod -G allows for comprehensive group management. Following proper verification steps and best practices ensures secure and effective user group management in Linux systems.

Updated on: 2026-03-17T09:01:38+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements