[Linux] Deleting Users and Removing Memberships with the deluser Command

目次

Overview

In Debian-based distributions like Ubuntu and Debian, the deluser command is the primary tool for removing user accounts or excluding users from specific groups. As a high-level wrapper for the userdel command, it offers safer and more flexible features, such as the ability to automatically back up home directories before deletion. This allows system administrators to perform account cleanup tasks efficiently without manual, multi-step procedures.

Specifications (Arguments and Options)

Syntax

BASH

# Remove a user account from the system
deluser [options] [username]

# Remove a user from a specific group (membership removal)
deluser [options] [username] [groupname]

Main Options

OptionDescription
–remove-homeDeletes the user’s home directory and mail spool during removal.
–remove-all-filesSearches for and deletes every file owned by the user across the entire system.
–backupCreates a compressed backup (tar.gz) of the home directory before deletion.
–backup-to [DIR]Specifies the target directory for the backup file (defaults to the current path).
–groupDeletes the group itself rather than a user.
–conf [FILE]Uses a custom configuration file instead of the default deluser.conf.
–systemAllows the deletion of system users (those with low UID values).

Basic Usage

Executing the command without additional options for a target account, such as former_web_admin, will remove the account information while leaving the home directory and data files untouched. This is a safe default for situations where data retention is required.

BASH

# Scenario: Cleaning up an account for a former_web_admin
# Verifying status before removal
id former_web_admin
ls -d /home/former_web_admin

# Executing user removal (files remain)
sudo deluser former_web_admin

Example Output

Removing user `former_web_admin' ...
Warning: group `former_web_admin' has no more members.
Done.

Note: The directory /home/former_web_admin persists even after the command finishes.

Practical Command Scenarios

Removing the Home Directory Completely

When an account for an expired_contractor needs to be fully erased along with all associated files, the --remove-home option ensures a clean removal.

BASH

# Remove account and the associated home directory/mailbox
sudo deluser --remove-home expired_contractor

Deleting After Creating a Backup

This approach is ideal for archiving data while removing an account, such as a legacy_developer profile. The system generates a file named legacy_developer.tar.gz.

BASH

# Create a backup and remove the home directory
sudo deluser --remove-home --backup legacy_developer

# Confirming the backup file exists
ls *.tar.gz

Example Output

Backing up /home/legacy_developer to legacy_developer.tar.gz ...
Removing user `legacy_developer' ...
Done.

Customization

The --remove-all-files option is used to purge every file owned by a compromised_account from the entire system, including directories like /tmp and /var. Because this process scans the whole disk, it may take significant time to complete. Additionally, modifying /etc/deluser.conf allows administrators to set default behaviors, such as making home directory deletion or backup creation the standard practice for every removal.

BASH

# Thoroughly purge files for a compromised_account
sudo deluser --remove-all-files compromised_account

Important Notes

The deluser command is specific to Debian and Ubuntu environments and is not available on Red Hat-based systems like CentOS or RHEL, where userdel is the standard. Furthermore, the behavior of the command changes significantly based on the number of arguments provided. While a single argument targets a user for deletion, providing two arguments (User and Group) will trigger a group removal action instead. Finally, deluser does not forcefully terminate active processes; if the target user is currently logged in, the command may stop and issue a warning, requiring a manual termination of processes before proceeding.

Advanced Usage

Removing a User from a Specific Group

This method is used when an account should remain active but needs to lose specific privileges, such as removing a standard_user from the sudo or docker groups.

BASH

# Remove the user 'standard_user' from the 'sudo' group
sudo deluser standard_user sudo

Example Output

Removing user `standard_user' from group `sudo' ...
Done.

The standard_user account is preserved, but it no longer has administrative access.

Summary

The deluser command provides a more sophisticated and safer method for managing user accounts compared to the standard userdel tool. By utilizing options such as --remove-home and --backup, administrators can ensure that no orphaned data remains on the system while preserving important information in compressed archives. Understanding the dual role of this command—both for deleting users and managing group memberships—is essential for maintaining a secure and organized Linux environment. Mastering these features allows for a more streamlined administrative workflow, particularly in Debian-based systems where this tool is standard.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次