As a Linux system administrator, one of the primary responsibility is to manage the users and groups on Linux systems. User management includes creating users, modifying existing users and deleting users.
Deleting / Removing a user account is the most important task for an administrator when any user leaves from an organization.
This post explains to you how to delete users in Linux using the userdel command.
To be able to delete/remove a user account, you need to be logged in as a root user or a user with sudo privileges.
In Linux, you can delete a user account with userdel command. The userdel command is a low-level utility which is used for removing user accounts in Linux and other Unix like operating systems.
deluser --remove-home user_name.
userdel [options] user_name
How To Delete / Remove User in Linux
To delete a user called user_name from Linux system, use:
Consider checking additional activities you may need to perform before you delete a user account in Linux.
When you run a userdel command without any options specified in the command line, the userdel command reads /etc/login.defs (RHEL / CentOS) or /etc/deluser.conf (Debian / Ubuntu) for default values, for example, the user’s home directory and mail spool will remain in system if REMOVE_HOME = 0 is set in /etc/deluser.conf.
How To Delete / Remove User with Home Directory in Linux
In most of the distributions, the userdel command will not delete the user’s home directory and mail spool.
User the -r option with userdel to remove the user’s along with the home directory and mail spool.
userdel -r user_name
The above command will not delete the user files present in the home directory. You will have to delete the files manually that are in other file systems.
Additional Activities Before Deleting User
Running the userdel command looks like super easy, but the system won’t let you delete the user account if the user is logged in or any process running on his/her name.
ps -U user_name
PID TTY TIME CMD 14532 ? 00:00:00 sshd 14536 pts/2 00:00:00 bash 14622 pts/2 00:00:00 vim
Kill the user’s running process with the killall command. This command will also kick the user out of the system.
killall -u user_name
Lock the user so that the user will not be able to log in again.
usermod -L user_name
You can consider taking the backup of the user’s home directory for future needs.
tar -zcvf /backup/user_name-`date +%Y_%M_%d.tar.gz` /home/user_name/
You have learned how to delete user accounts in Linux using the userdel command. Do double check the user name before you delete users from Linux system.