Exploring Userdel Command In Linux

Updated: Feb 4

Userdel command can be used to delete a user and related files in Linux. We are going through the whole process to check what will happen after we run userdel command.


Understanding Userdel Command

The following two steps will happen for userdel command.

  1. deletes the group with the same name as the user if USERGROUPS_ENAB is set to yes in this file /etc/login.defs file. It happens only if no other user is a member of this group. If this group belongs to a shared primary user group, it will not be deleted.

  2. removes the user entries from the /etc/passwd and /etc/shadow files


Which Directory will be Removed for Userdel Command?

In most Linux distributions, by default the user home and mail spool directories are not removed when removing a user account with userdel. If we are sure to remove these files, we can use -r option (--remove).

 -r, --remove
 Files in the user's home directory will be removed along with the home directory itself and the user's mail spool. Files located in other file systems will have to be searched for and deleted manually.

The files under other file systems are not deleted automatically. We need to remove these files separately. If the user is still login the system, userdel command will fail. We need to logout this user first.


Risky Option For Userdel Command

There is one risky option -f for this command. We should be very careful when we run it.

-f, --force
Remove the user even if they are currently logged in. Remove home directory and mail spool even if they are used by another user. Remove group too if USERGROUPS_ENAB is set to yes in /etc/login.defs.

Related Post:

Basic Linux Commands: Linux User Management

How useradd works in Linux

Linux Command: Change User Login Shell Safely

How to fix "WARNING: Your password has expired." in Linux?


26 views

Join our mail group. Get a free Linux account on Cloud.

Never miss a post!

Want a free Linux account?  This account can be used to login to our cloud server and practice Linux commands.

 
kamateravps.gif
topbackgroud.png

✔ Linux Commands   ✔ Linux Skills    ✔ LinuxPerformance   ✔ Linux Interview

Some articles are from the public internet. If you find your article misused or undesired here and you don't want us to display it, please let us know and we'll remove it immediately.