Remove user from group in Ubuntu

Remove User from Group in Ubuntu


Table of Contents

Here's a common situation Ubuntu sysadmins often face. You added a user to a specific group. Later, you realized that it was the wrong user. So now you have to remove the user from the group.

To do that, use the following command:

sudo deluser username groupname

Don't worry. It won't delete the user or the group. It will just remove the user from the specified group.

There is also the more standard usermod command but you need to provide all the existing group names the user belongs to.

sudo usermod -G group1,group2,group3 user_name

The deluser command is actually a script that is preinstalled in Ubuntu. You may not find it on all the Linux distributions. It uses the usermod command underneath.

Let me share both methods in detail.

To remove a user from a group, you must have sudo access or switch the user to root.

You need to know the group name from which you are removing the user. You must know the user name, of course.

You can use the groups command to see the groups a user account belongs to:

groups username

And when you have the desired group name, you can remove the user in this manner:

sudo deluser username groupname

If the command runs successfully, you will see an output similar to this:

root@learnubuntu:~# deluser abhi admin
Removing user `abhi' from group `admin' ...

You can verify that the user has been removed from the group in question by checking its groups:

groups username
Removing user from group in Ubuntu

Remove user from group using usermod command (not recommend)

Though usermod is the standard command, I don't recommend using it as it complicates things unnecessarily.

You first need to get all the groups the user belongs to:

groups username

And then, copy the groups but remove the group you don't want the user to belong to.

Now, you re-add the user to multiple groups it belonged to earlier by providing the group names separated by commas.

sudo usermod -G group1,group2,group3 user_name
Removing user from group in Ubuntu

As you can see, it's not as simple as the deluser command.

Troubleshooting tips

You may encounter a few errors while deleting users from the group.

You may not remove the user from their primary group

Every user has a primary group. In Ubuntu, when you create a user, a primary group with the same name as the user is also created.

If you try to use the deluser command to remove the user from its primary group, it will show you an error:

root@learnubuntu:~# deluser abhi abhi
/usr/sbin/deluser: You may not remove the user from their primary group.

The usermod command may not show an error but it won't remove the user from its primary group.

So, what if you really want to remove the user from its primary group? Well, a user cannot exist without a primary group. So, you have to change the user's primary group and then remove it from the previous primary group.

usermod -g new_primary_group username

The group does not exist

This is quite self-explanatory. You are trying to remove the user from a group that doesn't exist.

root@learnubuntu:~# deluser abhi notice
/usr/sbin/deluser: The group `notice' does not exist.

In this case, make sure that you are using the group name correctly and that there are no typos. You can always check the groups a user belongs to with:

grpups username

The user is not a member of group

Another self-explanatory error. You are trying to remove the user from a group but the user doesn't belong to this group.

root@learnubuntu:~# deluser abhi admin
/usr/sbin/deluser: The user `abhi' is not a member of group `admin'.

Again, make sure there are no typos in the group name. Verify which groups the user belongs to with:

groups username

As you can see, the deluser is such a user-friendly command. It's good to see it included by default in a user-friendly Linux system like Ubuntu.