Linux: Delete / Remove User Account

You need to use the userdel command to delete a user account and related files from user account. The userdel command must be run as root user. The syntax is as follows:

 
userdel userName

userdel Example

To remove the user vivek account from the local system / server / workstation, enter:
# userdel vivek
To remove the user’s home directory pass the -r option to userdel, enter:
# userdel -r vivek
The above command will remove all files along with the home directory itself and the user’s mail spool. Please note that files located in other file systems will have to be searched for and deleted manually.

A Note About /etc/login.defs File

Default values are taken from the information provided in the /etc/login.defs file for RHEL (Red Hat) based distros. Debian and Ubuntu Linux based system use /etc/deluser.conf file:

 
# /etc/deluser.conf: deluser configuration - Debian / Ubuntu Linux only.

# Remove home directory and mail spool when user is removed
REMOVE_HOME = 0

# Remove all files on the system owned by the user to be removed
REMOVE_ALL_FILES = 0

# Backup files before removing them. This options has only an effect if
# REMOVE_HOME or REMOVE_ALL_FILES is set.
BACKUP = 0

# target directory for the backup file
BACKUP_TO = "."

# delete a group even there are still users in this group
ONLY_IF_EMPTY = 0

# exclude these filesystem types when searching for files of a user to backup
EXCLUDE_FSTYPES = "(proc|sysfs|usbfs|devpts|tmpfs)"

Complete Example

The following is recommend procedure to delete a user from the Linux server. First, lock user account, enter:
passwd -l username

Backup files from /home/vivek to /nas/backup
# tar -zcvf /nas/backup/account/deleted/v/vivek.$uid.$now.tar.gz /home/vivek/
Please replace $uid, $now with actual UID and date/time. userdel command will not allow you to remove an account if the user is currently logged in. You must kill any running processes which belong to an account that you are deleting, enter:
pgrep -u vivek
# ps -fp $(pgrep -u vivek)
killall -KILL -u vivek

To delete user account called vivek, enter:
# userdel -r vivek

Delete at jobs, enter
# find /var/spool/at/ -name "[^.]*" -type f -user vivek -delete

To remove cron jobs, enter:
# crontab -r -u vivek

To remove print jobs, enter:
# lprm vivek

To find all files owned by user vivek, enter:
# find / -user vivek -print

You can find file owned by a user called vivek and change its ownership as follows:
# find / -user vivek -exec chown newUserName:newGroupName {} \;

You can automate the entire procedure by writing a shell script (to remove any at/cron/print/file jobs etc), which is left as an exercise to the readers.

Advertisements

Share your thoughts with us!!!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s