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

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

What to Know

  • To add a new user: Use the command useradd test ("test" is the new user's name). Use sudo useradd test if you lack the proper privileges.
  • To create a user with a home directory: Use sudo useradd -m test or sudo useradd -m -d /test test to change the default directory.
  • To set a user's password: Use passwd test and to switch users, use the su - test command.

This article explains how to create users in Linux using the command line and the "useradd" command. We also share commands to create users with a home directory, set a user's password, switch users, set an expiry date when creating a user, assign specific groups for a new user, adjust login defaults, create a user without a home folder, specify a user's full name, and view details about a particular user.

How to Create a User

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

The basic command to create a user (named test) would be:

useradd test

You'll need privileges to /etc/password to do this; as such, you'll likely sudo the above command:

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

Based on the /etc/defaults/useradd file, 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:

sudo 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:


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:

sudo useradd -m -e 2017-04-25 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 Them 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.

sudo useradd -g accounts john

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 many settings including the following, 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 -m -K PASS_MAX_DAYS=5 -K PASS_WARN_AGE=3 -K LOGIN_RETRIES=3 test

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:

sudo 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:

sudo useradd -m smithj -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 smithj /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 will always display as x)
  • Userid
  • User's group id
  • Full name of the user
  • User's home directory
  • Login shell

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.

Was this page helpful?