userdelDelete a user account
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.
userdel [-r] [-f] LOGINRahul has left. Back up his home, end his sessions and delete the account.
sudoDeleting accounts rewrites the account databases, which needs root.
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.
-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 loginfind / -xdev -nouserCompanion command: list files whose owner no longer existspkill -KILL -u USERCompanion command: end the user's processes firstsudo 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 USERAssuming 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 listsDeleting the newest user and creating a new one straight afteruseradd 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 valuesTry these on a Linux machine of your own. Each task has a hint and a solution.
Create user temp2 with a home, then delete it together with its home directory and confirm the home is gone.
Create user temp3, give it a file in /tmp, delete the account WITHOUT -r, and show the orphaned file.
Find every file on the root filesystem whose owner no longer exists.
userdelDelete a user account
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.
userdel [-r] [-f] LOGINRahul has left. Back up his home, end his sessions and delete the account.
sudoDeleting accounts rewrites the account databases, which needs root.
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.
-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 loginfind / -xdev -nouserCompanion command: list files whose owner no longer existspkill -KILL -u USERCompanion command: end the user's processes firstsudo 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 USERAssuming 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 listsDeleting the newest user and creating a new one straight afteruseradd 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 valuesTry these on a Linux machine of your own. Each task has a hint and a solution.
Create user temp2 with a home, then delete it together with its home directory and confirm the home is gone.
Create user temp3, give it a file in /tmp, delete the account WITHOUT -r, and show the orphaned file.
Find every file on the root filesystem whose owner no longer exists.