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|
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 user with mentioning home directory:
# useradd -d /tmp/geek geek
4. To add user with expiration date:
# useradd -e 2013-12-31 geek
5. To add user account with number of days of inactivity:
# useradd -f 2 geek
6. To add user with specifying primary group to it:
# useradd -g UNIX geek
7. To add user with mentioning secondary groups to it:
# useradd -G Support,IT geek
8. To add user while home directory will not be created for it:
# useradd -M geek
9. To create a user with duplicate UID:
# useradd -u 500 -o geek
10. To create a system account:
# useradd -r geek
11. Creating user with assigning specific shell to him:
# useradd -s /bin/bash geek
12. Creating user with particular user ID:
# useradd -u 521 geek