How to Remove User on Ubuntu from Terminal

Written by: Bobbin Zachariah   |   Last updated: February 18, 2023

Ubuntu is one of the most stable and suitable Linux Distribution for everyone. Multiple users can work simultaneously on Ubuntu, and you might want to remove a user from Ubuntu, whether you are using Ubuntu as your desktop operating system or Ubuntu as a server on the cloud.

In this tutorial, we learn how to delete user accounts on Ubuntu from the command line.

Understanding User Accounts in Ubuntu

Generally, user accounts are created to identify the individual user and then grant control access to the system. Additionally, it helps keep their own files secure and custom settings.

Commonly users are created with a password and home directory. The recommended command is adduser. More privileged users are added to sudo group to get admin access. Based on the purpose some users can be added to groups.

Same way, you might be required to remove a user when the user leaves your organization, and that user account is no longer needed, or when an unauthorized person accesses the user account. . Debian and Ubuntu users commonly prefer to use deluser command to remove user accounts. It is a user-friendly wrapper of userdel command written in Perl.

Removing a User Account Using Command Line

For removing a user account you should require root user or a non-root user with sudo privileges. Let's check the steps to remove user accounts from the command line.

Note: Before running the command, make sure that you have backed up any important files that belong to the user account. If you want to keep the user's home directory or other files, you should back them up before deleting the user account.

Step1. Access the terminal

Once you log in as a root user or user with sudo privileges, press the Windows key and search for Terminal or you can open the Terminal by pressing the Ctrl, Alt, and T keys together.

Remember, an easy shortcut to access the terminal on Ubuntu is: Ctrl + Alt + T

Step 2. Identify the User Account

Before deleting a user from the system, let’s check the list of all the users available on Ubuntu.

All the users are listed in /etc/passwd file by default. The first column of /etc/passwd file contains a list of user accounts on your system. You can verify the uses account list by running the following command into the terminal:

cat /etc/passwd
identify the user

Here, from the list look at the first column to identify the username (login name).

Step 3. Remove the user using the Terminal

Type the following deluser command into the terminal, replacing username with the name of the user you want to remove:

sudo deluser username

This removes the user but the user's home directory and files still exist. The associated group will not be removed if any users are present in that group.

Example:

To delete user named bob, type

sudo deluser bob
delete a user using deluser

The user account bob is deleted. Now bob home directory remains for the deleted user. To remove the home directory for the previous user, you have to manually remove the directory of the user from the home directory location

To remove user including the home directory and mail spool use --remove-home option. Example

sudo deluser --remove-home tom
delete user and home directory using deluser command

The --remove-all-files option allows to remove of all files owned by that user from the system. This option should be used with caution as it could end up deleting all files mounted (such as NFS) by the user.

sudo deluser --remove-all-files john

deluser command options

Useful options of deluser command:

  • --force: This parameter is used to remove the root account. It is worth noting that you should not remove the root account from your system.
  • --remove-home: It will remove the user’s home directory and mail spool file.
  • --remove-all-files: It will remove all the files the user owns.
  • --backup: This parameter will create a backup copy of all the user's files and the mail spool file to the user’s home directory with the name $user.tar.bz2 or /$user.tar.gz
  • --backup-to DIR: Set the custom backup directory path. 

The/etc/deluser.conf file mentions all the default parameters. By default, all the parameters are disabled, and you can change the default setting by changing the value from 0 to 1.

Step 4. Verify

There are a few files you can check to confirm whether the user deleted on Ubuntu. Run any of the following commands:

id bob
grep -w 'bob' /etc/passwd
sudo grep -a 'bob' /var/log/auth.log | tail
confirm the user successfully deleted

Conclusion

In this tutorial, we learned how to remove user on Ubuntu from the command line.

Thanks for reading, please leave your feedback and suggestions in the comment section.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment