The “userdel” Command in Linux [4 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Linux is a multi-user os that allows several users to interact with the system simultaneously. In case of deleting any unnecessary or unused user account, you can use the userdel command in Linux. In this article, I will explain the userdel command, including its syntax, options, and examples, so that you can become a more confident and capable Linux system user.

A. Description

The userdel command is a useful tool in Linux for deleting user accounts on a system. This command is an essential part of Linux system administration as it helps to manage user accounts and maintain the security of the system. Whether you need to remove an old or unused user account, or simply need to clean up your system by removing unnecessary accounts, the userdel command can help.

B. Syntax

The syntax of the userdel command in Linux is simple, as shown below.

userdel [OPTION]... USER_NAME

Note: In the syntax above, the OPTION enclosed by the square brackets means it is not mandatory and three dots after the square brackets mean multiple OPTIONS can be used simultaneously after the userdel command.

C. Options

The userdel command has multiple options available. Here, I have listed a few of them. However, you can learn more about the userdel command, its options and their uses by checking the man page.

man userdel

Useful Options

  • f, –force: Force removal of files, even if not owned by the user.
  • -h, –help: Displays the help message.
  • -r, –remove: Removes the home directory and all traces of the user from the system.
  • R, –root CHROOT_DIR: This option allows you to specify a directory to chroot into before performing the user deletion, which can be useful for separate partition or filesystem cases.
  • -Z, –selinux-user: This option removes any SELinux user mapping for the user. SELinux is a security module that provides fine-grained security policies for Linux systems.

Note: Commands and their options are case-sensitive in Linux. So, be careful while using them.

Practical Examples of the “userdel” Command in Linux

In Linux, the userdel command is a helpful tool to delete user accounts from the system. In the section below, I will show you some of the most useful applications for the userdel command in Linux.

Example 1: Delete a User With the “userdel” Command in Linux

You can remove any user account from your system by simply running the userdel command. In this example, I will delete a user named “james” from my system. To do so, follow the below procedures.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

 Type the following command in the command prompt to see the name of user accounts in your system.

grep james /etc/passwd

➌ Now, press the ENTER button.

 Type the following command in the command prompt to remove the user account “james”.

sudo userdel james

➎ Now, press the ENTER button.

➏ Input your Password.

➐ Run the following command.

grep james /etc/passwd

➑ Press ENTER key again.

Output >

The following image shows that I have removed the user account named “james”.I have removed user james using userdel command in Linux.


Similar Readings


Example 2: Delete a User Along With its Home Directory and Mail Spool

The userdel command allows you to delete a user account with its home directory and mail spool. For this, you have to use the -r option with the userdel command. In this section, I will remove the user “michael” with its home directory and other traces of user “michael”. You can do the same for any other users in your system by following the steps given below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

 Type the following command in the command prompt.

ls /home

➌ Now, press the ENTER button.

 Type the following command in the command prompt to remove the user account “michael”.

sudo userdel -r michael

➎ Now, press the ENTER button.

➏ Input your Password.

➐ Run the following command.

ls /home

➑ Press ENTER key again.

Output >

You can see in the image that I have removed the user “michael” along with the home directory and other traces of user.Image showing that, I have removed user michael along with its home directory.

Example 3: Remove SELinux User Mapping

You can remove SELinux user mapping from the user login. Here, I will remove SELinux user mapping for the user “emily.”  You can do the same by following the steps mentioned below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Now, type the following command in the command prompt

ls /home

➌ Now, press the ENTER button.

 Copy the following command

sudo userdel -Z emily

➎ Now, press the ENTER button.

➏ Input your Password.

➐ Press ENTER key again.

Output >

In the image below, you can see that no login mapping is defined for the user “emily”. If it was defined, then the login mapping would have been removed.You can see that no login mapping is defined for user “emily”. If it was defined, then the login mapping would have removed.

Example 4: Help Page for the “userdel” Command in Linux

In Linux, the help page for the userdel command contains a brief description. You can see the help page by following the steps mentioned below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

 Type any of the following commands into the command prompt:

userdel --help

Alternatively,

userdel -h

➌ Now, press the ENTER button.

Output >

In the following image, you can see the help page of the userdel command in Linux.The help page for userdel command in linux.

Conclusion

In this article, I’ve discussed the userdel command which is helpful to delete user accounts on a Linux system. At the same time, I’ve discussed its options and provided some relevant examples. Hopefully, this article will help you better understand the userdel command and its use, allowing you to safely and effectively remove user accounts as needed. Consequently, it will make your journey easier to become a power user of the Linux command line.


Similar Readings

Rate this post
S. M. Amdadul Islam

Hello everyone. I am S. M. Amdadul Islam, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Naval Architecture and Marine Engineering (NAME) graduate from the Bangladesh University of Engineering and Technology. In addition to my regular work, I enjoy watching Youtube, discovering new things, gossiping with friends, visiting new places, and taking photos of landscapes. Read Full Bio

Leave a Comment