Remove Users from Linux Groups with usermod, gpasswd, and /etc/group
This tutorial explains how to delete a user from one or multiple Linux groups using the usermod and gpasswd commands, as well as by manually editing the /etc/group file, with step‑by‑step examples and complete command output.
Linux users belong to a primary group and optional secondary (supplementary) groups, defined in /etc/group. This guide shows three ways to remove a user from groups: the usermod command, the gpasswd command, and manual editing of /etc/group.
Creating a test user and groups
First, a test user is created, which automatically creates a primary group with the same name: $ sudo useradd -m testuser A password is set for the new account: $ sudo passwd testuser Two supplementary groups are added:
$ sudo groupadd testgroup1
$ sudo groupadd testgroup2The user is then added to the new groups and to root:
$ sudo usermod -a -G root testuser
$ sudo usermod -a -G testgroup1 testuser
$ sudo usermod -a -G testgroup2 testuserViewing /etc/group confirms the memberships.
Finding a user’s groups
Two commands provide the same information:
$ groups testuser
$ id -nG testuserRemoving a user from groups with usermod
The usermod command can replace the list of supplementary groups for a user. To keep the user only in the root group (removing it from testgroup1 and testgroup2), run: $ sudo usermod -G root testuser Result:
$ groups testuser
testuser : testuser rootMultiple groups can be specified as a comma‑separated list, e.g.:
$ sudo usermod -G root,testgroup1 testuserRemoving a user from groups with gpasswd
The gpasswd -d command deletes a user from a specific group. Examples:
$ sudo gpasswd -d testuser root
Removing user testuser from group root
$ groups testuser
testuser : testuser testgroup1 testgroup2
$ sudo gpasswd -d testuser testgroup1
Removing user testuser from group testgroup1
$ groups testuser
testuser : testuser testgroup2Manual removal by editing /etc/group
Open /etc/group with a text editor and delete the user name from the desired group lines. After editing, the relevant lines look like:
testuser:x:1001:
testgroup1:x:1002:
testgroup2:x:1003:After saving and restarting (or re‑reading the file), the user is removed from those groups:
$ groups testuser
testuser : testuser rootConclusion
The tutorial demonstrates how to remove a Linux user from groups using usermod, gpasswd, and by directly editing /etc/group. These commands work on any Linux distribution such as Ubuntu, CentOS, or Fedora.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
