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:
- Help: Old Employees Accessing The Linux Server
- /etc/passwd - The basic attributes of users.
- /etc/shadow - The basic attributes of users password.
- /etc/group - The basic attributes of groups.
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop











{ 5 comments… read them below or add one }
The “userdel” command must be use at the end… not a the beginning.
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
thanks. this helped.
Thanks man! All your tutorials really help me when I’m managing my Linux systems. +1′d
Really, it helped . Thanks……..