This article is part of the following series.
- 2 ways to Create New User with home directory in Linux
- 4 Ways to Find User Home Directory in Linux
- 3 ways to change user home directory in Linux
In Linux, the home directory (also called “home” or “home folder”) is a directory designated for a particular user, where their personal files and configuration settings are stored.
Each user on a Linux system has their own home directory, and it is identified by a tilde (~) followed by the user’s username. For example, if the username is “john”, his home directory would be ~john.
In this article, we will explore how to create a new user with a home directory in Linux.
Table of Contents
Home directory for New user in Linux
There are two files that define the default info for new users.
- /etc/login.defs – This file defines entries for mail, password complexity and limitations, UID and GID minimum and maximum values, and whether the user’s home directory is created by default. The CREATE_HOME parameter in the /etc/login.defs file is used to specify whether or not to create a home directory for a user when the user account is created.
- /etc/default/useradd – This file defines the defaults of shell, home directory, skel file, group id etc. The HOME parameter in the /etc/default/useradd is used to specify the default home directory location for new users.
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 to create a user with the default home directory
- Use sudo useradd -m -d /test test to create a user with a non-default home directory
Create a New User With a default Home Directory location in Linux
The CREATE_HOME parameter in the /etc/login.defs file is used to specify whether or not to create a home directory for a user when the user account is created. The default value of CREATE_HOME is yes, which means that a home directory will be created for a new user account.
If you set CREATE_HOME to no, then the system will not create a home directory for the user when the account is created. This can be useful in situations where you have a large number of users and you do not want to create a home directory for each user.
To change the value of CREATE_HOME in the /etc/login.defs file, you will need to edit the file using a text editor with administrative privileges, such as the vi or nano editor. Once you have opened the file, locate the line that contains the CREATE_HOME parameter and change its value to either yes or no, depending on your requirements.
It is important to note that changing the value of CREATE_HOME in the /etc/login.defs file will only affect new user accounts that are created after the change has been made. Existing user accounts will not be affected.
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.
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 force to 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 about this option.
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.
Here is an example of how to create a new user with a home directory in Linux:
- Open a terminal window on your Linux system.
- Run the following command as a user with administrative privileges to create a new user with a home directory:
- sudo useradd -m newuser
- In this command, newuser is the name of the user you want to create.
- The -m option tells the useradd command to create a new home directory for the user.
- Set a password for the new user by running the following command:
- sudo passwd newuser
- This will prompt you to enter and confirm a new password for the user.
- You can now switch to the new user account using the su command followed by the username of the new user (su – newuser) and start using the account.
Create a New User With a non-default Home Directory location in Linux
The /etc/default/useradd file is a configuration file for the useradd command in Linux/Unix systems. It contains default settings for user account creation, such as the default home directory, default shell, default user group, etc.
For example, if the HOME variable is set to /home, then the home directory for a user named “jdoe” would be /home/jdoe.
The home directory for a user can be changed when the user is createad.
If we want the user to have a home directory in a different place than the default, use the useradd -d option.
useradd -m -d /test test
This command will create a new user test with the home directory under “/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: