A computer system in an organization can be operated by a number of users, if someone leaves the organization his/her account becomes useless, it should be removed or deleted from the system to save storage space. If the operating system is Linux-based, the users can be managed using the terminal conveniently. In this follow-up, we are going to explain some simple queries through which we can remove the users from Linux.

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-a-Linux-user-1.png" data-lazy- height="242" src="data:image/svg xml,” width=”1112″>

Scroll down to view the list of the users available in the Linux:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-a-Linux-user-2.png" data-lazy- height="266" src="data:image/svg xml,” width=”1083″>

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:

$ userdel [options] [username]

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-a-Linux-user-3.png" data-lazy- height="85" src="data:image/svg xml,” width=”850″>

To verify the status of the last executed command, run the following command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-a-Linux-user-4.png" data-lazy- height="121" src="data:image/svg xml,” width=”811″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-a-Linux-user-5.png" data-lazy- height="116" src="data:image/svg xml,” width=”852″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-a-Linux-user-6.png" data-lazy- height="88" src="data:image/svg xml,” width=”814″>

Again, execute the command of userdel to delete the user john:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-a-Linux-user-7.png" data-lazy- height="90" src="data:image/svg xml,” width=”819″>

To verify the status, echo the command status:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-a-Linux-user-8.png" data-lazy- height="119" src="data:image/svg xml,” width=”661″>

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.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/hammad–150×150.jpg61c81f1c38c85.jpg" height="112" src="data:image/svg xml,” width=”112″>

Hammad Zahid

I’m an Engineering graduate and my passion for IT has brought me to Linux. Now here I’m learning and sharing my knowledge with the world.