FUNDAMENTALS A Complete Guide for Beginners
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 need to be a root user or have root/sudo privileges to remove users. You can also 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.
Process Flow Chart to Delete Multiple Users in Linux
[Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS]
Watch Video 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.
Furthermore, some extra commands such as read and echo are utilized for taking user input and displaying output messages in the terminal. You can follow the process below to do the same.
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:
- 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.
mkdir bin
EXPLANATION- mkdir: Creates a Directory.
- bin: User’s private bin directory.
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.
nano bin/delete_users.sh
EXPLANATION- nano: Creates/edits text files with Nano text editor.
- delete_users.sh: File for writing bash script of deleting multiple users.
- 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
EXPLANATION- read -p “Message” variable: Displays a message while taking input from the terminal and stores in the variable.
- echo: Displays a given text.
- $Variable: Returns the value stored in a Variable.
- for (( InitialValue; TestExpression; UpdateStatement)); do done: Runs the commands inside the do and done keywords in a loop from the InitialValue. Updates the initial value using UpdateStatement or comes out of the loop depending on the TestExpression.
- sudo: Grants administrative privileges.
- userdel: Deletes the specified user.
- if [ condition ]; then else fi: Checks a condition. If the condition is true, execute the command/commands after “then”. Otherwise, execute the command after “else”.
- 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
EXPLANATION- chmod: Changes folder permissions.
- u+rwx: Adds read, write and execute permissions for the current user.
- delete_users.sh: Bash script file for deleting multiple users.
- Finally, restart your system to add the newly created bin directory to the $PATH variable by typing the command below.
reboot
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.
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 a script for deleting multiple users, follow the process below:
- At first, press CTRL+ALT+T to open the Ubuntu Terminal.
- Run the previously written script by simply typing the file name.
delete_users.sh
- 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.NOTE: To terminate the running script you can press CTRL+Z and achieve the deletion of a lesser number of users than you initially wanted.
- 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
Complementary Information
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.
To delete a user without a home directory, follow the steps below:
- At first, open the Ubuntu terminal.
- Then, type the following command into the command prompt.
sudo userdel UserC
EXPLANATION- sudo: Grants administrative privileges.
- userdel: Deletes a user account.
- UserC: Username of the user to be deleted.
- 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.
ls /home
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 delete multiple users from a group, open the file given below in a text editor and remove the user names within your desired group:In this example, the users “UserA” and “UserB” will be removed from the group “multi_users”. You can do the same by following the process below.
- Open the Ubuntu terminal.
- Then, type the following command in the command prompt.
sudo nano /etc/group
EXPLANATION- sudo: Grants administrative privileges.
- nano: Creates/edits text files with Nano text editor.
- etc/group: A text file that contains all the group information.
- 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"
Conclusion
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
What is the command to delete user in Ubuntu?
In Ubuntu, you can use the userdel command to delete users. The userdel command with option -r will delete the home directory of the user along with the user, whereas without the -r option only the user will be deleted.
How to Remove User and user group in Ubuntu?
To remove a user and the user group first you will need to log in to another user account that has access to sudo privileges. Then, you can use the userdel/geoupdel command to delete a user and a user group according to your need.
How do I list all users in Ubuntu?
You can list all the users in Ubuntu by displaying the /etc/passwd file contents. For displaying file contents you can use any text editor/viewing command such as nano, cat, more, less, etc.
How do I bulk delete in Linux?
To bulk delete files in Linux, you will need to list the desired file names separated by spaces and apply the rm command. The rm command followed by name of the files will delete all the listed files.
How do I add and remove users in Linux?
You can add a user to the Linux distribution using the useradd command. On the other hand, you can remove a user with the userdel command.
How do I remove a user from login screen Linux?
To remove a user from the login screen you can enable the HideUsers option in the /etc/sddm.conf file. All you need to do is open the /etc/sddm.conf file in a text editor then uncomment the HideUsers option and assign your desired user name to the HideUsers variable.
How do I remove a user from login shell?
To remove a user from the login screen you can enable the HideShells option in the /etc/sddm.conf file. All you need to do is open the /etc/sddm.conf file in a text editor then uncomment the HideShells option and assign “/sbin/nologin” to the HideShells variable.
How can I see all Users logged in?
Linux offers multiple commands to list all the currently logged-in users. You can run any of the following commands to view the logged-in users in your system: w, who, users, etc
Related Articles