How to Remove Users from Groups in Linux?
Last Updated :
15 Nov, 2023
Groups in Linux are an important part of organizing the system's access control. Creating separate groups for separate types of roles of users allows the administrator to manage the access control of the Linux system efficiently.
It is an essential skill to understand how to add, remove, and update users in a group from Linux. In this article, we shall learn various methods for removing a user from a group in Linux using different command line tools. We shall use Kali Linux for demonstration in this article.
Pre-requisites
- A Linux machine with root privileges.
- Basic understanding of Linux terminal and commands.
Method 1: Using the `deluser` command
The deluser is a Linux command for deleting users from systems/groups. The syntax of the command is :
deluser <username> <groupname>
Step 1: Check the groups of the user (optional)
First, we shall verify the groups of the user for demonstration purposes. This can be done with the groups command.
groups <username>
In this article, we shall use the 'dummy' user as an example and delete it from different groups.
Checking groups of the user dummyAs we can see the user belongs to three groups. In the next step, we shall remove the dummy user from users group.
Step 2: Removing the user from a group
We can modify the userdel command for this purpose:
deluser dummy users
Output:
Deleting dummy from users groupAs we can see in the response message, the user is removed from the group.
Step 3: Verifying the deletion of the user from the group.
We can verify the result from the same method as used in Step 1.
groups dummy
Verification of user deletion from groupAs we can verify, the user is removed from the group users.
Method 2: Using the gpasswd command
There is another command, the gpasswd command, to remove users from a group. The syntax of the command is simple:
gpasswd [options...] <groupname>
Now, to delete a user from a group, we use the -d option followed by the username.
gpasswd -d <username> <groupname>
Step 1: Deleting a user from a group using the user command.
We shall use the same user 'dummy' as in the previous method. But, this time we shall delete the user from the 'video' group. The command we need is modified as follows:
gpasswd -d dummy video
We can recheck the existing groups using the same method as in Method 1:
Deleting a user from the group using gpasswd commandAs we can see, the user was a member of the video group and then, we deleted it using gpasswd.
Step 2: Verifying the result (Optional)
We can again verify using the groups command as before:
groups dummy
Verifying the resultsAs we can verify here, the dummy user is no longer a member of the video group.
Method 3: Editing the /etc/group file
In this method, we shall edit the /etc/group file, which contains information regarding the users, to remove a specific user from the group.
Step 1: Open the file
We can open the file using any editor with root/sudo permissions but, for this tutorial we are using the nano editor.
nano /etc/group
This will open the file. Then, you need to navigate to the line beginning with your group's name. For example, we are removing the dummy from the sudo group so, we shall edit the line beginning with 'sudo'.
Editing the sudo group from /etc/group file to remove dummy userStep 2: Removing the dummy from the sudo group
Now we need to edit the line containing sudo group and remove the user mentioned in the list of users seperated by ','. The syntax of this file is:
<groupname>:x:<GID>:[users...]
The last column/field contains the usernames seperated by a ','. So, in order to remove a user from the group, we need to delete that user's name from this field in group file. In our case the user is dummy so, we shall remove its name.
The file should look like this after edition:
Edited file.Now, press Ctrl+S for saving the file and Ctrl+X for exiting the editor. The dummy user is now successfully removed from the sudo group.
Step 3: Verification
We can verify the same using the same verification as previous methods:
groups dummy
This should not show sudo anymore.
Group of dummy userAs we can see, dummy is no longer a member of sudo group.
Conclusion
In this article, we learned three different methods for removing a user from a group in linux. First we used the deluser command and removed a user from a group. Then, we saw how to use the gpasswd command for the same purpose. Lastly, we used a more direct approach and edited the /etc/group file to remove users from a group.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
Linux/Unix Tutorial Linux is one of the most widely used open-source operating systems. It's fast, secure, stable, and powers everything from smartphones and servers to cloud platforms and IoT devices. Linux is especially popular among developers, system administrators, and DevOps professionals.Linux is:A Unix-like OS
10 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read