Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Add Users in Linux via Command Line

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Adding a new user to a Linux machine is done with the useradd or adduser command. The short answer: run sudo adduser username (Debian/Ubuntu) or sudo useradd -m username (RHEL/Fedora) and then set a password with sudo passwd username. This creates a home directory, assigns a user ID, and lets the person log in. You need root or sudo privileges to do it.

What You'll Need

  • A Linux machine with a terminal (local or SSH).
  • An account with sudo privileges (or root access).
  • Basic familiarity with the command line.

Step-by-Step

  1. Open a terminal. If you're on a desktop, press Ctrl+Alt+T. If you're remote, SSH into the machine.
  2. Check your current privileges. Run whoami. If it prints your username, you're a normal user. To add users, you need to use sudo before commands. If you're root (the prompt shows #), you can skip sudo.
  3. Choose the right command for your distribution. On Debian/Ubuntu and their derivatives, use adduser (the friendly wrapper). On RHEL, Fedora, CentOS, and Arch, use useradd (the low-level tool). If you're not sure, try adduser first—it's more interactive.
  4. Create the user. For Debian/Ubuntu, run: sudo adduser newusername (replace newusername with the desired login name). You'll be prompted to set a password and optionally fill in user info (full name, phone, etc.)—you can leave those blank by pressing Enter. For RHEL/Fedora, run: sudo useradd -m newusername The -m flag creates the home directory. Then set a password with sudo passwd newusername.
  5. Verify the user was created. Run id newusername to see the user ID, group ID, and groups. You should see output like uid=1001(newusername) gid=1001(newusername) groups=1001(newusername).
  6. Check the home directory (if you used useradd without -m). If you forgot -m, the home directory won't exist. Create it manually with sudo mkdir /home/newusername and set ownership with sudo chown newusername:newusername /home/newusername. Better to recreate the user with -m if you haven't set a password yet.
  7. Optionally, add the user to supplementary groups. For example, to give them sudo (admin) privileges: sudo usermod -aG sudo newusername (Debian/Ubuntu) or sudo usermod -aG wheel newusername (RHEL/Fedora). To add them to the docker group: sudo usermod -aG docker newusername. Always use -aG (append) to avoid removing the user from other groups.
  8. Test the login. Either switch to the new user with su - newusername or log out and log in with that username. If you're testing via SSH, open a new terminal and try ssh newusername@localhost.

Common Mistakes

  • Forgetting the -m flag with useradd. This results in no home directory, and the user may not be able to log in graphically.
  • Using useradd on Debian/Ubuntu without the full path. On Debian, useradd is a low-level tool that doesn't create a home directory by default. adduser is the recommended way.
  • Not setting a password. If you create a user with useradd and don't run passwd, the account is locked and the user cannot log in.
  • Adding a user to sudo or wheel group accidentally. This grants full admin rights. Only do it if intended.
  • Using usermod -G instead of -aG. The -G flag replaces the user's supplementary groups, which can lock them out of necessary groups (like audio or video).

Tips

  • Use adduser on Debian/Ubuntu—it's interactive and sets up the home directory, shell, and user group automatically.
  • Set a strong password. Use a mix of letters, numbers, and symbols. You can enforce password policies with chage (e.g., sudo chage -M 90 newusername to expire password after 90 days).
  • Give the user a proper shell. By default, useradd may set the shell to /bin/sh or /bin/bash depending on the system. To set it explicitly: sudo usermod -s /bin/bash newusername.
  • Create a SSH key for the user if they'll log in remotely. Add their public key to /home/newusername/.ssh/authorized_keys.
  • Check the user's groups with groups newusername after adding them to verify.

Troubleshooting

If the user cannot log in: Check that the password is set. Run sudo passwd newusername to reset it. Also check that the account isn't locked: sudo passwd -S newusername (the output should show P for password set).

If the home directory is missing: Create it manually: sudo mkdir /home/newusername then sudo chown newusername:newusername /home/newusername. Also copy default skeleton files from /etc/skel if needed: sudo cp -r /etc/skel/. /home/newusername/.

If the user can't use sudo: Verify they are in the correct group. Run groups newusername. If not, add them with sudo usermod -aG sudo newusername (or wheel on RHEL). Then log out and back in.

If you get "useradd: cannot lock /etc/passwd": This means another process is using the user database. Wait a few seconds and try again. If it persists, check for running processes with ps aux | grep passwd and kill them if safe.

If you accidentally created a user with no home directory and no password: Delete the user with sudo userdel -r newusername (the -r removes the home directory if it exists) and recreate properly.

Frequently Asked Questions

What's the difference between adduser and useradd?

adduser is a friendly Perl script that calls useradd and then does extra steps like creating the home directory, setting the shell, and prompting for a password. useradd is the low-level binary that only creates the account entry; you must do everything else manually.

How do I delete a user?

Use sudo userdel -r username to remove the user and their home directory. Be careful—this deletes all files in that home directory. To keep the home directory, omit -r.

Can I add a user without a home directory?

Yes, but it's not recommended for normal users. Use sudo useradd --no-create-home username (or -M). This is useful for system accounts that don't need a home.

How do I add a user to multiple groups at once?

Run sudo usermod -aG group1,group2,group3 username (comma-separated, no spaces). Always include -a to append.

What is the default shell for a new user?

It depends on the system's /etc/default/useradd file. On many distributions, it's /bin/bash. You can check with sudo useradd -D | grep SHELL.

How do I set an expiration date for a user account?

Use sudo chage -E YYYY-MM-DD username to set an expiration date. For example, sudo chage -E 2025-12-31 john makes the account expire on that date.