Maintaining users on the server means add them, modify them and delete them. When a user is no longer need on the system for any reason, we should delete it to avoid any security breach. On the Linux system, we have userdel command to delete a user
What is userdel
Userdel is a low-level utility for removing users. On Debian, we should usually use deluser command. Userdel will look at the system account files such as /etc/password and /etc/group. Then it will be deleting all entries related to the user name. The user name must exist before we can delete it.
How to use Userdel
Since userdel will modify system account files, we need root privilege to run it. Otherwise we will have an error message that saying “only root can do that” or similar. After we gain root privilege, we can delete a user by typing userdel from your console. Here’s a sample of default usage of userdel
$ sudo userdel pasadena
# userdel pasadena
As you see above, we can’t delete a user with the name pasadena without root privilege. When we have it, the system gives us no error which means that the user is deleted successfully.
Completely remove user home directory
Using userdel without options, will only delete the user. User home directory will still remain at /home folder.
When we go into /home folder, we still seeing pasadena folder which owned by 1002. Created user will usually have a same group name with user name. 1002 was the UID and GID of pasadena user name and pasadena group name.
To completely remove the home user along user deletion, we can use -r option. This option will also delete user’s mail spool if exist.
Force delete a user
Userdel provide -f option to force user deletion. This option will delete a user even the user still log in into Linux system. Please take a look a sample screenshot.
The screenshot above shows us that pasadena user is logged in to the Linux system. It is marked by process 6218 which is SSHD process. But when we do “userdel -f pasadena” the command only shows us the information that the user is logged in. The command itself was successful. If we see the content of /etc/passwd file using cat command, we don’t see pasadena user there. It’s home directory still exist but the owner is changed.
One thing that we must know is that, userdel with -f option did not broke the SSH connection. So the user actually still logged in and active even the user is not exist. But when the user log off, the user can not log in anymore because that user has been deleted.
So this options is dangerous to use since it can lead your system into an inconsistent state.
Userdel is a tool to delete a user inside Linux system. Userdel also a backend of deluser command, a perl script to delete a user. As usual, you can always type man userdel to explore more detail about userdel command.