As a system administrator, a time will come when you’ll need to delete or remove users in your organization’s Linux system. It could be due to security reasons, routine account management, or organizational changes that require you to remove inactive accounts or offboard employees. Whatever the reason, it's important to do this properly to avoid problems like broken processes, orphaned files, and security vulnerabilities.
User accounts on Linux fall into four categories: root, system, service, and regular. When you create users (regular and system), Linux systems usually list or store them in a system file called /etc/passwd. Password data for these users is stored in the /etc/shadow file in encrypted formats.
There are three main ways to safely remove Linux users:
userdel is a standard Linux utility that lets you delete users and, if necessary, associated or related account files, such as the home directory. It does so by modifying the system account files and deleting entries associated with the username LOGIN.
The basic syntax for this command is:
userdel
username
Where:
There are several options you can use with the userdel command, including:
It’s best to first confirm the existence of the users before deleting them:
cat users_to_delete.txt | xargs -I {} id {}
Once you’ve verified the users, you can run the userdel command.
Note: userdel -r does not remove files outside the home directory (e.g. files owned by the user in /var or /tmp). If you need to remove such files, you can use find / -user to locate them.
In Debian Linux-based systems, the deluser command is preferred because of its user-friendliness. It removes a user account and all its user-to-group connections.
The basic syntax for this command is:
sudo deluser username
You can incorporate options into the deluser command, such as:
This method gives you complete control over the deletion process, especially in cases where there are system inconsistencies or corrupted accounts. Here are the steps:
Before deleting the user account you want to remove, you may need to stop all the activities or processes it may be running. Use the following command:
sudo pkill-u
Where:
First, you need to open the files. To do so for each, you can use these commands:
Once the file is open, find the line containing the user and remove it.
Note: You should replace nano with the text editor you’re using, like vim or vi.
Additionally, directly editing these files can lead to system inconsistencies. It’s recommended to use vipw and vigr instead, as they lock the files to prevent corruption.
If you also want to delete the user’s home directory, you can use this command:
sudo rm -rf /home/
Where:
Next, you may need to delete all files owned by the user. If you do, you can use this command:
find / -user
-exec rm -rf {} \;
Where:
While powerful, this command can cause inconsistencies in your system if executed incorrectly. Therefore, it’s best to run find / -user first without -exec rm -rf {} \; to review what you want to delete.
It’s possible to delete a user but keep the files they owned, especially if you need them for reassignment or archival purposes. To do so, you can either:
First, ensure the backup directory exists:
sudo mkdir -p /backup
Then back up the user's files:
sudo tar -cvzf /backup/
_backup.tar.gz /home/
sudo chown -R newuser:newuser /home/
After deleting a user, you may need to verify that you’ve done so successfully. You can either:
For each, replace with the actual name of the user account.
If you also need to verify that you’ve deleted the user account’s home directory, you can run the following command:
ls -ld /home/
You may experience errors while deleting users in Linux, including:
This will usually occur if you try to remove a user actively doing something in the system. You can solve this by terminating their processes using the command below:
sudo killall -u
If you get this error after you delete a user and check whether you were successful, you can force-delete the user by running this syntax:
sudo userdel -f
Note: -f does not remove user-owned files; it only removes the account entry. Use find / -user to locate and clean up files after deletion.
This error can occur if the home directory is immutable. If this is the case, you cannot modify or delete it. To counter this:
User management is a time-consuming and sensitive process. One mistake can introduce vulnerabilities into your system and even impact system stability. You can avoid these risks by leveraging tools like Delinea, which provide:
Ready to easily and securely manage users in Linux? Book a demo today.