Differences and Proper Usage of useradd and adduser in Linux

When operating Linux systems, account management tasks such as adding and deleting users are among the fundamental responsibilities. However, when attempting to create a user in a CLI environment, you will often encounter two remarkably similar commands: useradd and adduser, which frequently causes confusion.

These commands are not merely variations in spelling or aliases; their design philosophies and the execution layers of their processes are fundamentally different. This article aims to clarify the internal structure and operational differences of both commands, and to establish appropriate criteria for using them properly depending on OS distribution differences and operational procedures.

目次

Eliminating Distribution-Specific Behavioral Inconsistencies and Automation Errors

The goals of this article are the following two points:

  1. To accurately understand the operational specifications of user management commands in Ubuntu/Debian-based systems versus RHEL-based systems (such as AlmaLinux and Rocky Linux), completely eliminating unintended parameter shortages and environment setup errors during manual configuration.
  2. To 100% achieve robust command invocation that does not halt due to errors or interactive prompts when automating environment setups using shell scripts or configuration management tools.

The Home Directory Non-Creation Incident and Distribution-Specific Traps

Common challenges encountered by Linux beginners and engineers working across different OS environments include the following two points:

First, when creating a user using the useradd command in an Ubuntu environment, a home directory is not generated by default, nor is an appropriate shell (such as /bin/bash) assigned. If you switch to that user in this state, problems occur such as corrupted prompt displays or login initialization processes failing to execute normally.

Second, the actual entity of the adduser command differs depending on the Linux distribution being used. In Debian/Ubuntu-based systems, it is implemented as an independent, high-level Perl script, whereas in RHEL-based systems, it is a symbolic link (simply an alias) to useradd. Consequently, this creates confusion where executing the exact same command results in different behavior depending on the OS.

Structural Differences Between Low-Level Binaries and High-Level Scripts

To avoid these issues, it is necessary to understand the classification and mechanics of these commands.

useradd (Low-Level Command)

  • Classification: Utility binary (Low-level command)
  • Characteristics: A built-in binary standardly included in all Linux distributions. When executed by itself, it only writes minimal account information to /etc/passwd and /etc/shadow.
  • Behavior: The creation of a home directory, specification of a shell, and setting of a password must be explicitly designated via command-line options. It operates according to the definitions in the configuration file /etc/default/useradd.

adduser (High-Level Command)

  • Classification: Interactive script (High-level command)
  • Characteristics: A Perl script developed for Debian and Ubuntu systems (*excluding RHEL-based systems).
  • Behavior: It sequentially invokes low-level commands such as useradd and passwd internally. When executed, it interactively (via prompts) requests password entry and GECOS information such as full names, automatically creating the home directory and copying skeleton files (/etc/skel). Its configuration relies on /etc/adduser.conf.

A comparison of the main operational specifications of both commands is as follows:

Comparison Item useradd (Debian/Ubuntu) adduser (Debian/Ubuntu) adduser on RHEL Systems
Command Entity C language binary Perl script Symbolic link to useradd
Processing Method Non-interactive (batch execution) Interactive (prompt response) Non-interactive (same as useradd)
Automatic Home Directory Creation None (-m required) Yes (automatic creation) Yes (per RHEL default settings)
Password Setting Requires separate passwd Requested automatically during execution Requires separate passwd
Suitable Use Cases Scripts and automated processing Manual creation in terminal Same as useradd

Operational Comparison of Deletion Commands (userdel and deluser)

Similar to user creation commands, there are two types of user deletion commands: userdel and deluser.

userdel is a low-level command common to all distributions. By default, it only deletes account information from files such as /etc/passwd, leaving the home directory and mailbox behind on the system. If you want to delete these simultaneously, you must explicitly provide the -r (--remove) option.

# Completely delete the user including their home directory
userdel -r username

On the other hand, deluser in Debian/Ubuntu-based systems is a high-level script that safely handles processing based on the contents of the configuration file /etc/deluser.conf. It incorporates flexible safety measures to prevent accidents, such as a feature to archive and back up the home directory before deletion (--backup) and a feature to search for and delete all files owned by that user across the system (--remove-all-files).

# Safely delete a user while acquiring a backup (Debian/Ubuntu)
deluser --remove-home --backup username

Note that RHEL-based distributions do not bundle the deluser command, so you should always use userdel on them.

Proper Usage and Implementation Code Examples in Practical Operations

In actual operations, it is best to choose between them based on the following guidelines.

Interactive Manual Management (For Ubuntu/Debian)

When manually creating new users one by one on the terminal, use adduser, which allows you to interactively process the necessary configurations all at once.

sudo adduser newuser

Automated Processing via Shell Scripts or Ansible

In CI/CD pipelines, initialization scripts, and automation environments like Ansible or Cloud-init, always adopt useradd with explicitly specified options to prevent processing halts caused by interactive prompts and to ensure cross-distribution compatibility.

# Create a user by explicitly specifying home directory creation (-m), default shell (-s), and group (-g)
sudo useradd -m -s /bin/bash -g users -G sudo devuser

# Set initial password in a non-interactive environment (configure as needed)
echo "devuser:SecurePassword123!" | sudo chpasswd

Additionally, if you want to change the default behavior, edit /etc/default/useradd.

# Configuration example for /etc/default/useradd
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

Conclusion

Linux user creation commands are divided into useradd, a low-level binary, and adduser, implemented as a high-level script in Debian-based systems. While using useradd on systems like Ubuntu does not generate a home directory by default, this is simply due to the specifications of low-level commands.

For manual management, adduser is suitable because it allows for safe, interactive configuration, and using deluser upon deletion enables flexible processing accompanied by backups. Conversely, in shell scripts and automation tools, standard design dictates using useradd with explicitly provided options to ensure it operates reliably and non-interactively without distribution dependencies.