How to Create a User in Linux (With Home Directory)

Create the user and create its home directory in one step
The -m option offers this functionality. Enter the following command:
sudo useradd -m aliceHere, the useradd command creates the account, and the -m option tells it to create the home directory at /home/alice and populate it with the standard startup files taken from /etc/skel. Without the -m option, the home directory will not be created, and this situation should be avoided.
Set a password
Initially, a user account created with useradd is locked and does not have a password. Set it before anyone tries to log in using this user:
sudo passwd aliceYou will be prompted twice: first to enter the password and second to confirm it. Otherwise, you will end up with a user that cannot authenticate himself.
Check the account and its home directory
The following commands will be enough to verify this configuration:
id alice
ls -la /home/alice
getent passwd aliceThe id command will show that the account has been created, together with its UID and group. The ls -la command will show the presence and correct ownership of the directory. The getent passwd command will output the whole password-database record, including the path to the home directory that bash will use as the actual home directory of the user. The path to it is shown only here in case the non-standard path to the home directory was passed to useradd with -d option.
Reasons for useradd to not create the home directory
Running a bare useradd alice command without any flags on some systems may result in the creation of a valid account without creating a home directory. This is not a bug but rather a function controlled by CREATE_HOME flag in /etc/login.defs and differs between distribution systems.
According to the useradd manual, the -m option is significant "if this option is not specified and CREATE_HOME is not enabled"—this means that if CREATE_HOME is enabled system-wide, the home directory will be created anyway without using the -m option. Systems based on Red Hat usually ship with CREATE_HOME=yes, and thus the home directory will be created automatically regardless of -m option. Distribution systems such as Debian and Ubuntu usually leave CREATE_HOME unset, and thus a call to useradd without -m will not create the home directory on these systems. In case if a setup script needs to be consistent on both types of systems, it is better to use -m option.
Check your system defaults before relying on them
Before using any defaults of useradd, you should check them by running the following command:
useradd -DIt will print the current defaults taken from /etc/default/useradd—this includes the base directory where the new home directories will be placed, the default shell and the skeleton directory that will be copied. It is a good practice to run it on any new server, as the system-wide defaults can be changed and apply to all subsequent user accounts. It is worth noting that the -D command will not show you the CREATE_HOME option, as it lives in a different place—namely, in /etc/login.defs, unlike the -D option.
adduser: alternative for Debian/Ubuntu
Debian and Ubuntu provide another utility called adduser, which is not just a convenient interface for useradd but actually a distinct Perl script which runs useradd behind the scenes and prompts the user about additional settings, including password and the account configuration, and creates the home directory automatically (no need for -m).
sudo adduser aliceBut adduser behavior is not the same on Fedora and RHEL systems, where adduser is just a symbolic link to useradd and behaves similarly to it—without interactive prompts. So, in case if an automation is done on multiple types of systems, useradd -m should be preferred exactly because of its consistency of the behavior no matter what system is running.
Customization of the account
A short list of the flags should be enough for common customizations of the account:
Flag | Meaning |
|---|---|
| Create the home directory if it does not exist |
| Set a custom home directory path instead of |
| Use a custom skeleton directory instead of |
| Set login shell, e.g., |
| Add user to one or more supplementary groups |
| Set the GECOS field, usually user's full name |
Example, with combined options:
sudo useradd -m -d /srv/users/alice -s /bin/bash -G sudo,docker -c "Alice Chen" aliceIt will create an account with its home directory placed in /srv/users/alice, login shell will be set to /bin/bash, Alice will belong to sudo and docker groups, and full name will be saved to the GECOS field.
Fixing account without home directory
In case if the account is created without -m option and this mistake is noticed afterwards, there is no need to delete and recreate the account. Just set its home directory:
sudo usermod -d /home/alice -m aliceThe -m option of usermod will move the content of the existing home directory to the new path if it exists or will create the directory if it does not. But it will not copy the content of /etc/skel, so additional steps are required:
sudo cp -rT /etc/skel /home/alice
sudo chown -R alice:alice /home/aliceUser account creation is one of the first steps during setup of the server. OrbitServers VPS and bare-metal instances provide full root access starting from the very first boot of the server.
Get started with Orbit Servers
Low-latency VPS, bare metal, and colocation across the US, EU, and APAC - provisioned instantly and built for performance-critical workloads.
Get startedRelated products
Written by
Julius