useradd Command: Options, -m and -s Usage, and Setting Passwords with passwd

Test environmentRHEL familyAlmaLinux 9.8shadow-utils 4.9root accountDebian 12 regular account
Users · groupsuseraddpasswdLocation/usr/sbin/useradd

The useradd command adds a new user account on Linux, and the passwd command sets or locks that account's password. Comparing the useradd defaults and configuration files on AlmaLinux 9 and Debian 12 servers showed that AlmaLinux was set up to create a home directory and use bash as the login shell even without options, while Debian had no setting to create a home directory and its default shell was sh.

The output below was produced on an AlmaLinux 9.8 server under the root account and on a Debian 12 server under a regular account. Password change dates, test account names, and the version numbers of the shadow-utils and passwd packages have been replaced with example values.

1️⃣ How to Use the useradd Command

The basic form is [useradd options username]. Because it modifies /etc/passwd and /etc/shadow, which hold account information, run it as root or with sudo.

useradd user1

According to the exit code table in the official manual, useradd ends with 0 on success and with 9 when an account with the same name already exists. Check the new account with id or getent passwd; the output format of id is shown below.

uid=1000(user1) gid=1000(user1) groups=1000(user1)

The UID of a new account is set to the next value after the largest number already in use, at or above UID_MIN 1000 in /etc/login.defs. A group with the same name as the account is created as well.

A line in /etc/passwd consists of seven fields separated by colons. The root account line was looked up on the AlmaLinux server.

getent passwd root
root:x:0:0:root:/root:/bin/bash

From the left, the fields are the account name, password field, UID, GID, comment, home directory, and login shell. The x in the password field means that the actual password hash is stored separately in /etc/shadow.

2️⃣ Creating a Home Directory with useradd -m

The -m option creates a home directory named after the account under /home and copies the files in /etc/skel into it. Whether a home directory is created even without -m is decided by the CREATE_HOME value in /etc/login.defs.

grep CREATE_HOME /etc/login.defs
CREATE_HOME	yes

On the AlmaLinux 9.8 server, CREATE_HOME is yes, so just typing useradd user1 creates /home/user1. On the Debian 12 server, the same grep found no line in /etc/login.defs and ended with exit code 1. The official manual states that if CREATE_HOME is not enabled, no home directory is created without -m.

On a Debian 12 server, also check the default shell. Printing the defaults from a regular account showed SHELL as /bin/sh.

/usr/sbin/useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
LOG_INIT=yes

On the Debian 12 server, /bin/sh was linked to dash. To have a new account log in with bash, specify the home directory and the shell together.

useradd -m -s /bin/bash user1

The files copied into the home directory are in /etc/skel.

ls -lA /etc/skel
total 12
-rw-r--r--. 1 root root  18 Apr 30  2024 .bash_logout
-rw-r--r--. 1 root root 141 Apr 30  2024 .bash_profile
-rw-r--r--. 1 root root 492 Apr 30  2024 .bashrc

/etc/skel on the AlmaLinux server held three bash configuration files, and the Debian 12 server had .bash_logout, .bashrc, and .profile. If there are configuration files every new account should get, put them in /etc/skel in advance; to use a different directory, specify it with the -k option.

Conversely, adding -M creates no home directory even when CREATE_HOME is yes.

3️⃣ useradd Options

Print the list of options with useradd --help. The commonly used options in shadow-utils 4.9 on the AlmaLinux 9.8 server are as follows.

OptionCopyDescription
-m, --create-homeCreates the home directory and copies the /etc/skel files
-M, --no-create-homeDoes not create a home directory
-s, --shellSets the login shell
-g, --gidSets the primary group; existing groups only
-G, --groupsSets supplementary groups, separated by commas when there are several
-c, --commentFills in the comment field (GECOS)
-D, --defaultsPrints the defaults; with options added, changes the defaults
OptionCopyDescription
-d, --home-dirSets the home directory path
-u, --uidSets the UID
-e, --expiredateSets the account expiry date (YYYY-MM-DD)
-f, --inactiveNumber of days after the password expires until the account can no longer be used
-r, --systemCreates a system account
-N, --no-user-groupDoes not create a group with the same name as the account
-p, --passwordSets an encrypted password

Several options can be written together on one line. This example sets the home directory path, the shell, and the comment field at once.

useradd -m -d /home/web1 -s /bin/bash -c 'Web user' web1

-G adds groups while leaving the primary group as it is. /etc/sudoers on the AlmaLinux server contained a line allowing the wheel group to run all commands. So an account added to the wheel group can run commands with root privileges through sudo.

grep -E '^%wheel' /etc/sudoers
%wheel	ALL=(ALL)	ALL
useradd -G wheel user2

Looking up an account created this way with id shows 10(wheel) appended in the groups field.

uid=1001(user2) gid=1001(user2) groups=1001(user2),10(wheel)

-r creates a system account used by service programs. The UID is chosen between SYS_UID_MIN 201 and SYS_UID_MAX 999 in /etc/login.defs. According to the official manual, an -r account gets no home directory even when CREATE_HOME is yes, and an account that never needs to log in has its shell set to /sbin/nologin.

useradd -r -s /sbin/nologin svc1

-p is an option that takes an encrypted password value. The official manual advises against it because the value can be exposed to users who view the process list, so set the password with passwd after creating the account.

🔲 useradd Command Simulator

In the simulator below, you can type the steps yourself: create an account with useradd, set its password with passwd, and check it with id and getent.

useradd Command Simulatorshadow-utils 4.9
[root@server work]#
This is a virtual AlmaLinux server that runs only inside the browser; the accounts you create appear in the table on the left and in the /home tree, and ↑↓ recalls the commands you typed earlier.

4️⃣ Practical useradd Command Examples

  • ↪️ Creating a development account and common beginner mistakes

🔲 Creating a Development Account

An account for a developer was created in one line, with a home directory, the bash shell, a comment field, and the wheel group. useradd finished without printing anything, and the specified values could be seen as they were in the output of id and in the /etc/passwd line.

~]# useradd -m -s /bin/bash -c 'Dev User' -G wheel devuser
~]# id devuser
uid=1001(devuser) gid=1001(devuser) groups=1001(devuser),10(wheel)
~]# grep '^devuser:' /etc/passwd
devuser:x:1001:1001:Dev User:/home/devuser:/bin/bash

The home directory was created with drwx------ permissions that only that account can open, and the three bash configuration files from /etc/skel were copied into it. Because the server has CREATE_MAIL_SPOOL=yes, a mail spool file of size 0 was created as well.

~]# ls -la /home/devuser
total 20
drwx------. 2 devuser devuser 4096 Nov 14 07:40 .
drwxr-xr-x. 4 root    root    4096 Nov 14 07:40 ..
-rw-r--r--. 1 devuser devuser   18 Apr 30  2024 .bash_logout
-rw-r--r--. 1 devuser devuser  141 Apr 30  2024 .bash_profile
-rw-r--r--. 1 devuser devuser  554 Nov  3 02:28 .bashrc
~]# ls -l /var/spool/mail/devuser
-rw-rw----. 1 devuser mail 0 Nov 14 07:40 /var/spool/mail/devuser

Checking with passwd -S right after creating the account showed the status LK.

~]# passwd -S devuser
devuser LK 2026-11-14 0 99999 7 -1 (Password locked.)

After setting the password by typing it twice with passwd devuser, checking again showed the status changed to PS.

~]# passwd -S devuser
devuser PS 2026-11-14 0 99999 7 -1 (Password set, SHA512 crypt.)

🔲 Common Beginner Mistakes

Writing a group that did not exist yet after -G ended with exit code 6 without creating the account. Groups to be added with -G must be created beforehand.

~]# useradd -G developers bob
useradd: group 'developers' does not exist

When the shell path was written wrong, only one warning line appeared and the account was created anyway. The nonexistent path /bin/bsh was recorded in the last field of /etc/passwd, so check this field again after creating the account.

~]# useradd -s /bin/bsh bob
useradd: Warning: missing or non-executable shell '/bin/bsh'
~]# grep '^bob:' /etc/passwd
bob:x:1002:1002::/home/bob:/bin/bsh

Another mistake is mixing up lowercase -g and uppercase -G. When the earlier bob was deleted with userdel -r and created again with -g wheel, wheel became the primary group, with no group named after the account.

~]# useradd -g wheel bob
~]# id bob
uid=1002(bob) gid=10(wheel) groups=10(wheel)

An account created with -M has no home directory, so switching to it with su - produced the warning [su: warning: cannot change directory to /home/tempuser: No such file or directory].

~]# useradd -M tempuser
~]# ls -ld /home/tempuser
ls: cannot access '/home/tempuser': No such file or directory
~]# su - tempuser -c pwd
su: warning: cannot change directory to /home/tempuser: No such file or directory
/root

🔲 Reading the Results of useradd

useradd prints nothing on success, so check the result in the account information files. In /etc/group, devuser was appended to the member list at the end of the wheel line added with -G, and the devuser group with the same name as the account had an empty member field.

~]# grep -E '^(wheel|devuser):' /etc/group
wheel:x:10:linuxuser,devuser
devuser:x:1001:

In /etc/shadow, the second field was [!!] before the password was set, and the third field, 20771, is the number of days counted from January 1, 1970, which is November 14, 2026.

~]# grep '^devuser:' /etc/shadow | cut -d: -f1-3
devuser:!!:20771

You can type useradd, passwd, and id yourself in the useradd Command Simulator.

The password validity period and the account expiry date of an account created this way are set with the chage command.

5️⃣ useradd Defaults and /etc/login.defs

Running useradd -D without options prints the defaults stored in /etc/default/useradd. This is the result on the AlmaLinux server.

useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

HOME, SHELL, and SKEL are the parent path where home directories are created, the default shell, and the directory holding the files to copy. GROUP=100 is the number of the default group users, used when no group with the same name as the account is created. INACTIVE=-1 and the empty EXPIRE mean that no deactivation after password expiry and no account expiry date have been set.

Adding an option such as -s after -D changes the values in /etc/default/useradd instead of printing the defaults. To only check the defaults, run -D on its own.

The UID range, home directory permissions, and password storage method are set in /etc/login.defs.

grep -E '^(UID_MIN|UID_MAX|SYS_UID_MIN|SYS_UID_MAX|HOME_MODE|USERGROUPS_ENAB|ENCRYPT_METHOD|CREATE_HOME)' /etc/login.defs
HOME_MODE	0700
UID_MIN                  1000
UID_MAX                 60000
SYS_UID_MIN               201
SYS_UID_MAX               999
ENCRYPT_METHOD SHA512
USERGROUPS_ENAB yes
CREATE_HOME	yes

HOME_MODE 0700 means new home directories are created with permissions that only that account can open, and ENCRYPT_METHOD SHA512 means passwords set with passwd are stored as SHA-512 hashes. USERGROUPS_ENAB yes is the setting that creates a group with the same name for each new account.

/etc/login.defs on the Debian 12 server had the same UID_MIN 1000 and ENCRYPT_METHOD SHA512, but no HOME_MODE or CREATE_HOME lines. The useradd -D output on Debian included CREATE_MAIL_SPOOL=no, which means no mail spool file is created.

6️⃣ How to Use the passwd Command

With an account name, the passwd command changes that account's password; run without a name, it changes the password of the account currently logged in. Only root can change another account's password.

passwd user1
Changing password for user user1.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

The password you type is not shown on screen, and it is saved only when the two entries match. If they differ, [Sorry, passwords do not match.] appears and the new password is asked for again. On the AlmaLinux server, the pam_pwquality line that checks passwords in the PAM configuration has retry=3, so the password is asked for up to three times in total.

The default minimum length in pwquality is 8 characters, so a shorter password gets [BAD PASSWORD: The password is shorter than 8 characters]. On the AlmaLinux 9.8 server, the enforce_for_root line in pwquality.conf was commented out, but the same value was attached to the PAM configuration line. So even when a short password was entered as root, the new password was asked for again after the warning, and it finally ended with [Password change has been aborted.].

The reason a regular account can change its own password lies in the permissions of two files.

stat -c "%A %U %G %n" /usr/bin/passwd /etc/shadow
-rwsr-xr-x root root /usr/bin/passwd
---------- root root /etc/shadow

/etc/shadow has all permission fields empty, so only root can read and write it. /usr/bin/passwd has an s (setuid) in the owner field, so even when a regular account runs it, it acts with root privileges and writes the new password to /etc/shadow.

7️⃣ passwd Options and Checking the Status

passwd options differ because each distribution ships a different program. On the AlmaLinux server, passwd was in a passwd package separate from shadow-utils.

rpm -qf /usr/sbin/useradd /usr/bin/passwd
shadow-utils-4.9-10.el9.x86_64
passwd-0.80-10.el9.x86_64

On the Debian 12 server, useradd and passwd were both in the passwd package. Based on passwd --help on the AlmaLinux 9.8 server, the commonly used options are as follows, and all of them can be used only by root.

OptionTask
-S, --statusPrints the password status in one line
-l, --lockLocks the password by adding ! in front of the hash
-u, --unlockUnlocks the password
-d, --deleteDeletes the password
-e, --expireExpires the password right away so it must be changed at the next login
-x, --maximumMaximum number of days the password can be used
-n, --minimumMinimum number of days the password must be used
-w, --warningNumber of days of warning before expiry
-i, --inactiveNumber of days after expiry until the account is deactivated
--stdinReads the new password from standard input

passwd --help on the Debian 12 server had no --stdin, and it had -a, which prints the status of all accounts together with -S.

-S prints the password status in one line.

passwd -S root
root PS 2026-01-15 0 99999 7 -1 (Password set, SHA512 crypt.)

The fields are, in order, the account name, status, last password change date, minimum days, maximum days, warning days, and inactive days. Status PS is a usable password, LK is a locked password, and NP means there is no password. According to the official manual, an account created by useradd has its password disabled, so it cannot log in with a password until one is set with passwd.

On AlmaLinux, -S can be used only by root. When root borrowed the privileges of the nobody account with runuser and ran it, it ended with [Only root can do that.].

runuser -u nobody -- passwd -S nobody
Only root can do that.

On the Debian 12 server, a regular account could check its own status. The status field showed P instead of PS, and there was no description in parentheses at the end.

passwd -S
webuser P 2026-01-20 0 99999 7 -1

Looking up root's status from the same regular account was blocked with [You may not view or modify password information for root.].

Even after the password is locked with -l, according to the official manual, the account can still log in with other authentication such as SSH keys. To block the whole account, use [chage -E 0 account].

🔢 FAQ & Recommended Content

On the AlmaLinux 9.8 server, /usr/sbin/adduser was a link to useradd, so both commands were the same program. On the Debian 12 server, adduser is a separate program from the adduser package, and the official manual describes it as a program that wraps lower-level tools such as useradd and asks for the comment field and the password.

useradd is in /usr/sbin. /etc/login.defs on the Debian 12 server did not put /usr/sbin in the PATH of regular accounts, and typing [useradd -D] from a regular account ended with [command not found]. Add [sudo] or run it from the root account.

passwd on RHEL-family systems such as AlmaLinux has a --stdin option that reads the new password from standard input, and only root can use it. passwd on the Debian 12 server did not have this option. Both servers had [chpasswd], and according to the official manual it reads lines in the form [account:password] from standard input and sets the passwords of several accounts at once. Passwords typed directly on the command line can remain in the shell history.

ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 4   +   5   =