How to Remove User on Ubuntu 22.04?


In Ubuntu or any other Linux system, user management is a key activity for a system administrator. The users are added or deleted to meet the specific business need. The removal of the user from Ubuntu is usually carried out when the user has left, or its role(s) has been changed. In such cases, if the user is not removed, it invokes potential security threats to the system or the intervening/clash of roles/authorizations between users also occurs.

Keeping this importance in view, this post will address the command line and GUI methods to remove the user on Ubuntu 22.04.

How to Remove a User on Ubuntu 22.04?

Method 1: How to Remove a User on Ubuntu 22.04 Using the userdel Command?

Method 2: How to Remove a User on Ubuntu 22.04 Using the deluser Command?

Method 3: How to Manually Remove a User on Ubuntu 22.04?

Method 4: How to Remove a User on Ubuntu 22.04 Using GUI?

How to Verify the Removal of a User on Ubuntu 22.04?

How to Disable(Lock/Set Expiry) a User on Ubuntu 22.04?

Bottom Line

How to Remove a User on Ubuntu 22.04?

Ubuntu 22.04 provides strong CLI and interactive GUI support. These both support the user management facility, including the removal. In this section, the command line methods are described first, one by one, followed by the GUI methods. Let’s start with the “userdel” command:

Method 1: How to Remove a User on Ubuntu 22.04 Using the userdel Command?

The “userdel” command line tool deletes the user and its associated files instantly. The options support of the command is very limited as the usage is quite to the point. Let’s check:

Syntax:

$ sudo userdel [Options] [UserName]

The [Options] support of “userdel” can be seen in the following image:

Example:

The command below removes the user “ubuntuuser” from the system:


The user named “ubuntuuser” has been removed from the system.

To verify, the “id <username>” command is used before and after the removal of the user.

Method 2: How to Remove a User on Ubuntu 22.04 Using the deluser Command?

The “deluser” is a symlink to the “userdel” command. However, it offers extended functionality through its supported functions. The syntax of the command is:

Syntax:

$ sudo deluser [Options] [UserName]

Let’s dig into the details of the “deluser” command using the “–help” flag:

The “deluser” utility removes a normal user from the system, a group, and removes a specific user from the group.

Example 1: Remove Only a User From Ubuntu

Let’s remove the user named “genie” from the system, using the deluser command without any options:


The output shows that the user has been removed but a prompt says that the group named “genie” has now no members. Moreover, the command does not remove the “home” directory of the user (in short it does not remove the files of the user which may cause conflict while going forward with the user management activities).

Example 2: Remove the User and the Home Directory

The “deluser” has the option/flag “–remove-home” that removes the user, the respective home directory, and the files. Let’s practice it on the user named “genieuser”:

$ sudo deluser –remove-home genieuser

Similarly, use the “–remove-all-files” to remove all the associated files of the user. Moreover, multiple options can also be used simultaneously to remove the user with the specified conditions as specified in the options(flags).

Example 3: Remove the User and Backup the Files

The “–backup” allows you to back up (in the current directory) the files alongside the removal as well. Let’s apply this to the user, named “lgenie”:

$ sudo deluser –backup lgenie

Note: To save the backed-up files in the desired directory, use the flag “–backup-to” instead of “-backup” and give the directory path as well.

Example 4: Remove a User From a Group

Apart from dealing with the user’s removal from the system, the deluser also allows you to remove the user from a specific group. The command below removes the user “linuxgenie” from the group named “lgenie”:

$ sudo deluser linuxgenie lgenie

The “linuxgenie” user has been removed from the group named “lgenie”.

Note: the “deluser” command removes the group as well using the command: “sudo deluser <groupname>”.

Difference Between userdel and deluser Commands

The “userdel” command offers the basic functionality of deleting the user and all associated system files. On the other hand, the “deluser” is an image (or mapping) of the “userdel” command. The “deluser” command supports options to perform specific functionality. The “deluser” can be used to remove the group as well.

It is recommended by the Debian/Ubuntu community to use the “deluser” command instead of using the “userdel”. However, we list down both the command line tools to remove the user from Ubuntu.

Method 3: How to Manually Remove a User on Ubuntu 22.04?

In Ubuntu, the user-related details are stored in the “/etc/passwd” file. Whenever a user is created/modified on the system, the relevant entry in the “/etc/passwd” is updated as well. The entries in the “/etc/passwd” file need to be managed to carry out the deletion process manually.

Example:

First, open the “/etc/passwd” file in an editable mode using the nano editor:


Let’s say the user named “ubuntu1” needs to be removed from the system. Use the “CTRL+W” key (inside nano editing mode) and search for “ubuntu1”.

Remove the entry and use the key CTRL+S to save the file. Verify the deletion via the “id username” command:


The user has been removed but not its home directory. To delete the home directory of the user too, use the command below:

$ sudo rm -rf /home/ubuntu1

That’s how you remove the user and its home directory manually.

Method 4: How to Remove a User on Ubuntu 22.04 Using GUI?

The above methods refer to the command line support to remove users on Ubuntu. Like any other GUI-supported OS, Ubuntu also has GUI support to remove the user. Let’s see how it works on Ubuntu.

Step 1: Open the Add/Remove User’s Section in Settings

Search for the Add/Remove user’s section and click on the following search result:

Click on the “Unlock…” button as highlighted in the following image:

Step 2: Choose the User and Remove

Navigate through the user’s list and choose which you want to remove. Click on the desired user and then on the “Remove User…” button, as shown below:

Now, choose between the following options, i.e., “Delete Files” and “Keep Files”:

Soon after that, the user is removed and is not present in the list, as can be seen below:

That’s how you verify the user’s deletion process in Ubuntu.

How to Verify the Removal of a User on Ubuntu 22.04?

Is the user removed or not from Ubuntu? This question arises in mind when you remove a user on Ubuntu. Well, the successful executions of the commands do ensure the deletion. However, the users have the command line and GUI options to verify the deletion as well. Let’s see how:

Using Terminal

The “/var/log/” directory contains the logs of the system and the user. Inside that directory, the “/var/log/auth.log” files store the user’s login-related records (logins and the authentication used to log in). This file can be accessed with various flags to verify the user’s deletion. For instance, the command below will check the logins of the user named “ubuntuuser”:

$ sudo grep ubuntuuser /var/log/auth.log

Scroll down the list to see the latest operations regarding the “ubuntuuser” keyword. It is observed that the removal operations have been performed.

Using GUI

To verify the user’s removal from Ubuntu using GUI, open the Users section inside the Ubuntu settings. The users available on the system can be seen on the right windowpane. Here, you need to check whether the user (you deleted) earlier is deleted or not.

How to Disable(Lock/Set Expiry) a User on Ubuntu 22.04?

Why remove the user every time? Let’s say a user will not be available for two months. Is it possible to delete the user temporarily or disable the user for a specific amount of period? Yes, it is possible by using the “usermod” command, and here’s how it works:

  • Specify the Expiry of the User

The “usermod” command with its “-e” option sets the expiry date of the user. The command below sets the expiry date “2023-11-27” of the user named “genieuser”:

$ sudo usermod -e 2023-11-27 genieuser

Note: If you want to remove this expiry, use the command “sudo usermod -e “” <username>”.

  • Lock the User

Another way of disabling the user is to lock the user’s password. For this, use the “-L” option with the username as follows:


The user password is locked now and will remain the same until it is unlocked.

Note: To unlock the user, log in as root user and use the “-U” option instead of “-L” in the above command.

Bottom Line

A user from Ubuntu is removed using two command line methods, i.e., deluser and userdel commands, or by accessing the “/etc/passwd” file. The “deluser” and the “userdel” remove the targeted user with some options (either remove it alongside the files, home directory, etc). While the”/etc/passwd” file is accessed using any terminal-based editor to trace and remove the user. Additionally, the GUI of Ubuntu can also be used to remove/delete the specific user.

All in all, this post has covered both the CLI and GUI methods to remove users on Ubuntu 22.04.

Print Friendly, PDF & Email
Categories