The useradd command creates a Linux user account. You need root access or permission to run it with sudo. Defaults, including home directory creation and the login shell, depend on your distribution and local configuration.
Create a user and set a password
Replace test_user with an unused account name. The -m option creates the user’s home directory:
sudo useradd -m test_user
sudo passwd test_userEnter and confirm the password when prompted. Do not supply a plain-text password with useradd -p: that option expects an encrypted password and its argument may be visible to other users through the process list.
Check the new account:
id test_userChoose a home directory
The creation commands below are alternatives to the first example. Choose the options you need before creating the account; do not run every example for the same username. Set the password afterwards with sudo passwd test_user.
Use -d to set the home directory path and -m to create it:
sudo useradd -m -d /home/test_user test_userTo create an account without a home directory, use -M:
sudo useradd -M test_userChoose a user ID or primary group
Use -u to specify a user ID. The example ID must be unused:
sudo useradd -m -u 1234 test_userUse -g to specify an existing primary group by name or group ID:
sudo useradd -m -g 1000 test_userReplace 1000 with the appropriate existing group ID. A user ID and a group ID are separate identifiers.
Set account expiry
Use -e to set the date when the account will expire. Replace YYYY-MM-DD with the required future date in that format:
sudo useradd -m -e YYYY-MM-DD test_userAccount expiry is separate from password expiry. This option sets when the account becomes unavailable.
Add a description or choose a shell
Use -c for an account description:
sudo useradd -m -c "Test account" test_userUse -s to choose an installed login shell:
sudo useradd -m -s /bin/sh test_userCheck available options
Read the help or local manual for the version installed on your server:
useradd --help
man useradd