Adding and removing user accounts is a daily task for a Linux system administrator. Ubuntu is one of the most popular distributions used in the Enterprise environment where the delete or removing a user account is a daily task too. Removing or deleting a user account in Ubuntu requires administrative privileges which are generally provided by the root user. A user account can be deleted in different ways and with different options in Ubuntu. In this tutorial, we examine how to delete a user account by deleting its home directory, removing permissions, etc.
List User Accounts
Before deleting or removing a Ubuntu user account we can list currently existing accounts. The users are stored in the /etc/passwd
where the first column is the user account name. We can list existing user accounts with the cat command like below.
$ cat /etc/passwd
Remove/Delete User Account with deluser Command
The official command to delete or remove the user account from the Ubuntu is the deluser
command. The user account name should be provided to the deluser command. Also, we require privileges to delete a user account which can be provided by logging in root user or by using the sudo
command as a regular user.
$ sudo deluser ismail
Remove/Delete User Account with deluser Removing User Home Directory
The deluser command does not delete the user account home directory by default. In order to delete the user account home directory, the --remove-home
option should be provided like below. In the following example while deleting the user “ismail” also its home directory “/home/ismail” will be deleted.
$ sudo deluser --remove-home ismail
Remove/Delete User with userdel Command
The userdel
command can be also used to remove existing user.
$ userdel ahmet
If the user is currently logged in or there is an error about the user removal the -f
force option can be used.
$ userdel -f ahmet
Remove/Delete User Account with GUI
The user management is provided via the Settings
tool in Ubuntu. First, we open the Settings tool which can be opened in different ways like clicking dash and then typing settings
which is listed below.
In the Settings navigate to the Users
left pane and click the Unlock
button in order to authenticate as sudo user to get the required privileges to delete the account.
Provide the current user password to get privileges.
In the following step we select the user we want to delete from the list of users. In this case, we select the user named ahmet
to delete it and click to the Remove User...
button in the right-left corner.
We asked if we want to delete the user’s files or if we want to keep them. This is the same operation as deleting the user’s home directory. In this example we Delete Files
for the user ahmet.
Verify User Account Has Been Deleted From Ubuntu
We can verify if the user has been deleted successfully or check if a specific user account exists in Ubuntu by using the grep command. Just provide the user name to the grep command to filter passwd
file like below. In the following example, we check if the user ahmet deleted successfully and if exists in the “/etc/passwd” file.
$ grep '^ahmet' /etc/passwd
Temporarily Disable User Account
Deleting a user in Ubuntu removes the account and its home folder completely where they can not be recovered in a regular way. But if we want to revert them back later but do prevent access for a specific time period we can disable the account. The usermod
command can be used to disable an account in Ubuntu.
$ sudo usermod -L -e 1 ahmet
Alternatively, we can specify the disable period where after the specified data the user account is enabled automatically. In the following example, we disable the account to the 2021-12-01. After 2021-12-01 the account is enabled automatically.
$ sudo usermod -e 2021-12-01 ahmet