Ugacomp

How to delete Linux Users using the Terminal

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

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

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.