How to Delete Multiple Users in Linux? [2 Steps]

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]Flowchart for deleting multiple users in linux.

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:

  1. At first, open a terminal window by pressing CTRL+ALT+T.
  2. 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.

  3. 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.
  4. 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

    Bash script for deleting multiple users in linux.

    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”.
  5. To save and exit from the script, press CTRL+S and CTRL+X respectively.
  6. 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.
  7. 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:

  1. At first, press CTRL+ALT+T to open the Ubuntu Terminal.
  2. Run the previously written script by simply typing the file name.
    delete_users.sh
  3. 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.
  4. Now, type each username in response to the prompt message to delete and press ENTER.
  5. Finally, Input the password of the sudo user to run the userdel command written inside the script.

    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.
    Executing script for deleting multiple users in linux.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.


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:

  1. At first, open the Ubuntu terminal.
  2. 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.
  3. Press ENTER.
  4. Finally, provide the password of the currently logged-in user.
  5. Now, to check if the UserC home directory still exists, type the following and press ENTER.
    ls /home

    Deleting user account without home directoryIn 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:Viewing /etc/groups file contentIn 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.

  1. Open the Ubuntu terminal.
  2. 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.
  3. Press the ENTER button.
  4. Then, give the password of the current user and hit ENTER.
  5. Erase the user names  “UserA” and “UserB” from the group “multi_users”.
  6. To save and exit from the etc/group file, press CTRL+S and CTRL+X respectively.
  7. Now, check the group members by typing the command below and then hit ENTER.
    sudo less /etc/group| grep "multi_users"
    EXPLANATION
    • sudo: Grants administrative privileges.
    • less: Displays file contents in the terminal.
    • etc/group: A text file that contains all the group information.
    • grep: Searches for a string inside a specified file.
    • multi_users: Name of the group from where to delete users.

    Erasing users from groups.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.

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

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Anonnya Ghosh

Hello there! I am Anonnya Ghosh, a Computer Science and Engineering graduate from Ahsanullah University of Science and Technology (AUST). Currently, I am working as a Linux Content Developer Executive at SOFTEKO. The strong bond between Linux and cybersecurity drives me to explore this world of open-source architecture. I aspire to learn new things further and contribute to the field of CS with my experience. Read Full Bio

Leave a Comment