How to Create Users in Linux Using the 'useradd' Command

The 'useradd' command adds new users to your Linux system

Create users in Linux using the command line. While many desktop Linux distributions provide a graphical tool for creating users, it is a good idea to learn how to do it from the command line so that you can transfer your skills from one distribution to another without learning new user interfaces.

How to Create a User

The following command adds a new user named test to your system:

sudo useradd test 

What happens when this command is run depends on the contents of the configuration file located in /etc/default/useradd.

To view the contents of /etc/default/useradd, run the following command:

sudo nano /etc/default/useradd

The configuration file sets a default shell which in Ubuntu is bin/sh. All the other options are commented out.

User Add Config

The commented-out options allow you to set a default home folder, a group, the number of days after the password has expired before the account becomes disabled, and a default expiry date.

The important thing to glean from the above information is that running the useradd command without any switches may produce different results on different distributions and it is all to do with the settings in the /etc/default/useradd file.

The sudo utility is not installed on every distribution. If it's not installed, log in to an account with appropriate permissions for creating users.

How to Create a User With a Home Directory

The previous example is simple but the user may or may not have been assigned a home directory based on the settings file.

To force the creation of a home directory, use the following command:

useradd -m test

The above command creates a /home/test folder for the user test.

Add User With Home

How to Create a User With a Different Home Directory

If you want the user to have a home folder in a different place than the default, use the -d switch.

sudo useradd -m -d /test test

The above command creates a folder called test for user test under the root folder.

Add User With A Different Home

Within the -m switch, the folder may not be created. It depends on the setting in the /etc/login.defs file.

To get this to work without specifying a -m switch, edit the /etc/login.defs file. At the bottom of the file, add the following line:

CREATE_HOME yes

How to Change a User's Password Using Linux

After you create a user with a home folder, change the user's password.

To set a user's password, use the following command:

passwd test

The passwd command allows you to set the test user's password. You will be prompted for the password you wish to use.

Change User Password Linux

How to Switch Users

Test your new user's account by typing the following into a terminal window:

su - test

The above command switches user to the test account and, assuming you created a home folder, you will be placed in the home folder for that user.

Switch User Linux

Create a User With an Expiry Date

If you work in an office and a new contractor is going to be at your office for a short period of time, set an expiry date on their user account. Similarly, if you have family coming to stay, create a user account for that family member that expires after they depart.

To set an expiry date when creating a user, use the following command:

useradd -d /home/test -e 2016-02-05 test

The date must be specified in the format YYYY-MM-DD where YYYY is the year, MM is the month number, and DD is the day number.

Add User With Expiry

How to Create a User and Assign It to a Group

When a new user joins your company, assign specific groups for that user so that they have access to the same files and folders as other members of their team. For example, John is joining as an accountant.

The following command would add john to the accounts group.

useradd -m john -G accounts
Add User To Group

Adjust Login Defaults Within Linux

The /etc/login.defs file is a configuration file that provides the default behavior for login activities. There are some key settings in this file.

To open the /etc/login.defs file, enter the following command:

sudo nano /etc/login.defs
Login Defaults

The login.defs file contains the following settings which you might want to change:

  • PASS_MAX_DAYS: How long before a password expires. 
  • PASS_MIN_DAYS: How often a password can be changed.
  • PASS_WARN_AGE: Number of days warning before a password expires.
  • LOGIN_RETRIES: Number of login attempts before failure.
  • LOGIN_TIMEOUT: How long before the login times out.
  • DEFAULT_HOME: A user can log in if no home folder exists.

These are the default options and can be overridden when creating a new user.

How to Specify Login Password Expiry

Set a password expiry date, the number of login retries, and the timeout when creating a user. The following example shows how to create a user with a password warning, a maximum number of days before the password expires, and login-retries set.

sudo useradd test5 -m -K PASS_MAX_DAYS=5 -K PASS_WARN_AGE=3 -K LOGIN_RETRIES=1
Add User With Login Expiry Date

Force Creation of a User Without a Home Folder

If the login.defs file has the CREATE_HOME yes option set then when a user is created, a home folder is automatically created.

To create a user without a home folder regardless of the settings, use the following command:

useradd -M test
Add User With No Home Folder

It is confusing that -m stands for create home and -M stands for do not create home.

Specify the User's Full Name

As part of your user creation policy, you might choose to use the first initial followed by the last name. For example, the username for John Smith would be jsmith. When looking for details about a user, you might not then be able to distinguish between John Smith and Jenny Smith.

Add a comment when you create an account so it is easier to find the user's real name. Execute the following to add a comment:

useradd -m jsmith -c "John Smith"
Add User With Comments

Analyze the /etc/passwd File

When you create a user, the details of that user are added to the /etc/passwd file. To view the details about a particular user, use the grep command as follows:

grep john /etc/passwd

The above command returns details about all users with the word john as part of the username.

Linux User Information

The /etc/passwd file contains a colon-separated list of fields about each user. The fields are as follows:

  • Username
  • Encrypted password (which you can't see)
  • Userid
  • User's group id
  • Full name of the user
  • User's home directory
  • Login shell