A Linux user from a group refers to a user account that belongs to a specific group on a Linux operating system. Every user can associate with one or more groups, and every group can associate with several users. The group membership for a user is utilized to manage the accessibility of directories and files.
This tutorial will illustrate various methods to remove Linux users from the group.
- Prerequisite: Check the Group of a User
- Using the “gpasswd” Command
- Using the “deluser” Command
- Using the “/etc/group” File
Prerequisite: Check the Group of a User
To look for the user’s group, use the groups command with the username as follows:
$ groups roger
The output displays the user “roger” with the “itslinuxfoss” group.
Method 1: Using the “gpasswd” Command
The “gpasswd” command with the “d” option also removes the user from the group. It requires the linux user with the associated group name. For instance, specify “roger” as the linux user and “itslinuxfoss” as the group name in the below script:
$ sudo gpasswd -d roger itslinuxfoss
The output shows that the specific user “roger” has been removed from the group “itslinuxfoss”.
Method 2: Using the “deluser” Command
The “deluser” utility can also be used to delete a user from a specific group by following the syntax provided below:
$ sudo deluser <username> <groupname>
We have a user named “ilf” which belongs to three groups as shown below:
$ groups ilf
Let’s remove this user from the group named “itslinuxfoss” via the command:
$ sudo deluser ilf itslinuxfoss
To verify, the “groups” command can be used:
$ groups ilf
The output shows the user named “ilf” has been removed from the group “itslinuxfoss”.
Method 3: Using the “/etc/group” File
The “/etc/group” file contains information about all the groups on the system. Edit the file using any editor as follows:
$ sudo nano /etc/group
Locate the user that is supposed to be removed from the group:
However, this method is not recommended as it can be error-prone, and changes made to the file may not take effect until the system is rebooted.
Conclusion
Linux offers the “sudo gpasswd -d <user> <group>” or the “sudo deluser <username> <groupname>” commands to remove an existing user from a group. You can also delete a specific user record from the “/etc/group” configuration file. This article has demonstrated all possible methods for removing a user from the group.