Can anyone tell me how to remove/delete a user in Linux?
remove user linux
Collapse
Unconfigured Ad Widget
Collapse
X
-
Two command-line tools are available for deleting a user account: userdel and deluser.
Remove a user using userdel:
Syntax: userdel [OPTIONS] USERNAME
-f This flag is utilized to forcefully eliminate the user from the Linux system, terminating all ongoing processes, logging out any active terminal sessions, and ultimately permanently erasing the user from the Linux environment.
-r This option is utilized to delete files linked to the user.
-Z, --SELinux-user: This flag eliminates any SELinux user mapping assigned to the user.
To remove a user in Linux, it's necessary to be logged in as the root user or a user with sudo access.
To delete a user:
userdel username
Upon execution, the command accesses the contents of the /etc/login.defs file. Properties specified in this file override the default actions of userdel if the file contains a setting of USERGROUPS_ENAB as yes; userdel deletes the group with the same name as the user if no other user is a member within the group.
The command deletes the user entries from the /etc/passwd and /etc/shadow files.
In most Linux distributions, when removing a user account with userdel, the user home and mail spool directories are not removed.
Use the -r (--remove) option to force userdel to delete the user's home directory and mail spool:
userdel -r username
The command above doesn't remove user files situated in different file systems. You will need to search and delete these files manually.
Suppose the user you wish to remove is currently logged in, or there are running processes associated with this user. In that case, the userdel command does not allow the removal of the user.
In this circumstance, it is suggested to log out the user and employ the killall command to terminate all active processes associated with that user:
sudo killall -u username
After these steps are done, you can proceed with removing the user.
An alternative approach is to use the -f (--force) option, which directs userdel to forcibly eliminate the user account, disregarding whether the user remains logged in or has active processes associated with the user.
userdel -f username
Remove a user using deluser
To delete the user without erasing the user files, execute the following:
deluser username OR
sudo deluser username
To remove both the user and their home directory along with the mail spool, use the --remove-home flag:
deluser --remove-home username OR
sudo deluser --remove-home username
In the context of Ubuntu, the deluser command is preferred over the lower-level userdel command due to its user-friendliness.
-
Comment