Linux Commands

How to Delete a User in Linux

Linux is a stable, secure, performance-oriented, and customizable operating system that has gained users exponentially over time. It is because of its remarkable features and the numerous tasks that one can perform within Linux.

Linux is a multi-user system which allows multiple users to work on a single OS simultaneously. This feature helps you maintain a hierarchical workflow but sometimes raises some safety concerns.

Therefore, you must use the proper user management measures to safeguard your data and prevent it from unintended consequences. However, some users don’t know how to delete a user from the system. So, in this quick tutorial, we will list the easy methods to delete a user in Linux.

How to Delete a User in Linux

First, you must be the root user or have the similar privilege to add or delete the users through the “userdel” command.

sudo userdel username

 
Replace the word “username” with the user that you want to delete. For instance, let’s remove an inactive username from our system using the previous method.

sudo userdel Shawn

 

Although the previous command deletes the users, their home directory stays intact. If you intend to delete a user entirely along with their directory, use the following:

sudo userdel -r Shawn

 

Here, the “-r” or “—remove” option with the “userdel” command further instructs the system to delete the home directory. The previous command will not work in the following conditions:

    • The specified user is active on the system.
    • That user has some processes running in the background.

However, you can delete the users without letting them finish their ongoing work. You need to terminate their session or kill their unfinished processes by running the “pkill” command or using the “-f” option with the “userdel” command. We recommend the “-f” method as it directs the system to remove the users forcefully even when they are active.

sudo userdel -f Shawn

 

Apart from kicking out a user from the system, deleting a user in Linux can also mean removing them from a particular group of users.

When the group has specific permissions that are not meant for that user, you must use the following command in this case:

gpasswd -d username group

 

    • The “-d” is the option to remove a user from a group.
    • In place of “group”, mention the name of the targeted group. You can enter multiple groups by separating them with a space in between.

You can also delete a user from two groups.

gpasswd -d username group1 group2

 

Conclusion

Deleting a user in Linux is essential for safety and management purposes. In this blog, we discussed how to delete the users and their home directories, terminate their ongoing processes (if any), and more. Furthermore, we also explained the method to remove them if you intend not to delete the user but to remove them from a single or multiple groups.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.