Using the userdel Command in Linux to Remove Users

In this tutorial, we will show how you can remove a user on a Linux operating system using the userdel command.

Remove Users in Linux using userdel

Knowing how to remove a user is essential if you administrate a Linux system with multiple users. Luckily the process is pretty straightforward, thanks to the userdel command.

Removing dormant Linux accounts clears up disk space and removes possible security issues. You can use the passwd command to lock accounts if they become dormant. However, if the account is never going to be in use again, it is best to remove it.

This tutorial touches on several different topics for utilizing the userdel command. We discuss the command’s syntax, options you can use, exit codes, and the process to remove a user successfully.

If you wish to add users rather than remove them, check out the guide on using the useradd command on Linux.

Table of Contents

userdel Command Syntax

The syntax of the userdel command is very straightforward as it only accepts two parameters. Below is an example of how you should structure this command.

userdel [OPTIONS] [LOGIN]
  • [OPTIONS] is where you specify any options that you would like applied to the command. We go through the options you can use further down the page.
  • [LOGIN] is the username of the user you wish to remove.

userdel Command Options

  • -f or --force will force the files and directories to be deleted. Even if the user does not own them. It also forces the user to be deleted. Even if they are still logged in.
  • -r or --remove removes the home directory and mail spool.
  • -R or --root CHROOT_DIR makes changes in the CHROOT_DIR directory and uses configuration files from within the same directory.
  • -P or --prefix PREFIX_DIR makes changes in the PREFIX_DIR directory and uses configuration files from within the same directory. Does not support SELinux (Security-Enhanced Linux).
  • -Z or --selinux-user removes the SELinux (Security-Enhanced Linux) user mapping.
  • -h or --help outputs a small amount of helpful information.

Command Exit Codes

When userdel exits, it will provide information on whether there were issues deleting the user. Below are the values that you should expect.

  • 0 Success
  • 1 Can’t update password file
  • 2 Invalid command syntax
  • 6 User does not exist
  • 8 User currently logged in
  • 10 Can’t update group file
  • 12 Can’t remove home directory

Optional Housekeeping Before Deleting the User

To clean up everything correctly, you can perform a few tasks before you delete a user.

These are all optional and are not required to remove a user from Linux. Simply skip to “using userdel” if you do not need to do these tasks.

Lock User Account to Prevent Login

If you wish to prevent the user from logging into the system while you are removing the account, you can simply run the passwd command with the -l option. Replace testuser with the username of the user you wish to lock.

sudo passwd -l testuser

You should get the following output after entering the above command.

passwd: password expiry information changed.

To ensure that the user is forced out of the system, you will want to do the next task of killing all the user’s processes.

Kill All Processes by the User

To remove any processes running under the user we wish to remove, we will need to use the killall command. You will need to replace testuser with the username of the user you want to remove.

killall -u testuser

You will not get any output unless there is an error. For example, the user does not exist.

Remove Any Cron Jobs

You will want to remove any cron jobs that might still exist for the user. To do this, you will need to use the crontab command. The -r option will remove the crontab, and the -u option allows us to specify which users crontab we wish to remove.

crontab -r -u testuser

Backup the Home Directory

Lastly, you may want to backup the user’s home directory as it may contain data you or the user might need in the future. We will be using the tar command to backup the directory.

In the example below, we are backing up our testusers home directory located at /home/testuser. We will be storing it in a backups directory located at /backups/users/. You may need to create the backup directory first.

tar -cjvf /backups/users/testuser-29-05-2022.tar.bz /home/testuser

The following output confirms our testuser directory was successfully backed up.

root@pimylifeup:/home/dev# tar -cjvf /backups/users/testuser-29-05-2022.tar.bz /home/testuser
tar: Removing leading `/' from member names
/home/testuser/
/home/testuser/.profile
/home/testuser/.bashrc
/home/testuser/.bash_logout
root@pimylifeup:/home/dev# ls /backups/users/
testuser-29-05-2022.tar.bz

You can now remove the home directory using the rm command, or you can use userdel to remove it.

Using userdel to Remove the User

Removing a user in Linux is a relatively straightforward procedure. However, it is recommended that you use a root user or a user with superuser privileges to avoid any permission errors.

To delete a user from your Linux system, you will need the username of the user you wish to remove. If you do not know the username, you can use the following cat command to list all the users on the system.

cat /etc/passwd

Simple Deletion

In this example, we will be removing the testuser. You will need to be logged in as root or have superuser privileges (sudo).

userdel testuser

To check the outcome of the above command, you can use echo $?. We covered exit codes earlier in this tutorial. In the output below, we have 0, which means success.

root@pimylifeup:/home/dev# echo $?
0

Remove Home Directory and Mail Spool

By default, userdel will not delete the user’s home directory and mail spool. However, if you no longer require either of these or have them backed up, you can use the -r option to remove them.

userdel -r testuser

In the output below, we have successfully deleted our testuser and its home directory. However, there was no mail spool to be deleted, resulting in a warning. Our exit code is also 0, which means the command was successful.

dev@pimylifeup:~$ sudo userdel -r testuser
userdel: testuser mail spool (/var/mail/testuser) not found
dev@pimylifeup:~$ echo $?
0

Force Delete

If you have trouble deleting a user, you can force the deletion. This process will remove the user even if they are logged in. It will also delete the user’s home directory and mail spool, even if it is shared by another user.

It is not recommended that you use this option as it can be dangerous and leave your system in an inconsistent state. If you need to force log out the user, we recommend locking the account and forcing logging out the user.

If you wish to force delete a user, you can use the following command.

userdel -f testuser

The output below shows that our user is successfully removed thanks to the 0 success exit code. You can also see we did not have a mail spool, so userdel threw a warning.

dev@pimylifeup:~$ sudo userdel -f -r testuser
userdel: testuser mail spool (/var/mail/testuser) not found
dev@pimylifeup:~$ echo $?
0

Conclusion

I hope by now you have a good understanding of how to use the userdel command to remove users from a Linux operating system.

If you wish to learn more about user administration on Linux systems, I recommend taking a look at some of our other Linux command tutorials. For example, we touch on the passwd command, adding users, chown, chmod, and much more.

Please let us know if you notice a mistake or if an important topic is missing from this guide.

Leave a Reply

Your email address will not be published.