≡ Menu

Linux: Delete / Remove User Account

How do I remove a user’s access from my server? How do I delete a user account under Linux operating systems include home directory and running cron jobs?

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 [options] userName
userdel -r userName

userdel command examples

To remove the user named vivek or account named vivek from the local Linux system / server / workstation, enter:
# userdel vivek
To remove the user’s home directory and mail spool 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. Pass the –-f option orce removal of files, even if not owned by user:
# userdel -r -f vivek
To remove any SELinux user mapping for the user pass the -Z option:
# userdel -Z -r -f vivek

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 to remove user account from Linux

The following is recommend procedure to delete a user from the Linux server. First, lock user account, enter:
# passwd -l vivek
OR set the date on which the user account will be disabled (syntax is usermod --expiredate YYYY-MM-DD userNameHere):
# usermod --expiredate 1 vivek
If user try to login, he or she will get the following message:

Your account has expired; please contact your system administrator
Authentication failure

Next, 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. Tye 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

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 {} \;
Finally, delete user account called vivek, enter:
# userdel -r vivek
Sample session:

Fig.01: Delete User Accounts with Home Directory and All Data In Linux

Fig.01: Delete User Accounts with Home Directory and All Data In Linux


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:

Share this tutorial on:
{ 12 comments… add one }
  • logicos June 26, 2011, 5:41 pm

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

  • Server management October 2, 2011, 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.

    • Umair November 27, 2014, 10:48 pm

      Hey,

      How would you kill all the process from a user before deleting?

  • Mike DePaulo February 29, 2012, 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

  • bagheera July 3, 2012, 9:21 am

    thanks. this helped.

  • Joshua Garde August 1, 2012, 10:16 am

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

  • Vineet Roy September 2, 2012, 12:56 pm

    Really, it helped . Thanks……..

  • Sushil Panchal January 29, 2013, 6:47 pm

    very useful notes
    Thanks

  • Magesh M January 3, 2014, 9:58 am

    @ Server management,

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

  • L. Priyambodo January 28, 2014, 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?

  • Wolf September 11, 2015, 4:42 pm

    Article refers to disabling user account with passwd -l. This will stop someone’s password from working but if they have access to the machine by some other auth method this won’t prevent access. According to passwd manpage, you should instead use usermod –expiredate 1 (this set the account’s expire date to Jan 2, 1970)

  • shashi April 14, 2016, 6:06 am

    Nice tutorial, thanks .

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: , , , , , , , , , , , ,