How to Remove All Users From a Group in Linux?
Last Updated :
04 Nov, 2023
A group is a collection object in the Linux operating system, which associates certain properties and access control to all its members. It is an efficient way for a system administrator to manage Access Control for its users.
Groups are an important tool for a system administrator to maintain many aspects of the system such as defining roles for users efficiently, defining Access control, etc. Thus, knowing how to add, remove, and update users in a group is an important skill that a user/administrator should have.
In this article, we shall learn different methods to remove all members from a group in a Linux system, without deleting the group itself.
Pre-requisites
- A Linux machine with root privileges.
- gpasswd command installed in the system.
- Basic understanding of Linux terminal and shell scripting.
Removing all users from a group using the gpasswd command
The gpasswd command is used in Linux systems for managing groups. We can use this command to perform various purposes such as editing group properties, removing users, etc. Its syntax is:
gpasswd [options...] <groupname>
We need to delete users from this group so, we can use the -d option followed by the username(s):
gpasswd -d <username(s)> <groupname>
Step 1: Checking all the members of the group(Optional)
We will verify the present users before removing them from the group. In this article, we shall use the group geek. Now, the listing of users can be done by the following command.
getent group geek
from
This command displays information about the group from the /etc/group file and displays the lines specific to the given group. You can replace the 'geek' username with the group you need to remove all users from.
Output:
Checking all users present in the groupStep 2: Create a script to remove all users from a group.
Now, we shall remove all users from this group. The process is not straightforward as there is no command to remove all users at once from a group. So, we shall create a script that fetches all users of the group and deletes them, one at a time. We shall use the for loop in the bash script to achieve this goal.
Create a bash script and add the following code to it:
#!/bin/bash
group=$1
for user in $(getent group "$group" | cut -d: -f4 | tr ',' ' '); do
gpasswd -d "$user" "$group"
done
Code Explanation:
- Here, we take the group name as the first positional argument from command execution and save it in a variable.
- Then, we start a for loop which iterates over the given object.
- The object passed to the for loop, $(getent group "$group" | cut -d: -f4 | tr ',' ' ') fetches the usernames from the output of the getent command as shown in step 1.
- Then, it fetches the comma-separated usernames using the the cut command.
- Finally, it replaces the ',' with a ' ' (blank space) to make the object iterable.
- Doing this is necessary as a loop can iterate over an array only.
- Then, (assuming you are using the root user, if not then add the prefix sudo) we run the gpasswd command using the above-mentioned syntax and delete users one at a time.
Step 3: Make the script file executable.
Now, you have to give the file execute permissions in order for it to run. We have saved the file as the name remove_all_users_group.sh, you can use any name you want. To provide the executable permissions, type the following command.
chmod +x <your script file's name>
Step 4: Executing the script.
Now, to execute the script, you need to pass the group name as a positional argument to the script like the following:
./remove_all_users_group.sh geek
Then, press enter,
Removing all users from the group using a scriptOn successful execution, the gpasswd command will give the success alert for each user present.
Step 5: Verifying the execution(Optional)
You can verify whether all users are removed from the group or not with the same intent command as used in step-1.
getent group geek
Output:
Verifying the results.As we can verify from this, there is no user present in the group anymore.
Method 2: Editing the /etc/group file
In this method, we shall learn how to remove all the users from a group by editing the /etc/group file. This file contains details of all groups. So, we shall use the following approach.
- Open the file in an editor with root permissions.
- Find the line that contains the users of the group we need to edit.
- Remove all usernames from the line without removing the entire line.
- Save the changes and verify the result.
Step 1: Opening the /etc/group file
We shall use the nano editor to open the file in this example, however, you can use any editor of your choice.
nano /etc/group
This will open the file in the editor.
The /etc/group fileHere, we can see the geek group. We shall delete the users in the next step.
Step 2: Removing the user names from the geek group
As we can see the syntax in the group file is as follows
<groupname>:x:<GID>:[users...]
Now, the first 3 fields contain the information about the group such as group name, and group ID. The last field contains all its users' names separated by ','. We need to remove all the user names from this line without removing any delimiter ':'. You can do this by navigating to the line entry corresponding to your group and editing it like any normal file.
After editing, the line should look like this:
Edited /etc/group fileNow, you can save this file by pressing the following key combination in order:
- `Ctrl + S` (For saving the edition in the file).
- `Ctrl + X` (For exiting the editor).
Step 3: Verifying the results
We can verify the result of this method as we did of the previous method, by executing the following command:
getent group geek
Output:
Verification of removal of all usersAs we can see, there are no users present in this group anymore.
Conclusion
In this article, we explained how to remove all member users from a group in linux. We used the gpasswd command to remove all users. We created a custom script to remove all users from the group as gpasswd only allows one user removal at a time and it does not support the wildcard (*) syntax either.
Then, we learned how to directly edit the /etc/group file to remove all users from a group. Lastly, we verified the successful execution of our script.
Similar Reads
How to Remove Users from Groups in Linux?
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
4 min read
How to Add User to a Group in Linux
A group in Linux is a way to put users with similar access and permissions in a collection. By using groups, an administrator can define access control and permissions for all the users belonging to that group. Without groups, the administrator would have to define roles for individual users however
7 min read
How to Rename a Group in Linux?
Renaming a group in a Linux system is a straightforward but essential administrative task. If you're reorganizing your user management structure or enhancing security measures, this quick guide will walk you through the simple steps to rename a group. Linux provides a powerful command, 'groupmod' to
4 min read
How to add multiple users to a group at once in linux?
Managing user groups in Linux is an essential part of system administration. Often, you'll find the need to add multiple users to a specific group simultaneously. This article provides a detailed explanation of how to accomplish this task, covering every aspect, and includes examples for clarity. Un
5 min read
How to Check the Groups a User Belongs to in Linux?
This article shows how to check the groups a user belongs to in Linux operating systems. We introduce some concepts related to the topic and then describe how to do so using the groups command available on the terminal. Groups in LinuxAll Linux operating systems are designed as multi-user operating
8 min read
How to Change The Primary Group of a User in Linux?
In the field of Linux system administration, learning the modification of the primary group is essential. In this article, we are going to deliver concise details about user groups and how to manage them. What is a Primary Group?In Linux, every user is associated with a primary group. The primary gr
7 min read
How to Grant Admin Privileges to a User in Linux
Linux is an operating system that is open source operating system. There are many distros available in Linux distribution like Ubuntu, Kali Linux, Arch Linux, and Red Hat Linux. In this article, we will understand how to give admin privileges to non-admin users in the Ubuntu Operating system. Introd
5 min read
How to Remove Directory in Linux
In Linux, directories are used to organize files and other directories into a hierarchical structure. Just like folders in Windows, directories in Linux can contain files, other directories, and links. Removing directories in Linux is a common task that you might need to perform as you manage files
5 min read
How to Delete a Group in Linux | groupdel command
Group management is a crucial aspect of Linux system administration, and understanding how to create, modify, and delete groups is essential for maintaining a secure and organized environment. In this article, we will delve into the process of deleting a group in Linux using the 'groupdel' command.
3 min read
How to Create, Delete, and Modify Groups in Linux
If you're using Linuxâwhether on a personal system, a company server, or a shared networkâknowing how to manage groups is essential for handling multiple users efficiently. In simple terms, a group in Linux is a way to give shared access and permissions to multiple users without setting things up on
6 min read