The Wayback Machine - https://web.archive.org/web/20240722132059/https://www.geeksforgeeks.org/userdel-command-in-linux-with-examples/
Open In App

How to Delete User in Linux | userdel Command

Last Updated : 17 Jul, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Managing user accounts is an essential aspect of Linux system administration. Whether you need to remove an unused account, revoke access for a departing employee, or clean up your system for security reasons, understanding how to delete a user in Linux is a crucial skill. In this guide, we will explore the userdel command, a powerful tool in Linux for removing user accounts.

How to Delete User in Linux using `userdel` Command

userdel command in Linux system is used to delete a user account and related files. This command basically modifies the system account files, deleting all the entries which refer to the username LOGIN. It is a low-level utility for removing the users. The userdel command is a standard utility in Linux that allows administrators to delete user accounts from the system. It is a part of the user management tools that also include commands like useradd and usermod. The userdel command not only removes the user account but also takes care of associated files, such as the home directory and mail spool.

Syntax of `userdel` Command

The basic syntax of the userdel command is as follows:

userdel [options] username

Here,

  • options: Various command options can be used to customize the behavior of user deletion.
  • username: Specifies the name of the user account to be deleted.

Options Available in `userdel` Command

Option

Description

-f

Force removal of the user account, including home directory and mail spool, even if the user is logged in.

-r

Remove the user’s home directory along with the account. Useful for a complete cleanup.

-h

Display a help message and exit, providing information on command syntax and available options.

-R

Apply changes in the specified CHROOT_DIR, useful for user deletion operations within a chroot environment.

-Z

Remove SELinux user mapping for the user’s login, applicable in SELinux-enabled systems.

-help

Display a help message with the general syntax and available options for the userdel command.

Pratical Implementaion of How to Delete User in Linux

1. How to Delete User in Linux

To delete a user using the userdel command, open a terminal and type:

sudo userdel username

Replace “username” with the actual username you want to remove. The ‘sudo’ command is used to execute the userdel command with administrative privileges.

2. How to Forcely Delete User in Linux

userdel -f: This option forces the removal of the specified user account. It doesn’t matter that the user is still logged in. It also forces the userdel to remove the user’s home directory and mail spool, even if another user is using the same home directory or even if the mail spool is not owned by the specified user.

Example:

sudo userdel -f neuser

3. How to Delete User in Linux with Home Directory and Mail Spool

userdel -r: Whenever we are deleting a user using this option then the files in the user’s home directory will be removed along with the home directory itself and the user’s mail spool. All the files located in other file systems will have to be searched for and deleted manually.

Example:

sudo userdel -r newuser2

4. Display Help Message and Exit

userdel -h : This option display help message and exit.

Example:

userdel -h

5. Apply Changes in CHROOT_DIR

userdel -R: This option apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory. Useful for user deletion operations within a chroot environment.

Example:

sudo userdel -R newuser2

6. Remove SELinux User Mapping

userdel -Z : This option remove any SELinux(Security-Enhanced Linux) user mapping for the user’s login.

Example:

sudo userdel -Z newuser2

7. User Deletion with Help Option

userdel command with help option: The userdel command throws an error if no options, filename or arguments are passed. So, when we use the -h option, it gives the general syntax along with the various options that can be used with the userdel command.

Example:

How to Delete User in Linux | userdel Command – FAQs

How to delete a user account in Linux while they are still logged in?

We can use the `userdel -f` option to forcefully remove a user account, even if the user is currently logged in. This option terminates active processes associated with the user.

What happens if I use the `-r` option with userdel?

When you use the `userdel -r` option, it not only deletes the user account but also removes the user’s home directory and mail spool. This ensures a complete cleanup of the user’s files.

How to delete user without deleting their home directory?

By default, `userdel` retains the user’s home directory. If you want to keep the home directory intact, omit the `-r` option. This preserves the home directory even after the user account is deleted.

How to delete a user in a chroot environment using userdel?

We can use the `userdel -R` option to apply changes in a specified `CHROOT_DIR` directory. This is useful when performing user deletion operations within a chroot environment.

What is SELinux, and how does the `-Z` option relate to user deletion?

SELinux (Security-Enhanced Linux) is a security module in Linux. The userdel -Z option removes any SELinux user mapping for the specified user’s login. This is relevant in SELinux-enabled systems for maintaining security policies.

Conclusion

In this article we discussed how to delet user accounts in Linux which is a crucial task for system administrators. The userdel command is a handy tool for this job, efficiently handling user deletions along with associated files like home directories. With options like -f for forceful removal and -r for a thorough cleanup, it provides flexibility. The practical steps outlined in this guide make user deletion straightforward. The FAQs offer insights into common queries, such as deleting users while they’re logged in or preserving home directories. This guide equips administrators with the knowledge needed to manage user accounts effectively, ensuring system security and resource optimization.



Previous Article
Next Article

Similar Reads

How to add User in Linux | useradd Command
useradd is a command in Linux that is used to add user accounts to your system. It is just a symbolic link to adduser command in Linux and the difference between both of them is that useradd is a native binary compiled with the system whereas adduser is a Perl script that uses useradd binary in the background. It makes changes to the following file
5 min read
How to Change User Password in Linux | passwd Command
Securing user accounts is a fundamental aspect of maintaining a robust and secure Linux system. One essential task is changing user passwords regularly to prevent unauthorized access. The passwd command in Linux provides a straightforward and effective way to modify user passwords. This article will guide you through the process of changing user pa
8 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
How to Delete a Group in Linux | groupdel command
Group management is a crucial aspect of Linux system administration, and understanding how to create, modify, and delete groups is essential for maintaining a secure and organized environment. In this article, we will delve into the process of deleting a group in Linux using the 'groupdel' command. We will explore the command syntax, options, and p
3 min read
GithubFind3r - Fast Command Line Repo/User/Commit Search Tool
GithubFind3r is an automated tool developed in Node JS which is a CLI-based tool used to get information about the Repositories, Commits, and information about the Users from GitHub. GithubFind3r is available on GitHub, it's free and open-source to use. The GithubFind3r tool displays the results in a more sophisticated and more attractive manner. N
2 min read
How to create a Shared Folder between two Local User in Linux?
This article shows how to set-up a shared folder between two local users in Linux. The shared directory/folder will be accessible to both the users, they will be able to read/write each other's file. Let us create shared directory /home/shareFolder for user Bob and Alice and add them to a common group named projectA. Note: You can create the uses B
2 min read
Configure Passwordless Sudo For A Specific User in Linux
Linux has a tough permission rule. Being a root user you are permitted to do anything however the normal user has only some allowed permissions. To run many other commands, they required to ask permissions from the superuser, using keyword sudo after which they need to enter the password. This requirement of the password after sudo command can be r
1 min read
User Management in Linux
A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system. In this post, we will learn about users and commands which are used to get information about the users. After installation of the operating system, the ID
4 min read
Anonymity and Privacy For Linux User
Today, more than 3 billion of the world's population is connected to the virtual world known as the internet in some way or another. And the size of the hard-wares and data centers is on the increase every year due to insufficient users' information holding capacity. The lack of awareness and knowledge behind the proper functioning of the internet
6 min read
three90RightbarBannerImg