CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Users & Groups / userdel

userdel

Delete a user account

Beginnerusersadminoffboarding

userdel removes an account from /etc/passwd, /etc/shadow and /etc/group. With -r it also deletes the home directory and mail spool. Files the user owned anywhere else stay behind, owned by a bare number that the next new user may inherit.

Syntaxuserdel [-r] [-f] LOGIN

Watch it run

Rahul has left. Back up his home, end his sessions and delete the account.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 5
sudo userdel -r rahul

userdel checks /proc first and refuses while anything runs as rahul: a forgotten tmux session, a cron job or an SSH session.

Anatomy of a command

$
prefixsudo

Deleting accounts rewrites the account databases, which needs root.

How it works

userdel first scans /proc for processes running as the user and refuses if it finds any (unless -f). It then removes the user's lines from /etc/passwd and /etc/shadow, deletes the user's private group if no other account uses it as primary group, and drops the name from the member lists in /etc/group and /etc/gshadow. With -r it recursively removes the home directory and /var/mail/LOGIN; many distros also remove the user's crontab and at jobs. It does not search the filesystem: files in /srv, /tmp, /var/www or a data volume keep the raw UID in their inode, so ls shows a number instead of a name. useradd picks the highest UID in use plus one, so if you delete the newest account, the next person you create gets the same UID and silently owns all those orphaned files.

Options you'll actually use

-rRemove the home directory and mail spool too
-fForce: delete even if the user is logged in or has processes
-ZRemove any SELinux user mapping for the login
find / -xdev -nouserCompanion command: list files whose owner no longer exists
pkill -KILL -u USERCompanion command: end the user's processes first

Common mistakes

sudo userdel -f -r user as the first step

-f deletes the account while processes still run as it, leaving them running under a dead UID. -r destroys the home with no backup.

Check ps -u USER, archive the home, pkill -u USER, then userdel -r USER
Assuming userdel -r removes all the user's files

-r only removes the home directory and mail spool. Files in /srv, /var/www, /tmp and data volumes stay, owned by the old UID.

sudo find / -xdev -uid UID before deleting, then chown or remove what it lists
Deleting the newest user and creating a new one straight after

useradd takes highest UID + 1, so the new user gets the old UID and inherits every orphaned file.

Re-own or delete orphans first, or create users with explicit -u values

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create user temp2 with a home, then delete it together with its home directory and confirm the home is gone.

  2. 2

    Create user temp3, give it a file in /tmp, delete the account WITHOUT -r, and show the orphaned file.

  3. 3

    Find every file on the root filesystem whose owner no longer exists.

Related commands

useraddCreate a user accountusermodChange an existing user accountchownChange file owner and groupfindSearch the filesystem by name, size, age, owner and more
PrevioususermodNextpasswd
Linux / Users & Groups / userdel

userdel

Delete a user account

Beginnerusersadminoffboarding

userdel removes an account from /etc/passwd, /etc/shadow and /etc/group. With -r it also deletes the home directory and mail spool. Files the user owned anywhere else stay behind, owned by a bare number that the next new user may inherit.

Syntaxuserdel [-r] [-f] LOGIN

Watch it run

Rahul has left. Back up his home, end his sessions and delete the account.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 5
sudo userdel -r rahul

userdel checks /proc first and refuses while anything runs as rahul: a forgotten tmux session, a cron job or an SSH session.

Anatomy of a command

$
prefixsudo

Deleting accounts rewrites the account databases, which needs root.

How it works

userdel first scans /proc for processes running as the user and refuses if it finds any (unless -f). It then removes the user's lines from /etc/passwd and /etc/shadow, deletes the user's private group if no other account uses it as primary group, and drops the name from the member lists in /etc/group and /etc/gshadow. With -r it recursively removes the home directory and /var/mail/LOGIN; many distros also remove the user's crontab and at jobs. It does not search the filesystem: files in /srv, /tmp, /var/www or a data volume keep the raw UID in their inode, so ls shows a number instead of a name. useradd picks the highest UID in use plus one, so if you delete the newest account, the next person you create gets the same UID and silently owns all those orphaned files.

Options you'll actually use

-rRemove the home directory and mail spool too
-fForce: delete even if the user is logged in or has processes
-ZRemove any SELinux user mapping for the login
find / -xdev -nouserCompanion command: list files whose owner no longer exists
pkill -KILL -u USERCompanion command: end the user's processes first

Common mistakes

sudo userdel -f -r user as the first step

-f deletes the account while processes still run as it, leaving them running under a dead UID. -r destroys the home with no backup.

Check ps -u USER, archive the home, pkill -u USER, then userdel -r USER
Assuming userdel -r removes all the user's files

-r only removes the home directory and mail spool. Files in /srv, /var/www, /tmp and data volumes stay, owned by the old UID.

sudo find / -xdev -uid UID before deleting, then chown or remove what it lists
Deleting the newest user and creating a new one straight after

useradd takes highest UID + 1, so the new user gets the old UID and inherits every orphaned file.

Re-own or delete orphans first, or create users with explicit -u values

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create user temp2 with a home, then delete it together with its home directory and confirm the home is gone.

  2. 2

    Create user temp3, give it a file in /tmp, delete the account WITHOUT -r, and show the orphaned file.

  3. 3

    Find every file on the root filesystem whose owner no longer exists.

Related commands

useraddCreate a user accountusermodChange an existing user accountchownChange file owner and groupfindSearch the filesystem by name, size, age, owner and more
PrevioususermodNextpasswd