As a System administrator, you have to maintain Linux servers up to standards with your organizational policies. Therefore there may be some situations where you may need to remove the existing users. Linux permits you to remove existing users from the Linux server. Once the user is removed, the user’s SSH access on the server would be revoked. All the files related to the users also can be deleted with directory ownership. with this tutorial, Let’s learn how to remove a user from Linux.
root user access or non-root user with sudo privileges
How to use Linux to Remove User?
Step by step guide to removing user in Linux
1) First login to the server with user login/using the SSH.
2) type the below command. It will switch to the root user if you are not the root.
$ sudo su -
3) Next, run the
userdel command to remove the user.
$ userdel user-name
4) Done. The user will be deleted securely.
Delete/Remove Users in Linux
Linux can operate on a multi-user level and the system can handle more than one user to access the core functionalities of Linux. The user gets permission to interact with the system at the same time when another user is accessing the data. It is system administrators who take care of the new user creation/deletion and group activity.
You get the rights to create new users and assign them to different groups based on their tasks. Each group would have predefined permission.
You get to set the group level permission, which restricts all the users from private access to data. You can assign the user’s role and responsibilities in the group.
There will be a time when you want to delete the user account in the Linux operating system. Perhaps the user’s job is complete, or the user may have moved to another organization.
In this case, keeping the user’s access to the systems may create vulnerability or a data leak, which will be a costly affair.
You can use this command to remove the particular users without damaging the other profiles. It is the safest way to delete specific data.
To delete the user account with the user’s home and mail spool, run the below command.
$ userdel -r user-name
Mention the username after the userdel command. Once you invoke the command, the Linux system will search for the username’s exact match and identify the final output that will be deleted in the process.
How verify user delete status in Linux
after running the
userdel command, you can check the exit code to verify the command execution status. You can understand the user delete status in the Linux system with the output value.
Run the following command after the
$ echo $?
These are the expected output values with the explanation.
0: success 1: can't update password file 2: invalid command syntax 6: specified user doesn't exist 8: user currently logged in 10: can't update group file 12: can't remove home directory
if the command exits as a success, it completely removes the user, user’s access to the server, user’s home directory, and the mail spool.
When deleting the user account without
-r option, you should note that the user home and mail spool directories are not removed.
Additionally, the user’s file stored in the other system would not be deleted using the above command. You need to search for the file and use the manual process to delete them.
User not removed after running the command
There are chances that the user will not be removed from the system if the user logged into the system. The user delete command will be terminated, and it will prompt the error message that the process is unsuccessful. The user has to be sign out before you delete the files from the system.
Suppose such a situation occurs where users are still log in, ask them to log out from the system. After the process is complete, you have to run the kill all process command to terminate all the background processes activated by the users.
killall command identifies the active processes related to the user and starts terminating them one by one.
$ sudo killall -u user-name
Once the process of killing all background files is done, you may remove the user from the system.
There are several other ways you can delete the user. The force delete is one of the options available in Linux.
When you run the -f force command in Linux, the command tells the system to find the given user name and remove it forcefully.
The command is effective and enables the administrator to remove the user’s account event forcefully. The user is logged into the system. All the running processes of the users will be terminated.
Available options with userdel command
-f: The f in the command stands for force removal. When you run this command, the selected username will be forcefully terminated as well as logged out from the system. All the processes running in the background also stopped. -r: If you want to remove the additional files such as the home directory and mail spool, then you should use this command. -Z: The command line would help you remove SELinux user mapping for the users.
All of the above commands are highly secure. It does the job smoothly without any error. Suppose any of the commands are not able to detect the users.
Kindly verify the user’s name again. The username use in the command line is case sensitive, so any typing mistake would result in an error while running the command.
Also, type the command line correctly. If there is a typo mistake, the command will not perform correctly, and the system will show an error while processing the data.
Each command performed on the Linux system is species. Once you know the command, you can easily control the Linux system and do whatever process you want to run on the system without any trouble. The Linux system ensures higher security to the stored data.
Moreover, Control the logged-in users and monitor the activity to avoid any malpractice on the system.
I hope the Linux guide has helped you learn how to delete the user’s account in Linux using the userdel command. The syntax is common for the Linux distribution, Ubuntu, Debian, and all other Linux-supported software and applications.