What it does
userdel removes the user from the account files, and the private group of the user if no other user has it as primary group.
Without -r the home folder stays. Files of the user in other places always stay: they then show a number instead of a name as owner. A new user with the same UID would own them, so find and clean them first.
How to write it
userdel [OPTION]... LOGINOptions 6
Most used
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1Delete a user and the home folder
userdel -r contractor1The usual way for a lab or a test account.
2Delete only the account
userdel olduserThe files in /home/olduser stay (for a backup).
3Find files of a user before you delete it
find / -xdev -user olduser 2>/dev/nullKeep or move important files first.
4Find files without an owner after a delete
find / -xdev -nouser 2>/dev/nullA security check: orphan files can be taken over by a new user with the same UID.
5Check that nobody uses the account
userdel refuses when the user has processes (exit code 8).
6Stop the processes, then delete
pkill -KILL -u olduser; userdel -r olduserOnly when you are sure the work is saved.
7Offboarding: lock first, delete later
usermod -L -e 1 olduserWait some weeks, then
userdel -r olduser.8Remove the cron jobs of the user
crontab -r -u olduseruserdel does not always remove them on every system.
9Archive the home folder before the delete
tar -czf /root/olduser-home.tgz -C /home olduser && userdel -r olduserKeep a copy for the legal hold period.
10Delete a service account
userdel myappIts data in /var/lib/myapp stays; remove it yourself.
11Check that it is gone
id olduser id: 'olduser': no such userThe account is removed.
Nothing matches this filter.
Watch out
- Files outside the home folder are not removed.
-fcan remove a group that other users still use as primary group.
Files
/etc/passwd, /etc/shadow, /etc/group, /etc/gshadow- The account files.
/etc/login.defs- USERGROUPS_ENAB decides if the private group is removed.
Exit codes
0 :: Done. 1 :: Could not update the password file. 2 :: Wrong syntax. 6 :: The user does not exist. 8 :: The user is logged in. 10 :: Could not update the group file. 12 :: Could not remove the home folder.