Psychz - Yugandhara
Votes: 0Posted On: Nov 09, 2021 12:10:05
You may come across various scenarios where you may feel the need to remove a user from your Linux machine/server. If you are a system admin of an organization, you may want to delete a user who probably no longer works with the company. Or you may want to delete a demo user from your local machine. The command to do so is simple.
To do so, you must be a root user or have sudo previleges. To delete a user account named 'john_doe' use the command 'userdel usename'
# userdel john_doe
To remove the user’s home directory at the same time, add the -r option:
# userdel -r john_doe
The userdel command won’t work if the user is currently logged in or has processes running under the account. In this case, you have two options. You can either kill all the user’s processes with the killall command or use the -f option with the userdel command to force deletion.
When invoked, the command reads the content of the /etc/login.defs file. Properties defined in this file override the default behavior of userdel. If USERGROUPS_ENAB is set to yes in this file, userdel deletes the group with the same name as the user, only if no other user is a member of this group. The command removes the user entries from the /etc/passwd and /etc/shadow, files.
In this situation, it is recommended to log out the user and kill all user’s running processes with the killall command:
# killall -u username
Once done, you can remove the user.
# userdel -r john_doe
Another option is to use the -f (--force) option that tells userdel to forcefully remove the user account, even if the user is still logged in or if there are running processes that belong to the user.
Deleting a user can be done using the same syntax for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, Fedora, and Arch Linux.