How to Remove a User From a Group in Linux
There are multiple different methods and commands that can be used. If you are wondering how to remove a user from a group in Linux you have options to pick from. I’ve used the following Linux distros/versions to test these commands:
- Ubuntu 18.04.3
- Fedora 31
- CentOS 8.1.1911
If you just want something that is easy and works on all of these, use the first command shown below ‘gpasswd -d user group’.
In general, after making changes to a group the user will need to log out and back in again. The effect won’t go in until they log out. At least, it won’t afffect their currently logged in session. If they login using a second terminal at the same time, that should be updated.
NOTE - We assume you’re running as root. If not you are going to want to use sudo with all of these commands.
Use this command to remove a user from a group in Linux:
gpasswd -d user group
This worked great on all three systems, Ubuntu, Fedora, and CentOS.
This worked great on Ubuntu. It didn’t work on CentOS and Fedora. The command didn’t exist on either of those distros. People also report this working on Debian which would make sense.
deluser user group
The Manual Way - Use vi
You can always just edit the group file manually using vi ( or emacs, pico, nano, ed … ). It should go without saying that this works on all distros. It even works on non-linux systems like Solaris.
Edit it like this:
To remove user2 from test-group1 change this line:
To look like this:
How to Remove a User From a Group in Linux? Use vigr
With a special tool vigr:
This works pretty much the same as manually using another editor to edit the file. The difference is that you don’t need to specify the file name. This command will open the /etc/group file with the editor of your choice. It also is supposed to set locks to prevent corruption.
We tested this out. It looks like all three systems ( Ubuntu, CentOS, and Fedora ) have this command. We didn’t actually use it to remove any users though.
This is kind of the round about way. You specify all of the groups that you want the user to be in and exlude the group that you don’t want the user to be in. If you mess this up, it could have unintended side effects.
We tested this on all three systems and it works exactly as expected ( Ubuntu, CentOS, and Fedora ).
Remove from all except the listed groups:
usermod -G docker,mysql user
Remove a user from all groups except thier own:
usermod -G "" username