Blog

03/06/2019

How to delete a user account on Ubuntu Linux



My AWS server has a user named ubuntu. I created another user for admin purpose. I no longer need a user account named ubuntu on Ubuntu Linux 18.04 LTS cloud server. How do I delete that user account?

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

kbd { display: inline-block; margin: 0 .1em; padding: .1em .6em; font-family: Arial,”Helvetica Neue”,Helvetica,sans-serif; font-size: 11px; line-height: 1.4; color: #242729; text-shadow: 0 1px 0 #FFF; background-color: #e1e3e5; border: 1px solid #adb3b9; border-radius: 3px; box-shadow: 0 1px 0 rgba(12,13,14,0.2), 0 0 0 2px #FFF inset; white-space: nowrap;}
  1. Open the terminal app
  2. Login to server using the ssh user@server-ip-here command
  3. Run sudo deluser --remove-home userNameHere command to delete a user account on Ubuntu
  4. 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

Deleting a user account in Ubuntu Linux

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

# 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)"

# /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

Removing a user account and see log of deleted user account on Ubuntu with grep

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

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

20/08/2019

Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...
14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....