Understanding the useradd Command

Basic Usage of useradd

The useradd command is used to create a new user account on a Linux system. It is typically run with superuser privileges and allows administrators to specify various options to configure the new user's account settings.

useradd [OPTIONS] USERNAME

Common Options for useradd

The useradd command supports a variety of options:

Examples of useradd Command

Create a New User

sudo useradd newuser

This command creates a new user named newuser with default settings.

Create a User with a Home Directory

sudo useradd -m newuser

This command creates a new user newuser and also creates a home directory at /home/newuser.

Create a User with a Specific Shell

sudo useradd -s /bin/zsh newuser

This command creates a new user newuser with the Zsh shell as the login shell.

Create a User and Assign to a Group

sudo useradd -g developers newuser

This command creates newuser and assigns them to the developers primary group.

Create a User with an Expiration Date

sudo useradd -e 2025-12-31 newuser

This command creates newuser and sets their account to expire on December 31, 2025.

Understanding User Creation Process

When a user is created using the useradd command:

Summary of Options

Option Description
-d, --home Specify the home directory for the new user. Default is /home/USERNAME.
-m, --create-home Create the user's home directory if it does not exist.
-s, --shell Specify the user's login shell. Default is usually /bin/bash.
-G, --groups Specify a comma-separated list of supplementary groups for the user.
-u, --uid Specify a numerical user ID for the new user.
-g, --gid Specify the primary group for the new user.
-p, --password Set the user's password (encrypted).
-e, --expiredate Set an expiration date for the user's account.
-f, --inactive Set the number of days after a password expires until the account is disabled.
-r, --system Create a system account.
-h, --help Display help information and exit.