How to Delete/Remove Users on Linux (userdel Command)
Introduction
Before we begin talking about how to delete/remove users on Linux (userdel Command), let's briefly understand – What is userdel command?
The userdel command is a Linux command used to delete user accounts. It allows administrators to remove user accounts, along with their associated files and directories.
By running this command, system resources can be effectively managed and user access can be revoked. Learn more about the userdel command and its usage in Linux systems.
In this tutorial, you will delete/remove users on Linux (userdel Command). We will also address a few FAQs on how to delete/remove users on Linux (userdel Command).
Advantages of userdel
Command
- Efficient resource management: Userdel allows administrators to easily remove user accounts, freeing up system resources.
- Security enhancement: Revoking access to user accounts ensures unauthorized access is avoided.
- File and directory cleanup: Userdel deletes associated files and directories, streamlining system organization.
- User privilege revocation: Userdel eliminates user privileges, preventing potential security breaches.
- Simplified user account management: The command makes it straightforward to remove unwanted user accounts from the system.
userdel
Command Syntax
The userdel
command has the following syntax:
userdel [OPTIONS] USERNAME
You must be logged in as root or a user with sudo
privileges to use the userdel
command to delete users.
How to Delete User in Linux
You would use the userdel
command to delete a user account named username:
sudo userdel username
The command reads the contents of the /etc/login.defs
file when run. The properties defined in this file override userdel's default behavior. Userdel
deletes the group with the same name as the user if USERGROUPS_ENAB
is set to yes in this file, but only if no other user is a member of the group.
The command deletes the user entries from the files /etc/passwd
and /etc/shadow
.
When a user account is deleted with userdel
in most Linux systems, the user's home and mail spool folders are not deleted.
To compel userdel
to remove the user's home directory and mail spool, use the -r
(--remove
) option:
sudo userdel -r username
The above command does not remove user files from other file systems. You must manually search for and delete the files.
If the user you intend to remove is still signed in, or if there are any running processes associated with this user, the userdel
command will not work.
In this case, it's best to log out the user and execute the killall command to kill all of the user's ongoing processes:
sudo killall -u username
After that, you can delete the user.
Another alternative is to use the -f
(--force
) option, which tells userdel
to remove the user account even if the user is still logged in or if the user has any ongoing processes.
sudo userdel -f username
FAQs to Delete/Remove Users on Linux (userdel Command)
Will userdel delete the user's home directory and files?
By default, userdel does not remove the home directory or files. Use the "-r" option to remove the user's home directory and associated files. Example: userdel -r username
.
Can I delete multiple user accounts with a single userdel command?
Yes, you can specify multiple usernames separated by spaces when using userdel. Example: userdel username1 username2
.
Is it possible to delete a user account without prompting for confirmation?
Yes, the "-f" option can be used to force deletion without confirmation. Example: userdel -f username
.
How can I check if a user account has been successfully deleted using userdel?
The userdel command does not provide a direct confirmation message. However, if no error message is displayed, the account has likely been deleted successfully.
Can I still access files owned by a deleted user account?
Yes, the files owned by a deleted user account can still be accessed by other users or the administrator, provided appropriate permissions are in place.
What happens to running processes associated with a deleted user?
Running processes are not affected by user deletion. They still continue to run until they are terminated or complete.
Can I use userdel to only revoke user login access without deleting the user account?
No, userdel is specifically designed to delete user accounts. To restrict login access, consider using usermod or modifying the account's settings.
Conclusion
You learned how to use the userdel
command on Linux to erase user accounts in this article. Any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, Fedora, and Arch Linux, uses the same syntax.
Because userdel
is a low-level function, Debian and Ubuntu users will want to use the more user-friendly deluser
command.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.