Linux Commands

How to remove a Linux user

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:

$ cat /etc/passwd

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:

$ 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:

$ sudo userdel paul

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

$ echo $?

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:

$ sudo userdel john

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:

$ sudo killall -u john

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

$ sudo userdel john

To verify the status, echo the command status:

$ echo $?

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

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.