Linux system administrators have to manage multiple user accounts on their system. Often each of these users has their own home directory or folder to store their files and run their own applications. Sometimes system administrators need to remove user accounts from their system, in case an employee leaves the organization or an account remains unused for a long time. In this article, we will learn how to delete user accounts with home directory in Linux.
How to Delete User Accounts with Home Directory in Linux
Here are the steps to delete user accounts with home directory in Linux.
1. Create User Accounts
First, we will create user account test1 for our purpose, using the following adduser commands. You can also use useradd command for this purpose. Its home directory will be located at /home/test1.
# adduser test1 # passwd test1
You can use deluser (for Debian/Ubuntu) and userdel (for RHEL/Fedora/CentOS) for deleting user accounts. But the thing is that you can delete users from Linux only when they are not logged in. So if they are logged in and using different applications and services, you will need to terminate them first and forcefully logout the users before deleting their account.
2. Lock User Accounts in Linux
First of all, you need to lock the user account that you want to delete from your system, so that they are unable to run any processes or even log into your system. You can do this by using passwd command with –lock option, followed by username. Here is the command to lock user test1.
# passwd --lock test1 Locking password for user test1. passwd: Success
3. Find & Kill All Processes for User
Next, find out all running processes for the said user using pgrep command. It will give you the PIDs of all processes being run by the user. Here is the command to find all processes run by user test1.
# pgrep -u test1 2343 2323 3443 ...
You can further use the above command’s output in ps command, to get more details about these processes, such as their username, PIDs, PPIDs (Parent Process IDs), terminal used, process state, command path, etc.
# ps -f --pid $(pgrep -u tecmint) UID PID PPID C STIME TTY STAT TIME CMD test1 2343 1 0 10:49 ? SLl 0:00 /usr/bin/gnome-keyring-daemon --daemonize --login test1 2323 1280 0 10:49 ? Ssl 0:00 mate-session test1 3443 1959 0 10:49 ? Ss 0:00 /usr/bin/ssh-agent /usr/bin/dbus-launch --exit-with-session /usr/bin/im-launch mate-session
Next, you can use kill command to kill these processes. Use -9 option to kill the processes, followed by the PIDs of all processes that you want to kill.
# kill -9 2343 2323 3443 ...
Alternatively, you can use the -u option with killall command to kill all the processes of a given user.
# killall -9 -u test1
In the above command, we use -9 option to send SIGKILL signal to processes. We use -u option to specify the username whose processes we want to kill.
Sometimes killall command is not present in RHEL/CentOS/Fedora system. If killall command is not present on your system, you can use the following command to install it.
# yum install psmisc [On RedHat/CentOS 7.x] # dnf install psmisc [On Fedora 21+ versions]
4. Backup User Data
It is advisable to backup all files & directories in your user’s home directory for future use. /home/test1 is the folder of user test1. Here is the command to backup the contents of test1 user.
# tar jcvf /home/test1-backup.tar.bz2 /home/test1
5. Delete User Accounts
Now you can use deluser or userdel command, depending on your Linux distribution, to delete user account. Although this command deletes user credentials from the system, it will not delete the content of user’s home directory. For this purpose, you can use –remove-home option.
# deluser --remove-home test [On Debian and its derivatives] # userdel --remove test [On RedHat/CentOS based systems]
In this article, we have learnt how to delete user accounts in Linux. As mentioned earlier, the key is to lock the user account first, and kill all processes being run by the user, before you delete the user’s account. Otherwise, you will get an error.