Terminus
Remover Users in Linux

Remover Users in Linux

The short answer

In Linux, to remove a user account from the system, you can use the [.inline-code]userdel[.inline-code] command as follows:

$ sudo userdel <username>

Where:

  • [.inline-code]<username>[.inline-code] is the name of the user account you want to delete.

By default, this command will remove the user from the [.inline-code]/etc/passwd[.inline-code] file, the [.inline-code]/etc/shadow[.inline-code] file, the [.inline-code]/etc/group[.inline-code] file, and from any additional groups.

For example, this command will remove the user named [.inline-code]alex[.inline-code]:

$ sudo userdel alex

[#remove-users-home-directory] Removing users and their home directory [#remove-users-home-directory]

To remove a user alongside their home directory, including all the files and subdirectories it contains, you can use the [.inline-code]userdel[.inline-code] command with the [.inline-code]-r[.inline-code] flag as follows:

$ sudo userdel -r <username>

Note that this command will also remove the specified user's mail spool.

Alternatively, you can also use the [.inline-code]deluser[.inline-code] command with the [.inline-code]--remove-all-files[.inline-code] flag as follows to achieve the same result:

$ sudo deluser --remove-home <username>

For example, both of these commands will remove the user named [.inline-code]alex[.inline-code] including their home directory:

$ sudo userdel -r alex
userdel: john mail spool (/var/mail/john) not found
$ sudo deluser --remove-home alex
Looking for files to backup/remove ...
Removing files ...
Removing user `alex' ...
Warning: group `alex' has no more members.
Done.

[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code]remove user with home directory[.inline-code] in the AI Command Suggestions will prompt a [.inline-code]deluser[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#remove-users-files] Removing all the files owned by a user [#remove-users-files]

To remove all the files and directories owned by a specified user on the system, outside of their home directory, you can use the as follows:

$ sudo deluser --remove-all-files <username>

For example, this command will remove all the files belonging to a user named [.inline-code]alex[.inline-code]:

$ sudo deluser --remove-all-files alex

[#remove-users-secondary-groups] Removing a user from a secondary group [#remove-users-secondary-groups]

In addition to their primary groups, users may often be part of other groups referred to as secondary groups.

To remove a user from a secondary group, you can use the [.inline-code]deluser[.inline-code] command as follows:

$ sudo deluser <username> <group>

Where:

  • [.inline-code]<username>[.inline-code] is the name of the user.
  • [.inline-code]<group>[.inline-code] is the name of the secondary group.

For example, this command will remove the user named [.inline-code]alex[.inline-code] from the secondary group named [.inline-code]sudo[.inline-code]:

$ sudo deluser alex sudo
Removing user `alex' from group `sudo' ...
Done.

Note that, when executed on the primary group of the user, the [.inline-code]userdel[.inline-code] command will output the following error:

/usr/sbin/deluser: You may not remove the user from their primary group.

You can learn more about users and groups by reading our other article on how to create and configure new users in Linux.

[#remove-users-passwords] Removing a user’s password [#remove-users-passwords]

To disable the password of a user account, you can remove it using the [.inline-code]passwd[.inline-code] command with the [.inline-code]-d[.inline-code] flag (short for [.inline-code]--delete[.inline-code]) as follows:

$ sudo passwd -d <username>

This will ultimately allow any other user on the system to log in as that user without using a password.

For example, this command will remove the password of the user named [.inline-code]alex[.inline-code]:

$ sudo passwd -d alex
passwd: password expiry information changed.

You can learn more about users and passwords by reading our other article on how to change the password of a user in Linux.

[#remove-remote-users-accounts] Removing a user from a remote server [#remove-remote-users-accounts]

To remove a user account from a remote server, you need to first log into the server using the [.inline-code]ssh[.inline-code] command as follows:

bash 
$ ssh <username>@<server_address>

Where:

  • [.inline-code]<username>[.inline-code] is the name of the user you want to log in as on the remote server.
  • [.inline-code]<server_address>[.inline-code] is the hostname or IP address of the remote server.

Once logged in, you can use the [.inline-code]userdel[.inline-code] command to delete the specified user as shown in the previous sections.