How to Add Users in Linux via Command Line
In this article
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
- Open a terminal. If you're on a desktop, press Ctrl+Alt+T. If you're remote, SSH into the machine.
- Check your current privileges. Run
whoami. If it prints your username, you're a normal user. To add users, you need to usesudobefore commands. If you're root (the prompt shows#), you can skipsudo. - Choose the right command for your distribution. On Debian/Ubuntu and their derivatives, use
adduser(the friendly wrapper). On RHEL, Fedora, CentOS, and Arch, useuseradd(the low-level tool). If you're not sure, tryadduserfirst—it's more interactive. - Create the user. For Debian/Ubuntu, run:
sudo adduser newusername(replacenewusernamewith 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 newusernameThe-mflag creates the home directory. Then set a password withsudo passwd newusername. - Verify the user was created. Run
id newusernameto see the user ID, group ID, and groups. You should see output likeuid=1001(newusername) gid=1001(newusername) groups=1001(newusername). - Check the home directory (if you used
useraddwithout-m). If you forgot-m, the home directory won't exist. Create it manually withsudo mkdir /home/newusernameand set ownership withsudo chown newusername:newusername /home/newusername. Better to recreate the user with-mif you haven't set a password yet. - Optionally, add the user to supplementary groups. For example, to give them sudo (admin) privileges:
sudo usermod -aG sudo newusername(Debian/Ubuntu) orsudo usermod -aG wheel newusername(RHEL/Fedora). To add them to thedockergroup:sudo usermod -aG docker newusername. Always use-aG(append) to avoid removing the user from other groups. - Test the login. Either switch to the new user with
su - newusernameor log out and log in with that username. If you're testing via SSH, open a new terminal and tryssh newusername@localhost.
Common Mistakes
- Forgetting the
-mflag withuseradd. This results in no home directory, and the user may not be able to log in graphically. - Using
useraddon Debian/Ubuntu without the full path. On Debian,useraddis a low-level tool that doesn't create a home directory by default.adduseris the recommended way. - Not setting a password. If you create a user with
useraddand don't runpasswd, the account is locked and the user cannot log in. - Adding a user to
sudoorwheelgroup accidentally. This grants full admin rights. Only do it if intended. - Using
usermod -Ginstead of-aG. The-Gflag replaces the user's supplementary groups, which can lock them out of necessary groups (likeaudioorvideo).
Tips
- Use
adduseron 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 newusernameto expire password after 90 days). - Give the user a proper shell. By default,
useraddmay set the shell to/bin/shor/bin/bashdepending 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 newusernameafter 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.