2 ways to Create New User with home directory in Linux

Table of Contents

By default, a user’s home directory is usually located at /home/username where “username” is the name of the user account.

However, we can actually place a user’s home directory just about anywhere you’d like. Linux gives us the option to choose a location for the home directory whenever we are creating a new user.

  • To add a new user: Use the command useradd test (“test” is the new user’s name). Use sudo useradd test if we 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.

Create a New 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, we can use the following command: sudo useradd -m test

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

Create a New User With a Different Home Directory

If we want the user to have a home directory in a different place than the default, use the -d switch. sudo useradd -m -d /test test

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

Within the -m switch, the directory 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

Linux Troubleshooting Guide: