How to Delete User Accounts & Home Directories in Linux

As a System Administrator in Linux, you may have to remove a user’s account after some time when a user account may become dormant for so long, or the user may leave the organization or company for any other reasons.

When removing user accounts on a Linux system, it is also important to remove their home directory to free up space on the storage devices for new system users or other services.

In this tutorial, I am going to take you through steps you can use to delete a user’s account together with his/her home directory on a Linux system.

To learn how to create user accounts and manage them on Linux systems, read the following articles from the links below:

How to Delete/Remove a User Account with Home Directory

For demonstration purposes, first I will start by creating two user accounts on my system that is user tecmint and user linuxsay with their home directories /home/tecmint and /home/linusay respectively using the adduser command.

adduser tecmint
passwd tecmint

adduser linuxsay
passwd linuxsay
Create New User Accounts in Linux
Create New User Accounts in Linux

From the screenshot above, I have used the adduser command to create user accounts on Linux. You can also use the useradd command, both are the same and does the same job.

Let’s now move further to see how to delete or remove user accounts in Linux using deluser (For Debian and its derivatives) and userdel (For RedHat-based systems) commands.

sudo deluser --remove-home username
sudo userdel -r username

The above command will delete the user’s home directory along with their account.

The directives inside the configuration file for deluser and userdel commands determine how it will handle all user files and directories when you run the command.

Let us look at the configuration file for the deluser command which is /etc/deluser.conf on Debian derivatives such as Ubuntu, Kali, and Mint, and for RHEL/CentOS/Fedora users, you can view the /etc/login.defs files.

The values in these configurations are default and can be changed as per your needs.

vi /etc/deluser.conf         [On Debian and its derivatives]
vi /etc/login.defs           [On RedHat/CentOS based systems]
Del User Configuration
Del User Configuration

To delete a user with a home directory, you can use the advanced way by following these steps on your Linux server machine. When users are logged on to the server, they use services and run different processes. It is important to note that users can only be deleted effectively when they are not logged on to the server.

How to Lock User Accounts in Linux

To lock a user account in Linux, you can use the passwd command followed by the -l or --lock option and the username. Locking a user account prevents the user from logging in while still retaining the account and its associated files.

sudo passwd -l username
OR
sudo passwd -lock username
Lock User Account Password in Linux
Lock User Account Password in Linux

To unlock the account, you can use the passwd command again with the -u option.

sudo passwd -u username

How to Find and Kill User Running Processes in Linux

You can find and kill user-running processes in Linux using the ps command to find the processes associated with a specific user and the kill command to terminate them.

Finding User Processes

Use the ps command with the -u flag followed by the username to list processes for a specific user.

ps -u username

Then you can list the processes in terms of username, PIDs, PPIDs (Parent Process IDs), terminal used, process state, and command path in a full formatting style with the help of the following command as shown:

ps -f --pid $(pgrep -u tecmint)
Find All Running Processes of User
Find All Running Processes of the User

Killing User Processes

Once you find all the running processes of a user, you can use the killall command to kill those running processes as shown.

killall -9 -u tecmint

The -9 is the signal number for the SIGKILL signal or use -KILL instead of -9 and -u defines the username.

Note: In recent releases of RedHat/CentOS 7.x versions and Fedora 21+, you will get an error message:

-bash: killall: command not found

To fix such an error, you need to install psmisc package as shown:

yum install psmisc       [On RedHat/CentOS 7.x]
dnf install psmisc       [On Fedora 21+ versions]

How to Backup User Data Before Deleting

To back up user data before deleting a user account on a Linux system, you can follow these commands.

sudo mkdir /backup
sudo cp -r /home/username /backup

Optionally, you can create a compressed archive of the user’s data to conserve storage space. I have used the tar command to create a backup of user home directory as follows:

sudo tar -zcvf /backup/username_backup.tar.gz /backup/username
Backup User Home Directory in Linux
Backup User Home Directory in Linux

How to Delete/Remove User Files in Linux

Now you can safely remove the user together with his/her home directory, to remove all user files on the system use the --remove-all-files option in the command below:

deluser --remove-home tecmint      [On Debian and its derivatives]
userdel --remove tecmint           [On RedHat/CentOS based systems]
Delete User Account with Home Directory
Delete User Account with Home Directory
Summary

That is all to do with removing user and their home directory from a Linux system. I believe the guide is easy enough to follow, but you can voice a concern or add more ideas by leaving a comment.

Similar Posts