How to use userdel command in linux?

Use

– userdel command in linux is used to delete an existing user and related files. Only the super user can use this command.

Syntax

– userdel [options] LOGIN

The userdel command modifies the system account files, deleting all entries that refer to the user name LOGIN. The named user must exist.

Example

– First, let’s have a look at the /etc/passwd file to see the list of exiting users

list of users in passwd file
Before userdel

The output shows 5 users. Let us delete the user David

userdel command in linux
userdel command in linux

After using the userdel command the user David no more exists in the system.

Drawback

Deleting the user as shown above only removes the user entry. Therefore, the home directory and the files in the home directory still exist as such in the system.

Drawback of userdel command in linux without -r

In order to delete the files within the home directory and the home directory itself use –r option with userdel.

using -r option with userdel in linux
-r option with userdel

This time user Steve is deleted using –r option. It can be seen that the home directory of Steve is also deleted from the system.

Other options with userdel command in linux

-f : forces the removal of the user even if the user’s logged in. Sometimes as an administrator, you want to delete a user account but are not able to do so because of the user current status. For example:

Error deleting user account

In such a situation -f option can be used with userdel command to delete the user Steve.

Advice: Do not use this option as it can lead the system in an inconsistent state.

Sample Questions using userdel command in linux

Q1. Can you delete two user U1 and U2 simultaneously using userdel command as below?

#userdel U1 U2

Q2. As an admin you want to delete the account of user Smith and also all the associated files and directories. How can you do it?

PPT on userdel command

Video on userdel command

Other Related Commands

useradd
usermod
groupadd
groupmod
groupdel
passwd

Leave a Reply

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