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

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

Create users within 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

User Add Config

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

sudo useradd test 

What will happen 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 will set a default shell which in Ubuntu is bin/sh. All the other options are commented out.

The commented-out options allow you to set a default home folder, a group, 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 is not installed you need to log into an account with appropriate permissions for creating users.

How To Create A User With A Home Directory

Add User With Home

The previous example was fairly 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 to use the following command:

useradd -m test

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

How To Create A User With A Different Home Directory

Add User With A Different Home

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

sudo useradd -m -d /test test

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

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

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


How To Change A User's Password Using Linux

Change User Password Linux

Now that you have created a user with a home folder you will need to change the user's password.

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

passwd test

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

How To Switch Users

Switch User Linux

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.

Create a User With an Expiry Date

Add User With Expiry

If you are working in an office and you have a new contractor starting who is going to be at your office for a short period of time then you will want to set an expiry date on his or her user account.

Similarly, if you have family coming to stay then you can create a user account for that family member that expires after he or she has departed.

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.

How to Create a User and Assign It to a Group

Add User To Group

If you have a new user joining your company then you might want to 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, imagine you had a guy called John and he was joining as an accountant.

The following command would add john to the accounts group.

useradd -m john -G accounts

Adjusting Login Defaults Within Linux

Login Defaults

The file /etc/login.defs 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

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 can a password 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 is it before the login times out.
  • DEFAULT_HOME: Can a user login if no home folder exists.

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

How to Specify Login Password Expiry

Add User With Login Expiry Date

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

Force Creation of a User Without a Home Folder

Add User With No Home Folder

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

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

useradd -M test

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

Specify the User's Full Name

Add User With Comments

As part of your user creation policy, you might choose to do something like the first initial, followed by the last name. For example, the username for John Smith will 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"

Analysing the '/etc/passwd' File

Linux User Information

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 will return details about all users with the word john as part of the username.

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

The fields are as follows:

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