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
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 but the main difference between these commands lies in their intended usage and user interface.
The “adduser” command is a higher-level interface for adding users to a system, performs any necessary setup, and creates a home directory for the user by using default settings.
On the other hand, “useradd” is a lower-level utility for adding users to a system, and updating user information. It does not create the user’s home directory by default and requires the administrator to specify additional options manually.
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]
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
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)
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
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]
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.
You say that adduser and useradd is the same but the manpage for useradd says it is a low level utility for adding users and admins should use useradd instead. If you check the file sizes you will notice that useradd is about 4 times bigger than adduser.
In the ReHat version. How can I delete a user without deleting his/her home directory.
We have multiple users that share the same home directory.
Thanks
@Jaime
Deleting shared directories is not recommended. Thanks for your input.
That is correct – But, which command should I use to delete the user without deleting his/her home directory?
Thanks
@Jaime
Use userdel command, for instance:
Tutorial is good, but one thing is missing: no word about corresponding group.
Warning: group `tecmint’ has no more members.
@ProWebDeveloper
True, so group ‘tecmint’ has to be removed. Thanks for the heads up.
In the last step you make reference to a –remove-all-files option. What other files besides the home directory would be removed with that option?
@warren
The user’s mail spool, however, – -remove-all-files option does not have any effect any more for that purpose. Because – -remove-home removes both the user’s home directory and mail spool. Additionally, the – -backup option backups all files contained in the userhome and the mailspool-file to a file named /$user.tar.bz2 or /$user.tar.gz on the system.
Thank you @aaron kili k
@warren
Welcome and thanks for following us.
You have a mistake here…
@Karim,
Thanks a ton, good catch man, corrected in the writeup..:)
What if we use
userdel -r
to delete users from its home directory after taking backup? Thanks@Gagan,
The user and it’s home directory will be removed completely if we use
userdel -r
option..