LinuxCommandLibrary

userdel

Delete user accounts

TLDR

Remove a user

$ sudo userdel [username]
copy

Remove a user in other root directory
$ sudo userdel [[-R|--root]] [path/to/other/root] [username]
copy

Remove a user along with the home directory and mail spool
$ sudo userdel [[-r|--remove]] [username]
copy

SYNOPSIS

userdel [options] LOGIN

PARAMETERS

-r, --remove
    Removes the user's home directory and mail spool. This option also removes the user's primary group if it contains no other members.

-f, --force
    Forces the removal of the user account, even if the user is currently logged in. Use with extreme caution as this can lead to system instability.

LOGIN
    The username of the account to be deleted. This is a mandatory argument.

DESCRIPTION

The userdel command is used to delete a user account from the Linux system. By default, it removes the user's entry from system account files like /etc/passwd and /etc/shadow, and removes the user from any groups they are a member of. However, it does not remove the user's home directory or mail spool unless explicitly requested with the -r option. This default behavior is a safety measure to prevent accidental data loss. To successfully remove a user, it's generally best practice to ensure the user is not logged in and has no running processes. If the user's primary group has no other members after the user's deletion, the group might also be removed, depending on system configuration. The command requires root privileges to execute, as it modifies critical system files. It's a fundamental tool for system administrators to manage user lifecycles on a Linux machine, helping maintain system security and organization by removing obsolete or unauthorized accounts. Care should always be taken when using userdel, especially with the -r option, to avoid unintentional data deletion or system instability.

CAVEATS

The user should not be logged in and have no running processes to ensure a clean deletion, unless the -f (force) option is used.
Using the -r option will permanently delete the user's home directory and mail spool, which is irreversible. Backups are recommended.
If the user's primary group is unique and has no other members, it might also be deleted.
Files and directories owned by the user outside of their home directory will not be affected or removed by userdel. These may need to be manually cleaned up.
Reusing a deleted user's UID (User ID) can pose security risks, as new accounts might inadvertently gain access to old files still owned by that UID.

AFFECTED FILES

/etc/passwd: User account database.
/etc/shadow: Secure user account information (hashed passwords).
/etc/group: Group account information.
/etc/gshadow: Secure group account information.
/etc/login.defs: System-wide configuration for user management.

EXIT STATUS

0: Success.
1: Cannot update password file.
2: Invalid command syntax.
6: Specified user doesn't exist.
8: User currently logged in.
10: Cannot update group file.
12: Cannot remove home directory.
14: Cannot update SELinux user mapping.

HISTORY

The userdel command is a fundamental part of standard Unix and Linux system administration, particularly for managing user accounts. Its evolution is closely tied to the development of the shadow password suite (often part of the shadow-utils package), which enhanced the security of user information by separating hashed passwords into a restricted file (/etc/shadow). Over time, its core functionality has remained consistent: safely removing user entries from system authentication files. Its design reflects best practices for system security and data integrity by requiring explicit actions (like the -r option) for data deletion and preventing deletion of active users by default.

SEE ALSO

useradd(8), usermod(8), passwd(1), groupadd(8), groupdel(8), id(1), getent(1), chown(1)

Copied to clipboard