BASH

Linux Remove User Example

Dealing with users is a task that every systems administrator has to deal with frequently. This example will show you how to delete users in Linux, looking at the two different available commands: The default utility, userdel; and a script that acts as more friendly front-end for the default utility, which is named deluser.

For this example, Linux Mint 18 has been used.
 
 
 
 
 

1. How users are organized

The existing users of the system are registered in the file /etc/passwd. This file defines who has legit access to the system. This is an example of a line of the file:

julen:x:1000:1000:Julen,,,:/home/julen:/bin/zsh

Which follows the following format:

username:password:uid:gid:real_name:home_directory:command_shell
  • The username is the account name for the login.
  • The password field is actually not used in modern systems. The users credentials are stored in /etc/shadow file.
  • The uid (user id) and gid (group id) are the unique identifiers of the user and the group it belongs to, respectively.
  • The real_name is that, the user’s real name.
  • The home_directory is the working directory of each user, usually /home/<username>.
  • Finally, the command_shell is the program that is ran at login. Usually, this is the path to a shell. If not set, /bin/sh is used.

It’s better not to touch manually this file to remove (or add/modify) users. To delete users, we should use the methods that we will see in this tutorial.

2. Using native binary: userdel

userdel is the native, low level, binary of Linux systems. Its use is very simple:

sudo userdel [options] <username> # superuser privileges are needed.

So, deleting a user is as simple as it is shown below:

sudo userdel john_doe

And the user will be deleted. We can confirm it checking the /etc/passwd file:

grep "john_doe" /etc/passwd

Which won’t return any result.

Note: if we try to delete a non existing user, the binary will throw an error message:

userdel: user ‘john_doe’ does not exist

2.1. Deleting the home directory

By default, userdel does not remove the deleted user’s home directory. We can check it listing the directories in /home.

For deleting the home directory along with its owner, we have to use the -r (--remove) option:

sudo userdel -r <username>

So, for deleting john_doe with its home directory, would be:

sudo userdel -r john_doe

And the directory will disappear from /home.

Note: this option also removes the mail spool directory (/var/mail/<username>). If this directory, or the home one, are not found, the command will show a warning.

3. Using a user-friendly wrapper for userdel: deluser

We have seen how to use userdel, the native binary, which is actually not difficult to use. But the problem is that it doesn’t offer many options, and it also does not show much information about what’s being done.

To make the user deletion more comfortable, a Perl script named deluser was created, which is actually just an interactive wrapper for the native binary.

We can try to remove our john_doe user with deluser:

sudo deluser john_doe

(Of course, with deluser we also need superuser permissions).

And the following will be shown:

Removing user `john_doe’ …
Warning: group `john_doe’ has no more members.
Done.

3.1. Deleting the home directory

By default, this command neither removes the home directory. In this case, we have to pass the --remove-home option:

sudo deluser --remove-home john_doe

3.2. Deleting all the files owned by the user

We have seen how to remove the home directory, but this command also provides the chance for deleting every file in the disk owned by the user. For this, we have to use the --remove-all-files option:

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

Of course, we could combine it with  the home directory deletion:

sudo deluser --remove-home --remove-all-files john_doe

Note: take into account that looking for every file in the disk may take a long time.

3.3. Creating a backup of the files

It may be interesting to create a backup of the files that are going to be remove in the same time we delete those files belonging to the user. This can be achieved with the --backup option:

sudo deluser --remove-home --backup <username>

So, if we execute the following command:

sudo deluser --remove-home --remove-all-files --backup john_doe

A file named john_doe.tar.bz2 will be created, containing all the files that have been removed, in the directory from where we have executed the command.

Note: if we want to specify the folder where the backup should be created, we have to use the —backup-to option:

sudo deluser --backup-to <directory> <username>

For example, for saving the backup in /tmp directory:

sudo deluser --remove-home --remove-all-files --backup-to /tmp john_doe

Take into account that we must specify an existing directory, and without appending a file name, otherwise the command will throw an error and the user won’t be deleted.

4. Summary

In this example we have examined how to delete users in Linux systems, with two different commands: userdel and deluser. As we have seen, deluser can be considered a better option, since it shows more information, and also provides the option of creating a backup of the files of the user that are going to be removed.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button