Userdel Command in Linux

FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!

In this article, you will learn all there is to the userdel command in Linux-based operating systems. We will go through what userdel is, why it is used, and the syntax and options used with the userdel command. In the end, we will also look at some beautiful examples of the userdel command in the terminal of Ubuntu 20.04. So pay attention, grab a snack if you want to, and read right till the end!

What is Linux userdel command?

Userdel is the abbreviation for “User Delete”, It is a command-line-based utility in Linux-based operating systems that helps in removing or deleting users from the system. Userdel basically modifies the system account files, deleting all the entries which refer to the username LOGIN.

We all know that Linux is a multi-user architecture, meaning more than one person can interact with the same system at the same time. It is the system administrator’s job to wisely manage these users, tasks like creating users, removing users, creating groups, removing groups, and assigning users to different groups come under user management. One such user management command is useradd.

When you are in the need of deleting a user may be the user moved away from the organization, or it was created for a specific service that no longer runs on the system, you can use the userdel command. It is mostly advised for system administrators to use the deluser command which is slightly more user-friendly.

Syntax of userdel command in Linux

The syntax of the userdel command is pretty easy:

usedel <options> <username>

Let us take a closer look at the fields present in the syntax of the userdel command:

1. <options>

This field takes in a range of options that specify how the userdel command should work and print the output. We will look at all of these options in the next section.

2. <username>

This field takes in the name of the user’s account on the computer of which you want to delete.

Options used with the userdel command

In typical Linux fashion, userdel also has a long list of options that we can use to perform various tasks, let us look at some of them.

1. -f

This option forces the removal of the user’s account. It removes the account, even if the user is still active and logged in! You can also write this option as “–force”

2. -r

This option removes the files in the user’s home directory, along with the home directory itself and the user’s mail spool. You can also write this option as “–remove”

3. -R <chroot_dir>

This option applies the changes in the chroot directory you specified and uses the configuration files from the same directory. You can also write this option as “–root”

4. -Z

This option removes any SELinux (Security-Enhanced Linux) user mapping for the user’s login. You can also write this option as “–selinux-user”

5. -P

This option prefixes the directories which are located in the /etc/* files. You can also write this option as “–prefix”

6. -h

This option prints out the help menu as shown below. It contains information about the usage and information about the command and options of the userdel command. You can also write this option as “–help”

help menu of the userdel command

Configuration variables of the userdel command

The userdel has a configuration file /etc/login.defs. In this file there are a couple of configuration files that change the behavior of the command, let us look into each of the variables and what it does.

1. MAIL_DIR

This string-type configuration variable manipulates the mailbox when its corresponding user account is modified or deleted.

2. MAIL_FILE

This string-type configuration variable defines the location of the user’s mail spool files relatively to their home directory.

3. MAX_MEMBERS_PER_GROUP

This number-type configuration variable specifies the maximum number of members per group entry. If and when the maximum number is reached, a new group entry is started in /etc/group

4. USERDEL_CMD

This string-type configuration variable runs when removing a user when it is defined.

5. USERGROUPS_ENAB

If this boolean-type configuration variable is set to “yes”, the userdel command removes the user’s group if it contains no more members, and useradd creates by default a group with the name of the user.

Files used by the userdel command

The userdel command takes in some data from a specific file in the system, let us look at what these files are and what exactly does the userdel command read from them.

1. /etc/group

The userdel command takes group account information from this file.

2. /etc/login.defs

The userdel command takes the shadow password suite configuration from this file.

3. /etc/passwd

The userdel command takes user account information from this file.

4. /etc/shadow

The userdel command takes secure user account information (like the password) from this file.

Exit status

Upon executing a userdel command, it will exit with one of the following exit codes, let us look at what they are and what they mean.

0 – Success
1 – Failed to update the passwd file.
2 – Invalid syntax
6 – The user you specified does not exist
8 – Could not delete the user as the user you specified is currently logged in.
10 – Failed to update group file
12 – Could not remove the home directory.

Now that we have covered the fundamentals of the userdel command, let us look at some practical examples of it to understand how the options and the command itself work.

Force removing a user

If you want to force remove a user, meaning remove a user even if he or she is logged in and active, you can do so by either using the option “-f” or “–force” as shown below

sudo userdel -f <username>

force removing a user

Removing the home directory when removing the user account

We can remove the home directory of a user and the files present in it at the same time as removing the account of the user by using the option “-r”. However, you will have to search and manually delete all the other files located in other file systems. Use the syntax shown below:

sudo userdel -r <username>

removing the home directory when removing the user account

Changing chroot configuration directory

You can change the configuration file of chroot by using the option “-R” a shown:

sudo userdel -R <username>

changing chroot configuration directory

Removing SE Linux mapping

If you want to remove Security Linux for the user’s login, simply use the option “-Z” as shown

sudo userdel -Z <username>

Summary

As you have seen, the userdel command is a simple command that lets you remove users from your system. You have now learned what userdel is, why it is used, the syntax, options, configuration variables, exit codes, and the file used by the userdel command. We have also looked at a couple of examples to get a basic idea of how the userdel command works.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *