How to use the command userdel (with examples)

How to use the command userdel (with examples)

The userdel command is a Linux command used to remove a user account or remove a user from a group. This command is useful when managing user accounts on a Linux system. It provides options to remove a user, remove a user in a different root directory, or remove a user along with the home directory and mail spool.

Use case 1: Remove a user

Code:

sudo userdel username

Motivation: The userdel command allows us to remove a user account from the system. This is useful when we no longer want a specific user to have access to the system.

Explanation:

  • sudo: This command is used to execute the userdel command with root privileges.
  • userdel: The userdel command itself.
  • username: The username of the user account we want to remove.

Example output:

$ sudo userdel john
userdel: user 'john' does not exist

Use case 2: Remove a user in other root directory

Code:

sudo userdel --root path/to/other/root username

Motivation: Sometimes, a system may have multiple root directories. In such cases, this option allows us to remove a user from a specific root directory instead of the default root.

Explanation:

  • sudo: This command is used to execute the userdel command with root privileges.
  • userdel: The userdel command itself.
  • --root: This option specifies the root directory where the user account is to be removed from.
  • path/to/other/root: The path to the root directory where the user account is to be removed from.
  • username: The username of the user account we want to remove.

Example output:

$ sudo userdel --root /var/custom-root john

Use case 3: Remove a user along with the home directory and mail spool

Code:

sudo userdel --remove username

Motivation: When removing a user account, it might be necessary to also delete the user’s home directory and mail spool. This option allows us to remove the user account along with its associated files.

Explanation:

  • sudo: This command is used to execute the userdel command with root privileges.
  • userdel: The userdel command itself.
  • --remove: This option tells the userdel command to also remove the user’s home directory and mail spool.
  • username: The username of the user account we want to remove.

Example output:

$ sudo userdel --remove john

Conclusion:

The userdel command is a powerful tool for managing user accounts on a Linux system. It provides different options to remove a user account, remove a user from a specific root directory, or remove a user along with its associated files. By using the userdel command, system administrators can easily manage user accounts and maintain the security and integrity of their Linux systems.

Related Posts

Getting Information about STLink and STM32 Devices (with examples)

Getting Information about STLink and STM32 Devices (with examples)

1: Display amount of program memory available $ st-info --flash Motivation: This command allows you to quickly retrieve information about the program memory available on a connected STM32 device.

Read More
Using the getcap command (with examples)

Using the getcap command (with examples)

The getcap command is used to display the name and capabilities of specified files.

Read More
Using Flips Command (with examples)

Using Flips Command (with examples)

Introduction Flips is a command-line tool that allows you to create and apply patches for IPS and BPS files.

Read More