userdel

On this page 7

userdel is the low-level Linux command that removes a local user account by deleting its entries from /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow. In Linux permissions management, it closes the account lifecycle that begins with useradd and continues with usermod, and it requires root privileges, usually obtained through sudo.

The command belongs to the shadow-utils package (named passwd on Debian-based distributions) and acts only on accounts stored in local files. Deleting an account removes the name, but files owned by its numeric UID stay on disk unless the administrator handles them, which makes careful offboarding a security task rather than a single command.

How userdel removes an account

userdel works by editing the account databases under a lock, so concurrent tools such as passwd or usermod cannot corrupt them. It reads the login name, finds the matching records and rewrites each file without them, keeping backup copies such as /etc/passwd- for recovery.

The default run deletes only the account records. The user private group with the same name is removed as well when no other account uses it as a primary group and USERGROUPS_ENAB is set to yes in /etc/login.defs. The user is also dropped from every supplementary group listed in /etc/group.

The following table lists the files that userdel changes and the effect on each one.

FileContentChange made
/etc/passwdAccount name, UID, GID, home, shellLine for the user is deleted
/etc/shadowPassword hash and aging dataLine for the user is deleted
/etc/groupGroup names and member listsUser removed from member lists
/etc/gshadowGroup passwords and administratorsUser removed from entries
/etc/subuid and /etc/subgidSubordinate ID ranges for containersRanges for the user are released
Home directory and mail spoolPersonal files and mailDeleted only with the -r option
Files modified by userdel

Key userdel options and their effects

userdel has a small, deliberately conservative option set, because each option increases the amount of irreversibly destroyed data. The most consequential choice is whether to pass -r, which turns a record change into a file deletion operation.

OptionEffectTypical use
-r, –removeDeletes home directory and mail spoolFinal removal after data is archived
-f, –forceRemoves the account even if the user is logged inEmergency removal of a compromised account
-Z, –selinux-userRemoves the SELinux user mappingSystems that enforce SELinux policy
-R, –root CHROOT_DIRApplies changes inside a chroot directoryPreparing system images offline
-P, –prefix PREFIX_DIRUses account files under a prefix pathEditing mounted or alternate root trees
Common userdel options

Without -f, userdel refuses to remove an account that has running processes, returning exit status 8. This protection prevents a half-removed state in which processes keep running under a UID that no longer maps to a name. Even -r deletes only the home directory and the mail spool, so files elsewhere on the system remain and still carry the old UID.

userdel is one of several tools that manage accounts, and choosing the right one depends on whether removal must be permanent. Locking an account with usermod or passwd often serves offboarding better, because it preserves ownership information for audits.

ToolWhat it doesReversible
userdelDeletes account records, optionally home dataNo, without backups
deluserDebian wrapper that adds backup and policy optionsNo, but can archive files first
usermod -L -e 1Locks the password and expires the accountYes
passwd -lLocks only password authenticationYes, but SSH keys may still work
groupdelDeletes a group, not a userNo
Account removal and disabling tools

A frequent misconception is that passwd -l fully disables a user. It only prefixes the hash with an exclamation mark, so key-based SSH logins can still succeed. Setting an expiry date in the past or changing the shell to /usr/sbin/nologin blocks such access more reliably.

Safe offboarding with userdel

Safe use of userdel follows a fixed offboarding sequence: disable, inventory, archive, terminate processes and only then delete. Running these steps in order avoids lost data and orphaned files that later surprise other users.

  1. Lock the account with usermod -L -e 1 alice and remove its SSH keys.
  2. List the files it owns with find / -xdev -user alice on every mounted file system.
  3. Archive the home directory and any important owned files.
  4. Stop its processes and remove its cron jobs and at jobs.
  5. Run userdel -r alice and verify the result with getent passwd alice.

The decision table below matches common situations to a recommended approach.

SituationRecommended actionReason
Employee leaves, data needed laterLock now, archive, delete laterKeeps evidence and files available
Compromised accountLock, kill processes, then userdel -fStops access immediately
Temporary test accountuserdel -rNothing worth keeping
Service account with owned dataReassign files with chown firstPrevents orphaned ownership
Account from LDAP or FreeIPARemove it in the directory serviceuserdel edits local files only
Choosing a removal approach

Risks, orphaned files and common mistakes

The most serious risk of userdel is UID reuse. Files left behind keep the old numeric owner, and when useradd later assigns the same UID to a new person, that person silently inherits ownership of the old files, including private data. Many distributions avoid immediate reuse by allocating the next highest UID, but gaps can still be filled.

MistakeConsequenceRemedy
Deleting without archivingPermanent data lossBack up home and owned files first
Ignoring files outside homeOrphaned files show only a numberSearch with find -nouser and reassign
Forcing removal of a busy userProcesses run under an unnamed UIDKill processes before deletion
Removing a shared primary groupGroup ownership becomes numericCheck members of the group first
Editing /etc/passwd by handInconsistent shadow and group filesUse userdel or vipw with care
Mistakes and their remedies

Administrators should also remember that userdel does not revoke sudo rights defined by user name in sudoers files, nor entries in a Linux ACL. Stale names in these rules can later grant privileges to a new account that receives the same name, so auditing sudoers and ACLs belongs in every removal checklist.

The idea in brief

Think of a Linux computer as an office building with a guest list. userdel is the tool that crosses a person off that list, so they can no longer sign in. By default, it leaves their desk and boxes where they are; with one extra option it also clears out their personal locker.

The catch is that anything they left elsewhere in the building still carries their old badge number. If a newcomer later gets the same number, those leftovers suddenly belong to the newcomer. That is why careful administrators lock the account, save the files and tidy up first, and only then remove the person for good.

A practical scenario

A university research group runs a shared Ubuntu server for twelve doctoral students. When one student, with the login mkovac and UID 1007, finishes the degree, the system administrator first runs usermod -L -e 1 mkovac and deletes the student’s authorized SSH keys, so access ends that afternoon.

A search with find / -xdev -user mkovac reveals 40 GB of simulation results in /data/projects in addition to the home directory. The administrator archives the home directory to the group’s backup storage, changes ownership of the project data to the supervisor with chown, and removes the student’s crontab and a stale sudoers entry.

Two weeks later, after the supervisor confirms that nothing is missing, userdel -r mkovac removes the account. A final find / -nouser returns nothing, so no orphaned files remain for a future account with UID 1007 to inherit.