How to use the command 'deluser' (with examples)
- Linux
- December 25, 2023
The ‘deluser’ command is used to delete a user from the system. It provides several options to remove a user and their associated files. This article will illustrate each of these use cases with examples.
Use case 1: Remove a user
Code:
sudo deluser username
Motivation: This use case is useful when you want to remove a user from the system without deleting their home directory and files.
Explanation:
sudo
: This command is executed with administrative privileges.deluser
: The name of the command to delete a user.username
: The name of the user to be removed.
Example output:
Removing user 'username' ...
Warning: group 'username' has no more members.
Done.
Use case 2: Remove a user and their home directory
Code:
sudo deluser --remove-home username
Motivation: Sometimes, you may want to completely remove a user from the system, including their home directory and files.
Explanation:
sudo
: This command is executed with administrative privileges.deluser
: The name of the command to delete a user.--remove-home
: This option removes the user’s home directory.username
: The name of the user to be removed.
Example output:
Removing user 'username' ...
Warning: group 'username' has no more members.
Done.
Use case 3: Remove a user and their home, but backup their files
Code:
sudo deluser --backup-to path/to/backup_directory --remove-home username
Motivation: In some cases, you may want to remove a user and their home directory, but still retain a backup of their files for future reference.
Explanation:
sudo
: This command is executed with administrative privileges.deluser
: The name of the command to delete a user.--backup-to path/to/backup_directory
: This option specifies the directory where the user’s files will be backed up as a.tar.gz
file.--remove-home
: This option removes the user’s home directory.username
: The name of the user to be removed.
Example output:
Backing up files to 'path/to/backup_directory/username.tar.gz' ...
Removing user 'username' ...
Warning: group 'username' has no more members.
Done.
Use case 4: Remove a user and all files owned by them
Code:
sudo deluser --remove-all-files username
Motivation: When you want to completely remove a user from the system, including their home directory and any files they own, this use case comes in handy.
Explanation:
sudo
: This command is executed with administrative privileges.deluser
: The name of the command to delete a user.--remove-all-files
: This option removes all files owned by the user.username
: The name of the user to be removed.
Example output:
Removing user 'username' ...
Warning: group 'username' has no more members.
Done.
Conclusion:
The ‘deluser’ command provides a flexible way to remove users from the system. By using different options, you can decide whether to remove the user’s home directory, backup their files, or delete all files owned by the user. Make sure to use these options carefully to avoid any unintended consequences.