Linux: Delete / Remove User Account

by on June 16, 2011 · 9 comments· LAST UPDATED June 16, 2011

in

How do I remove a user's access from my server? How do I delete a user account under Linux operating systems?

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.

See also:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 9 comments… read them below or add one }

1 logicos June 26, 2011 at 5:41 pm

The “userdel” command must be use at the end… not a the beginning.

Reply

2 Server management October 2, 2011 at 7:02 pm

Before deleting a user (userdel) you should make sure that no processes running from this user. Otherwise, you can’t delete this user.

Reply

3 Mike DePaulo February 29, 2012 at 6:36 pm

I believe you are mistaken about ubuntu/debian & deluser.conf.
basically, “adduser” =! “useradd”

Ubuntu & debian have two config files:
deluser.conf
adduser.conf
which configure these 4 high-level debian-specific account management utilities
adduser
addgroup
deluser
delgroup

Debian/Ubuntu still have useradd & userdel, but I don’t see those options in my login.defs & Debian officially discourages the use of useradd & userdel anyway.
http://www.debian.org/doc/manuals/system-administrator/ch-sysadmin-users.html

Reply

4 bagheera July 3, 2012 at 9:21 am

thanks. this helped.

Reply

5 Joshua Garde August 1, 2012 at 10:16 am

Thanks man! All your tutorials really help me when I’m managing my Linux systems. +1′d

Reply

6 Vineet Roy September 2, 2012 at 12:56 pm

Really, it helped . Thanks……..

Reply

7 Sushil Panchal January 29, 2013 at 6:47 pm

very useful notes
Thanks

Reply

8 Magesh M January 3, 2014 at 9:58 am

@ Server management,

We can use the “userdel -rf username” to do it.

Reply

9 L. Priyambodo January 28, 2014 at 12:40 am

I cannot kill
username 2516 1602 0 01:32 ? 00:00:00 php-fpm: pool username

everytime I kill it, it spawned with new PID
Is it safe to stop php5-fpm first then delete the username?

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , ,

Previous Faq:

Next Faq: