howtouselinux

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 in Linux: 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 in Linux: 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 in Linux

You can easily create a new user with a home directory in Linux using useradd command with -m option. It will force the creation of a home directory for new users. no matter the configuration of CREATE_HOME in /etc/login.defs file.

Based on the configuration on /etc/login.defs file, the user may or may not have been assigned a home directory. If the CREATE_HOME is configured to yes, a home directory will be created for the new user by default. We don’t need to specify any option.

If the CREATE_HOME is configured to no, a home directory will be not created for the new user. In this case,  we can use the following command: sudo useradd -m test. It will create a home directory for the user test.

Check this post to get more about how to check the user’s home directory in Linux.

Here is the man info.

 -m, –create-home

Create the user’s home directory if it does not exist. The files and directories contained in the skeleton directory (which can be defined with the -k option) will be copied to the home directory.

By default, if this option is not specified and CREATE_HOME is not enabled, no home directories are created. The directory where the user’s home directory is created must exist and have proper SELinux context and permissions. Otherwise the user’s home directory cannot be created or accessed.

Create a New User With a Different Home Directory in Linux

If we want the user to have a home directory in a different place than the default, use the useradd -d option.

sudo useradd -m -d /test test

This command will change the home directory of the user “test” to “/test”. The user’s files will be put to this home directory.

You can also change existing user’s home directory with usermod command.

usermod -d /test test

The above command will change the home directory of test user to /test under the root directory.

Linux Troubleshooting Guide:

Welcome to howtouselinux.com!

Our website is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative, and we welcome your feedback and suggestions for future content.

Learn More

Facebook
Twitter
LinkedIn