≡ 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?

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 all files on the system owned by the user to be removed
# Backup files before removing them. This options has only an effect if
# target directory for the backup file
# delete a group even there are still users in this group
# 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:

  • 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


      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:
    which configure these 4 high-level debian-specific account management utilities

    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.

  • 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

  • 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: , , , , , , , , , , , , ,