Here is how to delete a Linux user using the terminal;
Using the userdel command
The userdel
command in Linux is used to delete a user account. It removes the specified user from the system, along with their home directory and mailbox, depending on the options used.
Here’s the basic syntax:
sudo userdel username
Now, you can replace “username” with the actual username you want to delete. Note that you need to have administrative privileges to delete a user. The sudo
command is used to execute the command with elevated privileges.
Remove the user with their associated home directory
You might also want to remove the user’s home directory and mailbox. You can use the -r
option with userdel
to achieve this:
sudo userdel -r username
This will delete the user, their home directory, and their mailbox.
Only Remove the User and keep the home directory
If you want to keep the home directory but remove the user, you can manually delete the user and leave the home directory intact:
sudo userdel username
And then manually delete the home directory if needed:
sudo rm -r /home/username
Replace “/home/username” with the actual path to the home directory of the user you want to delete.
How to delete multiple users
Deleting multiple users occurs when you have several user accounts created on your Linux account. This means that you need to establish which users you want to delete. So, you want to list and view all users on the system
RECOMMENDED READING: How can I list and view all Users in Linux?
To delete multiple users in Linux using the userdel
command, you can simply provide the list of usernames separated by spaces. Here is the basic syntax:
sudo userdel username1 username2 username3
You can replace “username1,” “username2,” etc., with the actual usernames you want to delete.
If you want to delete the home directories and mailboxes associated with these users, you can use the -r
option:
sudo userdel -r username1 username2 username3
This will delete the specified users along with their home directories and mailboxes.
Use a script to delete multiple users
You can use a script to delete multiple users by following these steps;
Create a deletion script
In this example, we’re naming the user deletion script delete_users.sh
and it will have the logic the list of users we will specify in a separate file called userlist.txt
#!/bin/bash
while read -r username; do
sudo userdel -r "$username"
done < userlist.txt
The provided Bash script utilizes a while
loop to iterate through a list of usernames stored in a file named userlist.txt
. For each username read from the file, the script employs the userdel
command with the -r
option to remove the corresponding user along with their home directory and mailbox. The sudo
command ensures the execution of these operations with elevated privileges. This script serves as a concise and automated way to delete multiple users, making it particularly useful for bulk user removal scenarios while being mindful of the potential impact on the system.
We also need to add the execute permission on the delete_users.sh
file we’ve created by running the following command;
chmod +x delete_users.sh
Create userlist.txt
The userlist.txt file will contain a list of users we would like to delete in bulk. You have to create this text file in the same directory as the script, delete_users.sh
. List the usernames you want to delete, one per line
user1
user2
user3
Run the Script
To execute the script, use the following command
./delete_users.sh