In Linux, user management is an essential task & one important aspect of this task is managing user groups. User groups help to organize users with similar access privileges, making it easier to manage permissions & access to files. However, sometimes it may be necessary to remove a user from a group. In this article, you will learn about Linux groups & how to remove a user from a group with different methods along with practical examples.
Key Takeaways
- Getting an overview of Linux groups with different types.
- Learning to remove a user from a group using 4 methods via CLI (using commands gpasswd, deluser, usermod, and by editing /etc/group file).
- Gathering knowledge with practical examples on how to add a user to groups, change the user’s primary group, change group ID & name, & Delete a group in Linux.
Requirements
- Need to be a root user or have root/sudo privileges to add, or remove a user from a group in Linux.
Process Flow Chart
Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS
What are Groups in Linux?
In Linux, a group is a collection of users who share common permissions and access rights to files, directories & other resources on the system. It is a vital part of the OS & a powerful way to manage file access in the system. Furthermore, having groups help you to set permissions for the group level rather than setting them individually for each user.
You can easily view which groups a user belongs to on your system by using a simple command:
groups
The output image shows the “groups” list that the currently logged-in user is a member of. You can also view the list of groups that the other users are members of by the following command:
groups tom
Check out the following command output image: The command output shows a group list where user ‘tom’ is a member.
Following are the types of groups generally found on Linux.
A. Primary Group
- By default, when a user is created it is assigned to a group of the same name as the username that is the primary group.
- The primary group is important as it determines the ownership of files & directories that the user creates.
- A user can only be a member of one primary group at a time. Moreover, this primary group membership is defined in the /etc/passwd
Check out the following output image: Here, the cat command is showing the user tom’s /etc/passwd file, where you can tom’s user ID (1005) & primary group ID (1006).
B. Supplementary Group
- A user can also be a member of one or more supplementary groups, in addition to their primary group.
- These groups’ membership isn’t defined in the /etc/passwd file rather you can find them in the /etc/groups file with other groups.
- Moreover, they are used to grant additional permissions or access to specific resources to a user.
C. Special Group
- Special groups are predefined groups that have specific functions and permissions in
- Some special groups are “wheel”, which grants users administrative privileges, and “nobody”, to run processes with restricted permissions.
Watch 4 Methods to Remove User from Group in Linux
4 Methods to Remove User from Group in Linux
In Linux, you can easily & quickly remove any user from any supplementary groups using the Command Line Interface (CLI). Since several commands can be used to remove a user from a group, I will discuss them in different methods in the following sections. Moreover, I will also explain how you can edit the /etc/group file to remove a user from a group.
Method 01: Using the gpasswd Command to Remove a User from Group
The first command I will use to remove a user from a group is the gpasswd command. It is a simple command tool that you can use with ease. Just type the command followed by the –delete or -d option of the command & then the user name (to be removed) & group name (to remove from). Follow the below steps to see practically:
Steps to Follow >
➊ At first, open your ubuntu terminal application.
➋ Then, type the following command & press ENTER to view the groups list user tom is assigned to:
groups tom
- groups: Lists groups that a user belongs to.
- tom: User name.
➌ Next, remove the user from the ‘read’ group by typing the below command:
Command Syntax >
sudo gpasswd option user_name group_name
sudo gpasswd -d tom read
- sudo: Permits administrative privileges.
- gpasswd: Used to manage groups & passwords.
- –delete or -d: Command option to delete the specified argument.
- tom: User name.
- read: Group name.
➍ At this point, type the currently logged-in user password & press ENTER.Upon execution, the command removes the user ‘tom’ from the group ‘read’.
➎ Afterward, check if you have successfully removed the user from the group by typing the groups command again:
groups tom
➏ Finally, press the ENTER button. Check the command output, in the tom user’s group list the ‘read’ group is no longer included.
Method 02: Using the deluser Command in Linux
In this example, I will use the deluser command to remove a user from a group. Just type the command followed by the user name (to be removed) & group name (to remove from). Go through the below steps to check out the process:
Steps to Follow >
➊ First, open your ubuntu terminal application.
➋ Afterward, type the following command to remove the user tom from the ‘logs’ group:
Command Syntax >
sudo deluser user_name group_name
sudo deluser tom logs
- sudo: Permits administrative privileges.
- deluser: Removes a user or group from the system.
- tom: User name.
- logs: Group name.
➌ Finally, press the ENTER key. The command removes the user ‘tom’ from the ‘logs’ group.
- How to Delete User Account In Ubuntu? [2 Methods]
- Delete a User from an Ubuntu Server in Just 3 Steps
Method 03: Using the usermod Command to Remove User form Group in Linux
Now let’s use the usermod command to remove a user from a group. Well, It is not that simple to use like the gpasswd & deluser command. To remove the user using this command, you have to learn the names of all groups it is a member of. And then, you have to specify them (excluding the ones you want to remove the user from) as parameters (separated by a comma & no spaces between them) with the command.
Thus, if the user is currently a member of a group that is not listed with the command, the user will be removed from the group. Follow the below steps with command syntax to see how it works practically:
Steps to Follow >
➊ Start by opening your ubuntu terminal application.
➋ Then, type the following command & press ENTER to view the groups list user tom is assigned to:
groups tom
- groups: Lists groups that a user belongs to.
- tom: User name.
➌ Next, type the following command with group names excluding the one (in this case logs) you want to remove the user tom from:
Command Syntax >
sudo usermod option group_names user_name
sudo usermod -G tom,sudo,read tom
- sudo: Permits administrative privileges.
- usermod: Modifies assigned attributes to the user.
- -G: Option to specify that the modification is related to the user’s group membership.
- tom,sudo,read: Group names.
- tom: User name.
➍ Now, press the ENTER button.
➎ Afterward, check if you have successfully removed the user from the group by typing the groups command again:
groups tom
➏ Finally, press the ENTER button again. First, from the groups command output you can see the user tom is a member of groups tom, sudo, read & logs. Next, execute the usermod command with group names (tom, sudo, read) excluding the group (logs) you want to remove the user from. Afterward, check the list again using the groups command. And you can see user tom is no longer a member of the ‘logs’ group.
Method 04: Remove User from Group By Editing /etc/group File in Linux
Each group has a unique ID listed in the /etc/group file followed by the group name and members. You can open the file in any editor & just manually remove the user from any group. Proceed according to the following steps to check out how to do it:
Steps to Follow >
➊ First, open your ubuntu terminal.
➋ Then, open the /etc/group file in the nano editor:
sudo nano /etc/group
- sudo: Permits administrative privileges.
- nano: Text editor for Unix-like
- /etc/group: File that contains info about groups on the system.
And if you scroll down to the bottom, you will find all the users’ primary & supplementary group info, just like in the following image. ➍ Afterward, remove the user from the group. I removed the user ‘tom’ from the ‘read’ & ‘logs’ groups.
➎ After that, save the changes (CTRL+S) & exit (CTRL+X) the nano editor.
➏ At this point, check if you have successfully removed the user from the group by typing the groups command:
groups tom
➏ Finally, press the ENTER button. From the output image, you can see the user ‘tom’ is no longer a member of the ‘read’ & ‘logs’ groups.
Comparative Analysis of Methods
Since this article introduces multiple methods for a single task, you might get confused about which one to pick. Therefore, in this section, I am presenting a comparative analysis of the methods for your convenience. You can learn the advantages and disadvantages of the available methods and choose which is the best for you.
Methods | Pros | Cons |
---|---|---|
Method 01 | Simple & straightforward that only requires group name & user name as parameters with the option –delete. | The command requires sudo privileges to execute. |
Method 02 | Easy-to-use command, that removes the user from the specified group. | The command requires sudo privileges to execute. |
Method 03 | Provides the ability to modify secondary groups for the user. | Users who are only members of a single group can not be removed using this command. |
Method 04 | Provides fine-grained control over the group membership of users. | Time-consuming & any mistakes while editing can result in system issues. |
Each & every method has its pros & cons. You can choose any one of them depending on your expertise in that particular command. Anyway, if I had to then I would suggest using method 2, where I used the deluser command. Cause it is the simplest method among them all.
Complementary Information
Besides, knowing how to remove users from groups, you will find the below information helpful.
How to Add a User to Group in Linux
You can easily add users to a group in Ubuntu using the gpasswd or usermod command. Here, I will show how to add a user to groups using both of them. Follow the below steps to check practically.
Steps to Follow >
➊ First, open the Ubuntu terminal.
➋ Then, type the following command into the command prompt to use gpasswd to add the user ‘tom’ to the group ‘logs’:
sudo gpasswd --add tom logs
- sudo: Grants administrative privileges.
- gpasswd: Used to manage groups & passwords.
- –add: Command option to add.
- tom: User name.
- logs: Name of the group.
➌ Press the ENTER key. Upon execution, the gpasswd command is adding the user ‘tom’ to the ‘logs’ group.
➍ Afterward, type the following command into the command prompt to use the usermod command to add the user ‘tom’ to the existing groups ‘logs’ & ‘read’:
sudo usermod -aG logs,read tom
- usermod: Modifies assigned attributes to the user.
- -aG: This option of the usermod command adds the user to an additional group.
- logs,read: Group names.
- tom: User name.
➎ Then press the ENTER button.
➏ At this point, check if you have successfully added the user to the groups by typing the groups command:
groups tom
➐ Finally, press the ENTER key again. After the execution, the usermod command adds the user ‘tom’ to the existing ‘logs’ & ‘read’ groups. Since the command shows no output messages, verify the addition with the groups command.
How to Change the Primary Group of User in Linux
You can not delete the primary group or remove the user from that group without deleting the user first. However, you can change the GID or name of the primary group using the usermod command. Follow the following steps to learn how to do that.
Steps to Follow >
➊ First, open the terminal application on your Ubuntu system.
➋ Next, type the following command in the command prompt to view user “tom’s” primary group ID:
id tom
- id: Finds the identity of the specified USER.
- tom: User name.
➌ Press ENTER & type the next command as written followed to change the primary group’s name:
sudo usermod -g new_tom tom
- sudo: Permits administrative privileges.
- usermod: Modifies assigned attributes to the user.
- -g: This option specifies that the modification is related to the user’s primary group.
- new_tom: New User name.
- tom: User name.
➍ Then press ENTER & see the changes by executing the id command again:
id tom
After executing the command, you can see the new name of the primary group (new_tom) with the id command. Thus, the command has successfully changed the primary group’s name (the previous name was ‘tom’).
- Remove User from a Group Using “gpasswd” Command in 3 Steps
- How to Delete User and All Files in Ubuntu [3 Methods]
How to Change Supplementary Group ID & Name in Ubuntu
You can easily change any supplementary group name or group ID using the groupmod command. For that, use the command options –new_name & –gid respectively. Follow the following steps.
Steps to Follow >
➊ Open your terminal.
➋ Type the following command to change the group name:
Command Syntax >
sudo groupmod option New_Group_Name Old_Group_Name
sudo groupmod -n write read
- sudo: Permits administrative privileges.
- groupmod: Used to modify group information such as name, and ID.
- -n or –new_name: Option that specifies the modification is related to the group name.
- write: New group name.
- read: Old group name.
➌ Press Enter & type the following command to verify the changes:
getent group write
- getent: Looks into the administrative database that is given as its argument.
- group: Lists groups that a user belongs to.
- write: Group name.
➍ Tap ENTER & type the below command to change the group’s ID (GID):
Command Syntax >
sudo groupmod option New_Group_ID Group_Name
sudo groupmod -g 1011 write
- -g: Option to change group ID to specified GID.
- 1011: New GID.
- write: Group name.
➎ Then, press ENTER & execute the following command to verify the changes again:
getent group write
The first command changed the group name from “read” to “write”. Since I can find the ‘write’ group’s info from the database, it’s verified that the name change was successful. Next, the command changes GID, and as you can see the last command output in the image shows the new GID (1011).
How to Delete a Group in Linux
You can easily delete a group in Ubuntu by using the groupdel command. Here, I will remove the group named “read”. To do so follow the below process.
Steps to Follow >
➊ First, open the Ubuntu terminal.
➋ Then, type the following command into the command prompt to search for a group you want to delete:
getent group read
- getent: Looks into the administrative database that is given as its argument.
- group: Lists groups that a user belongs to.
- read: Group name.
➌ Now, press the ENTER button.
➍ Afterward, type the following command to delete the group, here I will delete the ‘read’ group:
sudo groupdel read
- sudo: Grants administrative privileges.
- groupdel: Deletes a group.
- read: Name of the group.
➎ Finally, press the ENTER key. In the above image, you can see that I have successfully deleted the group named “read”.
Common Challenges That May Arise While Removing User from a Group in Linux
While trying to remove a user from any group, some common challenges may arise. This results in some errors in the output & you are unable to remove the user from the specified groups. Even though these errors are minor, they could be troublemakers. Check out the following two cases that may take place:
A. Linux Group Does Not Exist in /etc/group
A common mistake that may happen while trying to remove a user from a group is that the group may not exist. Moreover, you can also make typing errors while mentioning the group name. The image is showing the case, when you try to add a user to a group that does not exist. As a solution, try to view the groups list a user is a member of before removing it from any of them. For that, use the groups command with username to check the user’s groups list.
B. The User Is Not a Member of the Group in Linux
Another issue that can arise is that you are trying to remove a user from a group, but the user is not even a member of the group. See the image, where I tried to remove the user ‘munny’ from the ‘logs’ group & the output is showing that the user is not even a member of the group.
As a solution, use the groups command in this case too. Since, the command with the user name as an argument displays the list of groups to which that user belongs to. Therefore, use the group command for avoiding unnecessary dilemmas.
Conclusion
In conclusion, in Linux, removing a user from a group is a simple yet important process that can be accomplished using different ways. Throughout the article, I explained each way with practical examples of how to remove a user from the group in Linux. Moreover, you can find some additional information about Linux groups in the complementary information section. Hope, this writing helps you in understanding & managing Linux groups.
People Also Ask
How can I remove user from group in Linux?
How to remove user from group command?
How to remove user ID and group in Linux?
How do I remove a user from a group in Ubuntu?
How to use userdel command in Linux?
How to delete a user?
What is groupmod command in Linux?
How to find out user group in the Linux command?
Related Articles