The useradd command is used to create user accounts and configure basic settings. As part of the account creation process, useradd references several files:
- The account is stored in the /etc/passwd file.
- The account is configured according to various options set in the /etc/login.defs file.
- The account’s home directory is created at the /home/[account name] directory.
- The account’s home directory is populated using files from the /etc/skel directory.
By default, the useradd command does not set a password for the account. Since most Linux distributions will not permit a blank password, the account will exist but is not yet usable.
Syntax of useradd command
The syntax of the useradd command is:
# useradd [options] [user name]
useradd Command options
The useradd command includes many options to customize user accounts, as detailed in the below table.
|-c||Sets Comment Field||# useradd -c “User one” user01|
|-e||Sets account expiration date||# useradd -e 2021/12/31|
|-s||Sets user’s default shell||# useradd -s /bin/bash|
|-D||view default config for new users||# useradd -D|
If you encounter the below error while running the useradd command:
useradd: command not found
you may try installing the below package as per your choice of distribution.
|Debian||apt-get install passwd|
|Ubuntu||apt-get install passwd|
|Alpine||apk add shadow|
|Arch Linux||pacman -S shadow|
|Kali Linux||apt-get install passwd|
|Fedora||dnf install shadow-utils-2|
|Raspbian||apt-get install passwd|
useradd Command Examples
1. Adding the user with the default settings:
# useradd geek
2. To add the user with mentioning comments:
# useradd -c "Anything" geek
3. To add a user with mentioning home directory:
# useradd -d /tmp/geek geek
4. To add a user with an expiration date:
# useradd -e 2013-12-31 geek
5. To add a user account with the number of days of inactivity:
# useradd -f 2 geek
6. To add a user with specifying the primary group to it:
# useradd -g UNIX geek
7. To add a user by mentioning secondary groups to it:
# useradd -G Support,IT geek
8. To add a user while not creating the home directory for it:
# useradd -M geek
9. To create a user with a duplicate UID:
# useradd -u 500 -o geek
10. To create a system account:
# useradd -r geek
11. Creating a user by assigning a specific shell to him:
# useradd -s /bin/bash geek
12. Creating a user with a particular user ID:
# useradd -u 521 geek
The useradd command, when run without options, creates a user account with default parameters. The default parameters are read from the /etc/login.defs file and include parameters such as valid UID, GID number, default password aging rules, etc. Values from this file are used while creating a new user only. Apart from useradd, we can also use the adduser command to create a local user account in Linux. You can read more about adduser in the man page, using the man adduser command.