useradd - Linux
Overview
The useradd
command in Linux is used to create new user accounts. It sets up a new user with default settings and configurations based on the system’s user creation policies. This command is essential for systems administrators managing user accounts in multi-user environments or on servers.
Syntax
The basic syntax for useradd
is as follows:
useradd [options] USERNAME
Where USERNAME
is the name of the new user account to be created.
Options/Flags
Here are some commonly used options and flags for useradd
:
-d, --home-dir HOME_DIR
: Specifies the new user’s login directory.-e, --expiredate EXPIRE_DATE
: Sets the date on which the user account will be disabled.-g, --gid GROUP
: Specifies the primary group ID or name for the user account.-G, --groups GROUPS
: List of supplementary groups for the user.-m, --create-home
: Automatically create the user’s home directory if it does not exist.-s, --shell SHELL
: Path to the login shell for the user.-u, --uid UID
: Set the user’s unique ID. Using this option without specifying a UID will generate an appropriate one automatically.-M
: Do not create the user’s home directory, overriding system defaults.-k, --skel SKEL_DIR
: When creating a home directory, use files fromSKEL_DIR
.
The default behavior (if no options are provided) is to create a new user without a home directory, using system defaults for all other parameters.
Examples
- Create a new user with default settings:
useradd jdoe
- Create a new user with a home directory:
useradd -m jdoe
- Create a new user with a home directory and custom login shell:
useradd -m -s /bin/zsh jdoe
- Add a new user with a specific UID and multiple supplementary groups:
useradd -u 1100 -G staff,developers jdoe
Common Issues
- Permission Denied: Ensure you have sufficient permissions (usually root) to add a new user.
- Home Directory already exists: If specifying
-m
and the home directory exists, the command may fail unless-M
is also used. - Invalid User ID: Specifying a user ID (
-u
) that is already taken can result in a conflict. Check existing IDs withid USERNAME
.
Integration
useradd
can be integrated with other commands for complex scripts. For example, creating a user and setting a password:
useradd newuser && echo "newpassword" | passwd --stdin newuser
This sequence creates a new user and immediately sets a password for them, which can be useful in automated deployment scripts.
Related Commands
usermod
: Modify a user account.userdel
: Delete a user account.passwd
: Change user password.groupadd
: Add a new group.
For more detailed information or updates on useradd
, visit the Linux man page for useradd.