Linux Commands - How to delete user account in Linux

Reference Linux

In this tutorial we will learn to delete user account in Linux.

In one of the previous tutorial we learned how to create a new user in linux using useradd command. Feel free to check that out.

Switch to root user

Before we run the commands given in this tutorial we will switch to root user by running the sudo su command.

In the following example I am switching from my account to root account. You will get a similar result on your terminal.

yusufshakeel@yusufshakeel-ubuntu:~$ sudo su
[sudo] password for yusufshakeel: 
root@yusufshakeel-ubuntu:/home/yusufshakeel# 

Notice the $ sign changes to # and we also switch from yusufshakeel user account to root account.

Alright, lets learn how to delete user accounts.

passwd --lock - To lock user account

We start the deletion process by first locking the user account so that the user can't login again.

In the following example we are locking jane account.

# passwd --lock jane
passwd: password expiry information changed.

killall - Kill all processes of the user

Now we will kill all the processes by the user using the killall command.

In the following example we are listing all the processes by user jane.

# pgrep -u jane
4076
4081
4101
4177
...
...

To kill all the processes by user jane we will run the following command.

# killall -u jane

Backup - Creating backup copy of the user data

We have successfully stopped the user from logging in and we have also killed all the processes connected with the user.

Now, we will backup the data files of the user before deleting the account.

This is an optional step. If you think you are not going to need the data of the user then you can jump to the next step.

In the following example we are creating a backup copy of jane user account and saving it inside /user-account-backup directory.

# tar -zcvf /user-account-backup/jane-user-account-home-directory-backup.tar.gz /home/jane

userdel - Delete user

To delete the user we will use the userdel command.

In the following example we are deleting user account of jane.

# userdel --remove jane

On Debian and its derivative system try using deluser command.

# deluser --remove-home jane