Introduction: It is always a good idea to delete unwanted or unused user accounts on Ubuntu Linux or any other operating system for security reasons. This page shows how to remove a user account on Ubuntu Linux. You must log in as root delete a user from the Ubuntu Linux server.
How to delete a user account on Ubuntu
- Open the terminal app
- Login to server using the ssh user@server-ip-here command
- Run sudo deluser --remove-home userNameHere command to delete a user account on Ubuntu
- Verify it by running id command
Let us see all commands in details to remove a user account in Ubuntu.
Ubuntu delete user account command
Say you would like to delete a user named ubuntu, run:
$ sudo deluser --remove-home ubuntu
If you want to backup files before removing user account, try:
## create a dir to store backups ##
$ sudo mkdir /oldusers-data
$ sudo chown root:root /oldusers-data
$ sudo chmod 0700 /oldusers-data
$ sudo deluser --remove-home --backup-to /oldusers-data/ ubuntu
How to verify that user has been deleted from Ubuntu
Use the id command or grep command as follows:
$ id ubuntu
$ grep '^ubuntu' /etc/passwd
A note about /etc/deluser.conf file
The file /etc/deluser.conf contains defaults for the programs deluser and delgroup. You can edit this file to setup defaults. For example, you can set to remove the home directory and mail spool of the user to be removed by setting REMOVE_HOME to 1:
# Remove home directory and mail spool when user is removed REMOVE_HOME = 1 |
Use the cat command to display contains of /etc/deluser.conf:
$ cat /etc/deluser.conf
Sample outputs:
# /etc/deluser.conf: `deluser' configuration. # 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|afs)" |
How to remove a user account on Ubuntu Linux using userdel command
The syntax is:
$ sudo userdel -r {userName}
## remove tom user
$ sudo userdel -r tom
How to temporarily disable user login instead of deleting a user account
Use usermod command as follows:
$ sudo usermod -L -e 1 {username}
$ sudo usermod -L -e 1 jerry
You can specify expiry date too:
$ sudo usermod -e {YYYY-MM-DD} {username}
$ sudo usermod -e 2018-02-24 jerry
See “Linux Disable a User Account Command” for more info.
How to see log of deleted users on Ubuntu
You need to query /var/log/auth.log using either grep command or egrep command or cat command/tail command:
$ sudo tail -f /var/log/auth.log
$ sudo grep 'userdel' /var/log/auth.log
$ sudo grep 'ubuntu' /var/log/auth.log
Conclusion
You learned about deleting a user account on Ubuntu Linux when you no longer need them. I strongly suggest that you read man pages of deluser, userdel and deluser.conf by typing the following man command:
$ man userdel
$ man 5 deluser.conf
Hi,
I just wanted to remove user john from group sudo, I execute:
deluser rani sudo
Now rani can’t use command like:
sudo -i
sudo systemctl restart ngnix