In my previous article, I had explained how to create users in Linux. If you have not read that article, I would suggest you to read that guide.
Today you will learn how to delete a user account in Linux using the userdel command.
As a Linux administrator user and group management is one of the tasks that a Linux administrator has to do every day.
If you are working or have done in an organization, you would know that upon the resignation of an employee, it is the responsibility of the system administrator to delete that user account from the server.
In this article, I will explain the most useful features of this command. So let’s get to the topic.
Table of Contents
Syntax
You must follow the syntax given below to use the userdel
command.
userdel [options] LOGIN
1. How to Delete a user account
To delete a user account in Linux you can run the userdel command.
Here in this example, I am deleting a user named shrikant.
# userdel shrikant
Note: You have to run the userdel command as a root user because upon running this command, it makes changes to the following important configuration files whose owner is root.
- /etc/passwd
- /etc/shadow
- /etc/gshadow
- /etc/group
2. Delete a user account along with the home directory
In Linux operating systems, when we delete a user, the user’s home directory and the contents inside them are not deleted by default.
But users can be deleted with their home directory and mail spool. To do this you can use the -r
option with the userdel
command.
in this example, I am deleting a user named akash.
# userdel -r akash
Note: But if the files are in another file system, you have to manually search for them and delete them.
3. How to delete a user account forcefully
If a user is currently logged in, can they be removed?
The answer is yes.
Using the option that I am going to tell you right now, you can delete any logged in user account and the following things will also be deleted along with the user account.
- User’s home directory
- Mail spool
- If
USERGROUPS_ENAB
is defined toyes
in/etc/login.defs
and if a group exists with the same name as the deleted user, then this group will be removed, even if it is still the primary group of another user.
Caution: This option is not recommended.
Type the following command to forcefully delete a user account.
# userdel -f sagar
4. Help/Manual page access
Use the following commands to access the Manual Page/Help Page of userdel command.
# userdel --help
# man userdel
You can visit at following websites to get more information on userdel
.
Conclusion
I hope you have learned something from this article.
I have tried my best to include all the features of userdel command in this guide.
Now I’d like to hear your thoughts.
Was this guide useful to you?
Or maybe you have some queries.
Have I not included any command in this guide?
Leave a comment below.
Here are a few other hand-picked guides for you to read next: