others-Difference between userdel and deluser in Linux System

1. Purpose

In this post, I would show the difference between the userdel and deluser commands in Linux Operating Systems.

2. Environment

  • Ubuntu System

3. What is the difference between the userdel and deluser in Linux?

3.1 What is userdel?

The Linux userdel command can be used to delete user accounts. This command can only be used by root users. userdel can delete user accounts and related files. If no parameters are added, only the user account will be deleted, and the related files will not be deleted.

The grammer of the this command is:

userdel [-r] [user_account]

The -r option means to delete the user’s home directory while deleting the user.

We know that user information is stored in below files:

  • Basic user information: stored in the /etc/passwd file
  • User password information: stored in the /etc/shadow file
  • Basic user group information: stored in the /etc/group file
  • User group information: stored in the /etc/gshadow file
  • User personal files: The home directory is located at /home/username by default, and the mailbox is located at /var/spool/mail/username

In fact, the function of the userdel command is to delete the data information related to the specified user from the above files.

3.2 What is deluser?

The deluser command can be used to delete users. To delete a user from a group (ie exit the user group), everyone recommends usermod, but deluser is the more preferable manner. deluser has richer options and more powerful functions than userdel.

Delete a normal user:

[root@linux ~]# deluser mike

There are more options to specify when deleting a user:

  • --remove-home delete the user’s home directory and mailbox
  • --remove-all-files delete all files owned by the user
  • --backup Back up files before deleting.
  • --backup-to<DIR> The destination directory of the backup. The default is the current directory.
  • --system Delete only when the user is a system user.

Delete user from group:

[root@linux ~]# deluser mike admingroup

Delete all files of user:

[root@linux ~]# deluser --remove-all-files mike

3.3 The difference

  • userdel is a binary file in the system, but deluser is a script file in the system
  • The deluser is more low-level then userdel, it provides us more complicated and comprehensive options to delete users and groups
  • The userdel is more high-level command in the system, it provide us with a more intuitive and feasible command to delete users.

4. Summary

In this post, I tried to explain the difference between userdel and deluser in Linux system.