Amazon Cyber Monday deals See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

Linux Add User With a Home Directory

To add a Linux user and create a home directory, run sudo useradd -m username, replacing username with the desired login name. Then set the account’s password with sudo passwd username. On many Debian- and Ubuntu-based systems, sudo adduser username is a friendlier interactive alternative that creates the home directory by default.

What the Home Directory Does

A home directory is the user’s personal space for files and configuration. It is normally located at /home/username, though administrators can choose another location. Applications commonly store per-user settings in hidden files and directories there, such as ~/.config. The tilde character (~) refers to the current user’s home directory in a shell.

Creating a login account and creating its home directory are related but distinct operations. The useradd command can create an account without creating a home directory unless instructed to do so. The -m option requests that directory and, on typical Linux installations, populates it with default files from /etc/skel. Those defaults can include shell configuration files, depending on the distribution.

Choose the Right Command

Linux distributions commonly provide both useradd and adduser, but their behavior differs. useradd is a low-level command with options that are useful in scripts and consistent across many Linux systems. adduser is often a higher-level helper script; its prompts and defaults vary by distribution.

  • Use useradd -m when you want an explicit, script-friendly command. Specify the options you need rather than relying on defaults.
  • Use adduser when your distribution provides it and you prefer prompts for a password and optional account details. On Debian-family systems, it generally creates the home directory automatically.
  • Check your system first: run command -v adduser to see whether that command is available. Consult man useradd or man adduser for local behavior.

On systems using useradd, the following creates the account and home directory, then sets a password:

sudo useradd -m alex
sudo passwd alex

Depending on the distribution’s defaults, the account may use a particular login shell or group arrangement. If you need a specific shell or primary group, set it explicitly and verify the result.

Creating an Account Step by Step

  1. Choose a unique login name. Check existing accounts before reusing a name, especially on a shared or production machine.
  2. Create the account and home directory: sudo useradd -m alex. To select a shell explicitly, add an option such as -s /bin/bash, provided that shell exists on the system.
  3. Set a password: run sudo passwd alex and enter the password when prompted. The password is not supplied as a command-line argument, where it could be exposed in shell history or process information.
  4. Verify the account and directory: use getent passwd alex to inspect the account record and ls -ld /home/alex to inspect the directory, adjusting the path if you selected a different home location.
  5. Test access: use su - alex for a local test, then exit the session with exit. The dash requests a login-like environment and should place you in the new user’s home directory.

For an interactive Debian or Ubuntu example, use sudo adduser alex. Follow its prompts to set a password and, if desired, enter optional descriptive information. Avoid running both account-creation commands for the same username: the second command may report that the user already exists.

Home Location, Ownership, and Permissions

The -m option normally creates the home directory at the location specified by the system’s account-creation configuration. To choose a different path, use -d with -m, for example:

sudo useradd -m -d /srv/home/alex alex

Creating a directory is not enough: it should belong to the new user. Standard account-creation tools normally assign ownership appropriately. Check it with ls -ld /srv/home/alex; the owner and group should match the intended account and group policy. If you create or move a directory manually, correct ownership with care, for example sudo chown -R alex:alex /srv/home/alex only when that group and path are correct. A recursive ownership change on the wrong path can damage access to unrelated files.

Home-directory permissions are also worth checking. A directory mode such as 750 allows its owner full access, its group to read and traverse it, and other users no access. The appropriate mode depends on whether the machine is shared and whether group members need access. Do not assume every distribution uses identical defaults.

Groups, Shells, and Administrative Access

A regular account does not automatically need administrator privileges. On systems where administrative access is granted through a group, a distribution may use sudo or wheel. Group names and policy differ, so check the system’s documentation before adding someone. For example, on a system configured to grant sudo access to the sudo group, an administrator might run sudo usermod -aG sudo alex. The -a option matters: omitting it while using -G can replace the user’s supplementary group list instead of appending to it.

Likewise, choose a valid shell based on the user’s purpose. A human who needs an interactive terminal may use /bin/bash or another installed shell. A service account often should not have an interactive login shell; the correct choice depends on the service and the system’s security policy. Creating a home directory does not itself grant sudo rights, enable SSH access, or make an account safe for a particular workload.

Common Problems and Safe Checks

  • No home directory appears: confirm that you used -m with useradd, or check the local adduser behavior. Inspect the account’s recorded home path with getent passwd alex.
  • The command is missing: some systems do not include the same helper command. Use the account-management tool provided by the distribution and consult its manual rather than guessing at options.
  • The name already exists: check getent passwd alex before changing or deleting anything. An existing account may own files or run services.
  • The user cannot write to the home directory: inspect its owner and permissions with ls -ld. Correct only the intended path, and avoid broad recursive changes to system directories.
  • The user cannot use sudo: account creation does not grant administrative access. Check the configured administrator group and sudo policy, then have an authorized administrator make any needed change.

Use sudo only for commands that require administrative privileges, and double-check usernames and paths before running commands that alter accounts or recursively change ownership. On managed servers, follow the organization’s account-provisioning process, which may use centralized identity services rather than local accounts.

Frequently Asked Questions

Does useradd create a home directory by default?
Not necessarily. Behavior can depend on system configuration, so use useradd -m username when you want to explicitly request home-directory creation.

What is the difference between adduser and useradd?
useradd is generally the lower-level account-creation utility. adduser is commonly a more interactive helper, especially on Debian-family distributions. Availability, prompts, and defaults vary.

How do I add a user with a specific home directory?
Use sudo useradd -m -d /chosen/path username. Make sure the path is appropriate and verify the resulting owner and permissions.

How do I create a user without a password?
Account tools can create an account without setting a usable password, but exact locking and authentication behavior depends on the system. For security, follow the intended authentication method, such as centrally managed credentials or SSH keys, and check local policy rather than leaving an account unintentionally accessible.

Can I create the home directory after creating the user?
Yes. Create the intended directory, assign it to the correct user and group, set suitable permissions, and update the account’s home path if necessary. Verify the result before allowing the user to rely on it.

Linux Add User With a Home Directory
Scroll to top