Learn How to Delete or Remove A User on Ubuntu

delete user and home directory ubuntu

How do I delete a user from your Ubuntu Linux system? Such instances include when users leave the company or when their accounts become dormant for so long. In this tutorial, I will show you how to delete a user and home directory on Ubuntu 18.04.

But first, we are going to carry out some pre-tasks before deleting the user and home directory. These tasks will include

1) Locking the User account
2) Killing any  processes associated with the user
3) Backing up the  user's home directory
4) Removing any cron/print jobs
5) Deleting/removing user accounts

1) Lock the  User account

Begin by first locking the user's account to deny them entry into the system. The syntax for this will be

passwd -l {username}

For example

passwd -l alice

Output

Locking password for user alice.
passwd: Success

2) Kill all running processes of the User

After successfully locking the account, you need to find all the running processes attached to the user account and 'kill' them using their PID (Process IDs)
Using the ps command, the syntax is

ps -u {username}

For example

ps -u alice

Output

 PID TTY          TIME CMD
 6561 pts/0    00:00:00 bash
 6586 pts/0    00:00:00 ps

Using the top command, the syntax will be

top -U {username}

For instance

top -U alice

Output

 PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 6561 alice     20   0   21196   5000   3224 S  0.0  0.5   0:00.03 bash
 6595 alice     20   0   40540   3748   3148 R  0.0  0.4   0:00.01 top

To end or 'kill' all the running processes, use the killall command as shown

killall -9 -u {username}

In our case, we shall have

killall -9 -u alice

-9 flag is the SIGKILL command. It tells the system to terminate the processes without cleaning up.
-u flag defines the username

3) Backup user data before deleting

This is an optional step, but highly recommended should a need for reviewing a user's account or files arise.
I decided to use tar utility to perform the backup. The syntax would be

tar cvjf backup.tar.bz   /home/username

In our example, the syntax will be

tar cvjf backup.tar.bz   /home/alice

Sample Output

tar: Removing leading `/' from member names
/home/alice/
/home/alice/.bash_history
/home/alice/.profile
/home/alice/.bashrc
/home/alice/.bash_logout

4) Delete the user's cron jobs

To delete the cron jobs of a user, run

crontab -r -u alice

To erase printer jobs run

lprm alice

5) Delete/ remove user account and files

We've finally reached the last stage of removing or erasing user accounts from the system. The above command will remove both the user account and the home directory.

userdel -r alice

In this case, the user 'Alice' alongside the home directory will be purged. This is because we have used the -r flag.

To delete the user only and retain the home directory, simply  run

userdel alice

In this tutorial, we have briefly outlined the steps you need to take to delete a user and home directory from your system in a step-by-step manner. We hope you have taken note in case next time you are faced with such a scenario.

Read Also:

Jamie Arthur 1:57 pm

About Jamie Arthur

James is a passionate Linux and Windows Systems Administrator with 4 years of experience in Linux, databases and Front-End development. He loves doing research on different Linux distributions and experimenting with installation and configuration of different services and features. His hobbies include swimming, reading novels and playing video games.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.