One of the most widely used operating systems in the tech world is Linux. It is a multi-user system that implies that multiple people can interact with each other with the same system at one time. As an administrator, you have to manage the users in groups of the system by creating users and assigning them to various groups.

Now, there are various Linux commands for beginners, advanced, and intermediate that you can execute or perform several functions. There may occur an instance where you wish to delete a user in Linux along with your account along with its associated files. In this article, you will get to know about how to remove users in Linux/ userdel command.

How to remove users in Linux?

To remove users or user accounts in Linux, you can use userdel command syntax. Here is the syntax of the same:

userdel [OPTIONS] USERNAME

To execute this command, you have to log in as a root user or a user with Sudo access.

It’s this simple!

Moving on, in case you wish to delete a user in Linux, you can do that too!

How to delete a user in Linux?

You have to use the userdel command again. For instance, if the username is abc, then you use the command as stated below:

userdel abc

Most commonly, when we talk about the Linux distributions, whenever you try to remove a user account with userdel command, both the mail spool directories as well as the user home are not removed. 

Now, to accomplish this task, you have to use the remove option that prompts userdel command to remove the home directory and mail spool as well. Take a look:

userdel -r username

There may be a case when the user that you want to remove is still logged in. In such a situation, you have an option to use the killall command. Here is the syntax:

sudo killall -u username

Execute this command, and the user will be removed. 

Alternatively, you can use the force option to ask the userdel command to remove the user account forcefully. In this case, the user gets killed even if the processes are running or the user is still logged in. 

Take a look at the command mentioned below:

userdel -f username

Moving on to our next segment, we are providing you with a complete procedure to remove a user account from Linux.

How to remove user accounts from Linux?

The first step is to lock the user account. To do that, here is the command:

passwd- l abc

Now, if the user tries to login the account, the following message is displayed on the screen:

Your account has expired; please contact your system administrator
Authentication failure

The next step is now to find and kill all the running processes of the user. For that, you have to move the process IDs of all the processes of the user. Use the command given below:

pgrep -u tecmint

Now that you have configured the process IDs. You have to list the processes on the basis of their username, terminal used, process state, parent process IDs, and process IDs. For a better understanding, here is a screenshot for you.

You can enhance your Linux skills by learning about the watch command, which allows you to run a command periodically and see the output in real-time. Check out our detailed guide on the Linux watch command to explore how it can improve your workflow.

Now, moving on! The next step is to use the killall commands as mentioned previously to kill the running processes.

After you execute this command there is another step that you can perform which is completely optional. 

The option here is to take a backup of the user’s files for any future reference, in case you want to review the user account details or files. 

You can use the other utilities for the purpose of creating a backup as shown below:

tar jcvf /user-backups/tecmint-home-directory-backup.tar.bz2 /home/tecmint

Lastly, now it is safe to remove Linux user altogether along with his directory with the help of the remove-all-files option. Here is the command for you:

deluser --remove-home tecmint      [On Debian and its derivatives]

userdel --remove tecmint           [On RedHat/CentOS based systems]

For those looking to enhance their server experience, consider using a Linux Dedicated Server for better performance and control.

Conclusion 

The world of Linux is like an ocean, the more you spend time, the more you explore. You must be aware that there are ample commands related to numerous functions.

In this article, we tried to cover our core topic to remove/delete user in Linux in various methods. We hope that this article is helpful in the best way possible! When you buy linux vps, it is important to consider factors such as the level of technical support provided, the security measures in place, and the cost of the hosting plan. we offer you best services.

People Are Also Reading: