Home  >  Article  >  Operation and Maintenance  >  How to use the userdel command to delete a user in Linux? (code example)

How to use the userdel command to delete a user in Linux? (code example)

青灯夜游
青灯夜游Original
2019-03-22 15:46:325097browse

Linux is a multi-user system, which means that more than one person can interact with the same system at the same time. So what do you do if you need to delete a user's account? The following article will show you how to use the userdel command to delete users in Linux. I hope it will be helpful to you.

How to use the userdel command to delete a user in Linux? (code example)

Linux userdel command

In Linux, administrators can use the userdel command to delete user accounts and all its associated files.

Let’s take a look at the userdel command syntax:

userdel [OPTIONS] USERNAME

To use the userdel command to delete a user, the user needs to be logged in as root or as a user with sudo access.

How to delete a user in Linux?

To use the userdel command to delete the user account named "username", you need to run the following command:

userdel username

When the userdel command is called, /etc is read The contents of the /login.defs file. Properties defined in this file override the default behavior of the userdel command. If "USERGROUPS_ENAB" in this file is set to "yes", userdel will only delete groups with the same name as the user if other users are not members of this group.

Description: The userdel command will delete user entries from the /etc/passwd and /etc/shadow files.

In most Linux distributions, when you use the userdel command to delete a user account, the user's home directory and mail spool directory will not be deleted.

Use the -r (--remove) option to force userdel to delete the user's home directory and mail spool directory:

userdel -r username

Note: The above command will not delete it User files located in other file systems.

If the user to be deleted is still logged in, or if there are running processes belonging to that user, the userdel command will not allow the user to be deleted.

In this case, it is recommended to use the killall command to log out the user and terminate all running processes of the user:

sudo killall -u username

Once completed, you will be able to delete the user.

Another option is to use the -f (--force) flag to force deletion of a user account even if the user is still logged in or there are running processes belonging to that user.

userdel -f username

Recommended related video tutorials: "Linux Tutorial"

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to use the userdel command to delete a user in Linux? (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn