How to remove a Linux user
To remove users from the Linux, first, we have to display the user, the user’s data is placed in the /etc/passwd directory, this cat is displayed using the cat command:
Scroll down to view the list of the users available in the Linux:
We have four users in Linux that are maadi, hammad, john, and paul which can be seen in the above output. We can remove or delete the users from Linux at any time by using the simple Linux command whose general syntax is:
The explanation to the above syntax is:
- Use the userdel command to remove the user from Linux
- Use any flag in the place of [options] that perform any specified function like “-r” which removes the additional files of the users
- Replace the [username] with the actual user name which you want to remove or delete from Linux
There are three types of most common options used with the userdel command are:
-f | This option is used to forcibly remove the user from Linux, it terminates all the processes, logged out from the terminal, and finally removes the user permanently from Linux |
-r | This option is used to remove the files which are attached to the user |
-Z | SELinux user mapping is a security policy that is inherited by all Linux users, by using this option, you are independent of SElinux user mapping. |
Let’s remove the user,” Paul”, from the Linux system using the command “userdel” with the sudo:
To verify the status of the last executed command, run the following command:
The command “echo $?” has the following possible outcomes which explanations are explained:
Outcome | Explanation |
---|---|
0 | The command was executed successfully. |
1 | The file contains the password, which cannot be updated |
2 | The command entered has invalid syntax |
6 | The user-specified in the command doesn’t exist |
8 | The user-specified in the command is currently logged in |
10 | The file contains groups, which cannot be updated |
12 | Unable to remove the home directory |
Let’s assume there are some processes running in the background of user “john” due to which it is unable to remove it:
To resolve it, either forcibly remove the user using the option of “-f” with the userdel command or stop the processes. To stop the process, use the command:
Again, execute the command of userdel to delete the user john:
To verify the status, echo the command status:
The command is executed successfully.
Conclusion
In Linux, the user’s accounts that are not needed are just occupying the space and should be removed to make the space available for the new users. In Linux, this can be done very easily through the command-line, though the other option is still available and that is going through the Settings sections of the GUI of the Linux distribution and managing the users from there, the easy way to remove the users from Linux is explained in this write-up that is through the terminal. We just list down all users and remove the specified users which we want to remove by using the “userdel” command.