Linux is a multiuser operating system. Therefore, you may want to delete multiple users along with creating them. Unfortunately, Linux does not offer any built-in command to delete multiple users at once. There is only userdel command available that can remove one user account at a time. However, you can utilize the bash scripting technique to automate the deletion system and remove multiple users simultaneously from your machine. In this article, you will learn all about how to delete multiple users in Linux.
- Writing a bash script for deleting multiple users.
- Deleting user accounts with the home directory using the userdel command.
- Configuring and running bash script in Linux.
Process Flow Chart
Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS
Watch 2 Steps to Delete Multiple Users in Linux
2 Steps to Delete Multiple Users in Linux
Since Linux does not offer any command to delete multiple users, you can use a bash script to delete multiple users. In this section, I will create a bash script using the userdel command within a for loop to delete multiple users. To make the script interactive, I will also apply the if condition on the userdel command.
Step 1: Preparing a Script to Delete Multiple Users in Linux
To prepare a script that deletes multiple users in a loop follow the instructions below.
Steps To Follow >
- At first, open a terminal window by pressing CTRL+ALT+T.
- Then, create a bin folder in your home directory by typing the following command.
However, you can skip this step if the directory is already created.
- After that, create a bash script file inside the bin directory with the command below.
- Now, write the following script in the delete_users.sh file for deleting multiple users in loop.
#!/bin/bash read -p "Enter number of Users to be deleted:" num echo "------------------------------------------------------" for (( i=1; i<=$num; i=i+1)); do read -p "Enter Username no.$i: " uname if sudo userdel -r $uname ; then echo "------------------------------------------------------" echo "Successfully deleted User: $uname." echo "------------------------------------------------------" fi done
- To save and exit from the script, press CTRL+S and CTRL+X respectively.
- Now, Type the following to make the script executable for the current user in your system.
chmod u+rwx bin/delete_users.sh
- Finally, restart your system to add the newly created bin directory to the $PATH variable by typing the command below.
Restarting the system by default runs the .profile script which adds the user’s private bin directory to $PATH and makes the files inside the bin directory accessible for every shell session.
Step 2: Executing Script to Delete Multiple Users in Linux
Upon rebooting the system, you will be able to run the “delete_users.sh” script from any path under the current user account. To learn how you can execute the script for deleting multiple users follow the process below.
Steps To Follow >
- At first, press CTRL+ALT+T to open the Ubuntu Terminal.
- Run the previously written script by simply typing the file name.
- Type the number of users you want to delete and hit ENTER. For example, I will try to delete 3 users from my system. Therefore, I will type “3” and press ENTER.
- Now, type each username in response to the prompt message to delete and press ENTER.
- Finally, Input the password of the sudo user to run the userdel command written inside the script. In the above image, you can see that I have successfully deleted 2 users from my machine using the “delete_users.sh” script. I also stopped the running script by pressing CTRL+Z which allowed me to delete only 2 users even though initiated the deletion of 3 users.
- How to Delete User Account In Ubuntu? [2 Methods]
- Delete a User from an Ubuntu Server in Just 3 Steps
- Remove User from a Group Using “gpasswd” Command in 3 Steps
In addition to the specified method of deleting multiple users in Linux, you might find these supplementary tasks described here helpful as well.
Delete User Without home directory in Linux
You can choose to delete a user account without its corresponding files in your system. To achieve this, you will have to delete the user without its home directory. In this example, I will delete the “UserC” without its files. You can do the same by following the steps below.
Steps To Follow >
- At first, open the Ubuntu terminal.
- Then, type the following command into the command prompt.
sudo userdel UserC
- Press ENTER.
- Finally, provide the password of the currently logged-in user.
- Now, to check if the UserC home directory still exists, type the following and press ENTER.
In the above image, you can see that I have deleted the user account “UserC” from my system but without the home directory of UserC.
Delete Multiple Users From a Group in Linux
In Linux, there is no default command for removing multiple users from a group. But you can always edit the /etc/group file and remove multiple users from it. To do so, all you have to do is open this particular file in a text editor and remove the user names within your desired group. In this example, I will remove the users “UserA” and “UserB” from the group “multi_users”. You can do the same by following the process below.
Steps To Follow >
- Open the Ubuntu terminal.
- Then, type the following command in the command prompt.
sudo nano /etc/group
- Press the ENTER button.
- Then, give the password of the current user and hit ENTER.
- Erase the user names “UserA” and “UserB” from the group “multi_users”.
- To save and exit from the etc/group file, press CTRL+S and CTRL+X respectively.
- Now, check the group members by typing the command below and then hit ENTER.
In the above image, you can see that I have deleted the users “UserA” and “UserB” from the group “multi_users”. Furthermore, you can view the updated group members by combining the less and grep command for the group.
sudo less /etc/group| grep "multi_users"
In this article, I have illustrated an automated way of deleting multiple users in Linux. As in Linux, it is not possible to apply a single command and remove multiple users simultaneously, therefore I have introduced a bash script to automate the process. Apart from this major task, I have also demonstrated some extra information to aid your experience with multiple users and groups. I hope this article will help you with your journey in Linux and make you a power user moving forward.
People Also Ask