Linux deluser command


The deluser command in Linux is used to delete user accounts from the system. Here’s a guide to using deluser with expected output for common options.

1. Basic Syntax of deluser

The command format for deluser is:

sudo deluser <username>

This command removes the specified user from the system but keeps the user’s home directory and files by default.

2. Examples of deluser with Output

Example 1: Deleting a User

Command:

sudo deluser alice

Expected Output:

Removing user 'alice' ... Removing user 'alice' from group 'users' ... Done.

In this example, deluser removes the user alice from the system but leaves the home directory intact. It also removes the user from any primary groups they belong to.

Example 2: Deleting a User with the Home Directory

Command:

sudo deluser --remove-home alice

Expected Output:

Removing user 'alice' ... Removing user 'alice' from group 'users' ... Deleting home directory '/home/alice' ... Done.

Here, the --remove-home option not only removes the user alice but also deletes their home directory, /home/alice, along with any files within it.

Example 3: Deleting a User and All Files Owned by Them

Command:

sudo deluser --remove-all-files alice

Expected Output:

Removing user 'alice' ... Removing user 'alice' from group 'users' ... Deleting all files owned by 'alice' ... Done.

This command deletes the user alice, removes them from their groups, and deletes all files owned by alice across the system.

Example 4: Removing a User from a Specific Group

Command:

sudo deluser alice developers

Expected Output:

Removing user 'alice' from group 'developers' ... Done.

In this case, deluser does not delete alice from the system but simply removes them from the developers group.

3. Explanation of Output Messages

  • Removing user 'alice': Indicates that the user alice is being removed from the system.
  • Removing user from group: Lists groups the user is being removed from.
  • Deleting home directory: If --remove-home is used, it shows the home directory is being deleted.
  • Deleting all files owned by user: When --remove-all-files is specified, it deletes all files owned by the user, not just in their home directory.
  • Done: Confirms the operation has completed successfully.

4. Other Options for deluser

  • Backup before deletion:
    sudo deluser --backup alice
    This command creates a .tar.gz backup of the user’s home directory before deletion. The output might show:
    Backing up files to '/var/backups/alice.tar.gz' ...

These examples cover common scenarios and illustrate how deluser provides feedback for each action it performs, helping administrators verify that users and their data are managed as expected.