From its core, Linux is built to have multiple users, and dividing them into certain user groups is one of the best ways to control their access to certain parts of the system.
For example, you can add users to sudo group, so they can access sudo command.
But what if you want to remove a user from added group? Say, a user doesn't need to be sudoer anymore?
Well, in this tutorial, I'm going to show you multiple ways of removing users from group in Linux.
How to remove a user from group in Linux
In this guide, I am going to show you 3 ways to remove a user from the user group in Linux:
- Using the gpasswd command
- Using the deluser command
- By editing the /etc/group file
So let's start with the first one.
1. Using the gpasswd command
The gpasswd command in Linux is used to manage group passwords and its users.
Which means, it can also be used to remove users from group.
But first, let's check the user group. To do so, use the groups command in the following manner:
groups <username>
To remove a user from the group, you'd have to use the gpasswd command in the following manner:
sudo gpasswd --delete <username> <groupname>
So let's say I want to remove my user sagar
from the Tux
user group, then, I'd have to use the following:
sudo gpasswd --delete sagar Tux
To verify the process, you can check the groups associated to the user:
And as you can see, my user is no longer associated to the Tux
group!
2. Using the deluser command (For Ubuntu users only)
On Ubuntu, you get a special command deluser
which is specifically built to provide a more user-friendly way of removing users.
To use the deluser to remove user from the group, it needs to be executed in the following manner:
sudo deluser <username> <group>
So let's say I want to remove user sagar
from the IF
group, then I'd have to use the following command:
sudo deluser sagar IF
Pretty simple! Isn't it?
3. By editing the /etc/group file
In previous methods, you were given commands which would edit the /etc/group
file for you, but here, you'd be doing that manually!
First, open the /etc/group file using the following:
sudo nano /etc/group
Press Ctrl +w
and type the group name:
Once you find the group, you'd find that your username is appended to the group name.
So just remove the username as shown:
Finally, save changes and exit from the nano text editor.
Now, if you check, your user will be no longer associated to the group:
Here you have it!
It is time to embrace the terminal
The command line is the most efficient way to perform operations on your Linux system.
But most of the users still hate the terminal because of the way it looks, and typing commands looks too complex.
And at It's FOSS, we firmly believe that Linux users should embrace the terminal and this is why we came up with a series of tutorials to show you how to perform basic tasks:
I hope you will find this guide helpful!