Daily Linux Command: UserAdd

<span>useradd</span> is the command used in Linux systems to create new user accounts. It belongs to system management commands and typically requires root privileges (or the use of <span>sudo</span>) to execute.

🔧 Basic Syntax

useradd [options] username

📌 Common Options

Option Description
<span>-m</span> Create the user’s home directory (usually named after the username, located at <span>/home/username</span>)<br>It is strongly recommended to use this option, otherwise the user will have no home directory.
<span>-s</span> Specify the user’s login shell, such as <span>/bin/bash</span>, <span>/bin/sh</span>, <span>/sbin/nologin</span>, etc.
<span>-c</span> Add user description information (GECOS field), such as full name, phone number, etc.
<span>-d</span> Specify the user’s home directory path (must be used with <span>-m</span>).
<span>-u</span> Specify the user’s UID (User ID), which must be unique.
<span>-g</span> Specify the user’s primary group (initial login group).
<span>-G</span> Specify the user’s supplementary groups (can be multiple, separated by commas).
<span>-e</span> Set the account expiration date, in the format <span>YYYY-MM-DD</span>.
<span>-p</span> Set the encrypted password (it is not recommended to set it in plain text; use the <span>passwd</span> command instead).

✅ Example Usage

1. Create a regular user and create a home directory, specify shell

sudo useradd -m -s /bin/bash alice

This will create the user <span>alice</span>, with the home directory at <span>/home/alice</span>, and the default shell as Bash.

2. Set the user’s password (must be set separately)

sudo passwd alice

The system will prompt you to enter and confirm the password.<span>useradd</span> does not automatically set a password.

3. Create a user and specify primary and supplementary groups

sudo useradd -m -g developers -G sudo,docker bob

Create the user <span>bob</span>, with the primary group as <span>developers</span>, and also add to the <span>sudo</span> and <span>docker</span> groups.

4. Create a user and add remarks

sudo useradd -m -c "John Doe, DevOps Team" john

5. Create a user and specify a custom home directory

sudo useradd -m -d /opt/users/jane jane

6. Create a user and set account expiration date

sudo useradd -m -e 2026-12-31 tempuser

🛠 View User Information

id username        # View user UID, GID, and groups
grep username /etc/passwd   # View basic user information
ls /home          # Check if the home directory is created

🔄 Modify Existing Users (Use usermod)

<span>useradd</span> is only for creating users; to modify users, please use <span>usermod</span>:

sudo usermod -G wheel,sudo alice   # Modify supplementary groups
sudo usermod -s /bin/zsh alice     # Change default shell

⚠️ Notes

  1. You must use <span>-m</span> to create a home directory: Many distributions (like CentOS) do not create it by default.
  2. Password must be set separately using <span>passwd</span>: The <span>-p</span> parameter accepts an encrypted password; writing in plain text poses security risks.
  3. UID and GID conflicts: Avoid manually specifying existing UIDs/GIDs.
  4. Behavior differences across distributions:
  • Debian/Ubuntu:<span>adduser</span> is more commonly used (it is a Perl script, interactive).
  • CentOS/RHEL:<span>useradd</span> is the standard tool.

<span>useradd</span> vs <span>adduser</span>

Tool Type Features
<span>useradd</span> Low-level command (binary) Highly versatile, commonly used in scripts, but does not create a home directory by default (on some systems)
<span>adduser</span> High-level script (Perl) Provided by default in Debian/Ubuntu, interactive, automatically creates home directory and sets password

It is recommended to use <span>useradd</span> in scripts to ensure cross-platform consistency.

✅ Recommended Complete User Creation Process

sudo useradd -m -s /bin/bash -c "Alice Smith" alice
sudo passwd alice
# Enter password
sudo usermod -aG sudo alice   # If sudo privileges are needed

Leave a Comment