Here’s a short and sharp article on how to remove a user from a Linux system. These examples will work on the majority of Linux distributions.
The userdel Command
The userdel command can be run from the Linux shell to remove a user. Here’s the syntax:
userdel OPTIONS USERNAME
Note that:
- OPTIONS should be from the below table
- USERNAME should be the login username of the user to be deleted
- userdel requires administrative rights and will need to be run as root or using the sudo command
BE AWARE – RUNNING ANY userdel COMMAND IS DESTRUCTIVE – ACCOUNTS AND FILES WILL BE DELETED, SO MAKE SURE YOU’RE SURE OF WHAT YOU’RE DOING BEFORE RUNNING IT.
Here are the available options for the userdel command, straight from the manual:
Userdel Options | |
---|---|
-f, –force | This option forces the removal of the user account, even if the user is still logged in. It also forces userdel to remove the user’s home directory and mail spool, even if another user uses the same home directory or if the specified user does not own the mail spool. If a group exists with the same name as the deleted user, then this group will be removed, even if it is still the primary group of another user. Note: This option is dangerous and may leave your system in an inconsistent state. |
-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. |
-Z, –selinux-user | Remove SELinux user assigned to the user’s login from SELinux login mapping. |
You can always view the full user manual by running:
man userdel
How to Remove a User
This example removes a user called ‘fred’:
userdel fred
How to Remove a User’s Files (Home Directory and Mail)
This example will remove the account ‘fred’ and irreversibly delete all of the files in his home directory:
userdel -r fred
A user’s home directory is the default directory they are placed in when logging in, which they have full permissions for, and where they should be storing all of their personal files.
Home directories are usually located in the /home/ folder and named to the username of the owner – so the fred users home directory would most likely have been:
/home/fred/
Removing SELinux Mappings
SELinux is a security module for Linux, giving administrators finer control over what users can and cannot do. Roles can be created and mapped to users giving them access to system resources.
To remove all related SELinux assignments when deleting a user, pass the -Z option:
userdel -Z fred