How to Use userdel Command in Linux – Remove Users

Last updated: December 30, 2022

A system administrator might require to remove a user when the user account is compromised, user no longer required, or when the user itself creates problems.

In this tutorial, we learn how to use userdel command in Linux to remove a user.

Linux userdel command

The userdel command is a low-level utility that can be used to delete an existing user and associated files from the Linux system.

This command is commonly used by system administrators with root or sudo access privileges.

Syntax:

userdel [options] username

Where username is the name of the user to get removed.

These are common files that could be touched when deleting a user /etc/group, /etc/login.defs (on Debian based /etc/deluser.conf), /etc/passwd and /etc/shadow.

Remember if you using a Debian-based system the preferred command to delete user is deluser.

Keep in mind that deleting a user can have unintended consequences, so it is important to be careful when using this command.

options

The userdel command in Linux has two options when deleting a username.

  • -r - Remove the user’s home directory and mail spool along with the username.
  • -f - Forcefully remove the user account, even if the user is still logged in or if there are running processes that belong to the user.
  • -z : Remove the users SELinux user mapping.

Userdel Examples

Let's check how to use userdel command with few practical examples.

Removing a user

Type username followed by the userdel command to delete a user.

For example to delete a user named student1, type:

sudo userdel student1

This will delete the student1 user account, but will not remove the user's home directory or any files owned by the user. The command exit with zero output if successfully deleted the user account.

Deleting user including home directory

Use -r option to delete user including the user's home directory and mail spool.

Example:

sudo userdel -r student2

This will delete the student2 user's home directory and all files in it. You can verify using ls command to confirm home directory deleted.

Forcing delete

The -f option can be used with the userdel command to force the deletion of a user account, even if the user is still logged in or files cannot be removed.

Example:

sudo userdel -rf student3

This tells userdel to ignore any errors that may occur when deleting the user's home directory or files. This force deletes the home directory even if it's owned by another user.

Recommended steps when removing a user

Let's check the proper steps to safely remove a user in Linux.

Step 1. Prevent access by adding lock to user account

By executing the chage command with the -E flag we can set an expiration date for any user account. So if we set -E to 0 for any account, that account will instantly expire and the user will be locked out of the system.

sudo chage -E 0 bob

The passwd -l and usermod -L commands can also lock the user's account but the user can still authenticate using other methods such as SSH key authentication.

Step 2. Kill process used by user

After locking a user account, kill any processes that are running under their account.

Use the following pgrep to find all processes associated with the user:

sudo pgrep -u bob
or
ps -u username

This command displays the Process IDs of all the processes used by the user named bob.

You may now all processes associated with the user using the pkill command.

sudo pkill -u bob
or
kill pid-number

Step 3. Remove all schedules/cron jobs

Use the crontab command to list the cron jobs for the user you want to delete.

crontab -u bob -l

This will display a list of all the cron jobs that are scheduled to run under the bob user.

sudo crontab -r u bob

Now you can delete all the cron jobs for the user, you can use the -r option with the crontab command.

crontab -u bob -r

This will delete all the cron jobs for the bob user.

Step 4. Remove the user

If you've completely followed the above steps, we can now safely delete user accounts. This is done by executing the userdel command accompanied by the -r option.

sudo userdel -r bob

This will permanently delete the user and their files, so use caution when using this command.

Can userdel remove a group?

When a user is deleted using userdel, its primary group will also get removed if the group doesn't contain any other users. ie If the group contains a user other than the primary user, then user will be deleted and the group will stay.

Conclusion

Remember deleting a user is a permanent action that cannot be undone. It is a good idea to carefully consider the implications of deleting a user before doing so.

Navigate all-in-one place of Linux Commands for more learning.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin started his career in IT in the year 2000 as a computer science instructor and worked as a Linux system engineer and senior analyst roles. Currently working in DevOps environments to increase efficiency and improve delivery time in AWS Cloud infrastructure. He is certified in RHEL, CCNA, and MCP and holds a Masters's in computer science. When in his free time love playing cricket, blogging, and listening to music.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment