Situatie
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.
Solutie
Pasi de urmat
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>
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.
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
Leave A Comment?