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 commands that you can execute or perform several functions in Linux. There may occur an instance where you wish to remove a user along with your account along with its associated files.
In this article, you will know about one of the methods of removing user accounts in Linux.
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, the username is abc, then you use the command as stated below:
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 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
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 as 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, process IDs. For a better understanding, here is a snapshot for you.
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 the 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]
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 of removing a user in Linux in several ways.
We hope that this article is helpful in the best way possible!
Keep learning! Keep exploring!