userdel Command in Linux with Examples

Posted in

userdel Command in Linux with Examples

Sangeeta Gulia
Last updated on October 19, 2022

    Linux is a multi-user operating system in which we can create and remove users. In this article, we are going to discuss userdel command in Linux, which is used to remove a user account and its associated records. We will also discuss its options and examples.

    What is userdel command?

    In a Linux system, the userdel command is used to remove a user account and its associated files. Basically, this command alters the system account files by erasing any entries that relate to the given user. It's a low-level command for deleting users and their records.

    Options with the command

    userdel username

    This is used to simply delete the user and its records with a given username. When a user account is deleted using userdel on most Linux operating systems, the user home, and mail spool folders are not deleted.

    userdel -f

    This option causes the given user account to be removed. It makes no difference if the user is still logged in. It also causes userdel to delete the user's home directory and mail spool, even if the home directory is shared by another user or the mail spool is not owned by the selected user.

    userdel -r

    When we use this option to delete a user, the files in the user's home directory, as well as the home directory itself and the user's mail spool, are deleted. All files in other file systems will have to be individually looked for and removed.

    userdel -R

    Changes should be made in the CHROOT_DIR chroot directory, and configuration files should be used from the same location.

    userdel -Z

    For the user's login, removes any Security-Enhanced Linux user mapping.

    -h userdel

    This option displays a help message and then exits the programme.

    Exit status of userdel

    userdel exits with one of the following exit codes, depending on what occurred:

    0

    When the command is executed successfully success

    1

    When the command cannot update the passwd file.

    2

    When the syntax is wrong.

    6

    When the given user doesn't exist.

    8

    When the command cannot delete the user because the given user is currently logged in.

    10

    When the command cannot update the group file.

    12

    When the command cannot remove the home directory.

    Conclusion

    In this article, we discussed userdel command and its options which perform several functions, including deleting a user and the files in the user's home directory and mail spool, removing any Security-Enhanced Linux user mapping for the user's login, displaying help message, etc.

    People are also reading:

    Tags:
    command linux

    Leave a Comment on this Post

    0 Comments