Introduction to Linux Delete User
Linux Delete user option is used to delete a user from Server. By deleting a user form Linux, we can remove SSH access also and delete user’s files and user ownership directories. We can delete users in Linux only if we have root permissions.
Linux server is a server where multiple users can login at a time. This means that there will be a number of users created and interacted at the same time. To manage their accounts, the administration should take up the responsibility to manage the user’s account. There might be situations where we would need to delete a user from the Server because the user is no longer associated with the organization or the user is no longer running in the server. Here in this article, we will discuss how to delete a user from the server.
The following are the ways that are used to delete a user from Server. The syntax is below:
userdel [options] user_name
userdel -r user_name
Here the below options are used with userdel command in Linux:
- -f : Forcefully delete a user account and also with forceful removal of files.
- -r : Deletes user account along with mail spool and user’s home directory
- -z : Deletes SELinux users if they are mapped for the users while deleting user from Linux.
How to Delete a User from Linux?
Below are the options that we can use to perform the delete operation on a user in Server.
|-f||Forcefully delete a user account and also with forceful removal of files.|
|-r||Deletes user account along with mail spool and user’s home directory|
|-z||Deletes SELinux users if they are mapped for the users while deleting user from Linux.|
Follow the below process step by step to delete a user in Linux:
Step 1: The first step to delete a user in Linux Server is to lock the user’s account. We can do this by the below process.
$ passwd -l user_name
Step 2: Then, we need to back up the files from the user’s home directory to a backup folder. We can use the tar option to compress the size of the files.
$ tar -zcvf /backup_folder.tar.gz /home/user_name/
Step 3: Remove cron jobs that are set by the user.
$ crontab -r -u username
Step 4: Then delete the printed jobs available in the system.
$ lprm user_name
Step 5: Display all the files that are owned by the user.
Step 6: At the end, we will delete the user account in Server. Userdel command is used to delete a user in Server. We can do it as shown below.
$ userdel -r user_name
Examples to Delete User form Linux Server
The following are the examples of using userdel command in Server to delete a user form Server. Options are briefly described with examples for better understanding.
Example #1 – No Option
When no option is passed in userdel command in Linux, it will delete the user from the server.
Example: if we want to delete a user named ‘nikhil’, then we can delete his account from Linux by the below example.
Example #2 – Option -r
To delete the user’s home directory and mail spool, we can use option -r with userdel command to delete them.
userdel -r user_name
Example: To delete the user’s home directory and his mail spool for a user named ‘nikhil’, we can do it in Linux by the below example.
userdel -r nikhil
Example #3 – Option -f
when there are any other files in other locations for the deleted user, we can delete them forcefully by using option -f even though it is not owned by the user.
userdel -r -f user_name
Example: Forecfull deletion of files from other locations can be done as shown below.
userdel -r -f nikhil
Example #4 – Option -z
To delete any SELinux users mapped to the user, we can use -z option.
userdel -z -r -f user_name
Example: we can delete any SELinux users mapped to the user named ‘nikhil’ by using -z option as shown below.
userdel -z -r -f nikhil
In this article, you have now learned how to delete a user form Linux Server using userdel command. We can delete the users in Ubuntu, RHEL, CentOS also in a similar way that we did in Linux. Userdel command is a very important command mostly used by the administration to monitor if the user accounts are deleted when they are not been used in the Server. Delete user option is used to delete a user from Server. By deleting a user form Linux, we can remove SSH access also and delete user’s files and user ownership directories. We can delete users only if we have root permissions.
This is a guide to Linux Delete User. Here we discuss how to delete a user from the Linux server along with different examples. You may also have a look at the following articles to learn more –