totn Linux

Linux: How to Remove a User in Ubuntu Linux Server 16.04 LTS

In this Linux system administration tutorial you will learn how to remove a user from an Ubuntu Linux Server 16.04 LTS (Xenial Xerus) system with screenshots and instructions.

Description

System administrators create user accounts to allow access to the system or system services. In some situations is it necessary to remove a user account from a system. To remove a user, you will use the userdel command.

Syntax

The syntax for the userdel command is:

userdel -r User_Name

Prerequisites

The userdel command utilizes the following two parameters:

-r
Optional. The -r parameter removes the user's home directory and mail spool file if they exist.
User_Name
The user name of the account you would like to remove. This account must exist already.
IMPORTANT: The -r parameter will instruct userdel to remove the user's home directory and mail spool file. Please ensure these files are backed up if they are needed!

Prerequisites

To complete this tutorial you will require a running Ubuntu Linux Server 16.04 LTS system and an account with sudo administrative privileges. The sudo command is used to provide the superuser privileges required for the useradd command.

Remove a User

The following steps will guide you through removing an existing user from an Ubuntu Linux Server 16.04 LTS system.

IMPORTANT: Certain pre-installed user accounts are required for the system to operate properly. Do NOT remove a user from a system unless you are sure the account is not needed!
  1. To begin removing a user from your system, you will need to be logged in using a valid user account for your system. If you are unsure of how to do this, read our tutorial on Logging into Ubuntu Linux Server 16.04 LTS.

    In this tutorial, we have logged in as techonthenet on the host called ubuntu.

  2. We will remove the user called tsmith along with his home directory and mail spool file by entering the following command:

    sudo userdel -r tsmith

    The following screenshot demonstrates what you will see.

    Remove user

    When you have entered the command, press the Enter key to execute the command.

  3. The sudo command will now prompt you to enter the password for your administrator account.

    Enter sudo password

    Please note that no characters will show as you type your password. This is normal and is important to preserve the security of your password.

    After you have entered your password, press the Enter key to continue.

  4. If all goes well, you will see the system prompt appear again without any errors.

    The userdel command may report that the user's mail spool file was not found (similar to the screenshot below). This warning can be ignored and simply means that the userdel command could not remove the user's mail spool file since one did not exist.

    After user removed

  5. This step and step #6 are optional.

    In this step we will check to ensure that the user called tsmith was removed from the system by using the grep command to search the system /etc/passwd file for the tsmith user account. If the user tsmith is not found then we will have confirmed that the account has been successfully removed.

    Enter the following grep command after the system prompt to search the system password file:

    sudo grep tsmith /etc/passwd

    The following screenshot demonstrates what the command will look like after it is typed.

    Grep password file

    When you have typed the command, press the Enter key to execute the command.

  6. As seen in the screenshot below, the user tsmith was not found by the grep command confirming that we have removed the tsmith user account from the system.

    Confirm user removed

    Congratulations, you have successfully removed a user account from your Ubuntu Linux Server 16.04 LTS system!