Linux Command Line: Create Users and Groups
In this article
Yes, you can create users and groups directly from the Linux command line using the useradd and groupadd commands. This guide walks you through the essential steps, from adding a new group to creating a user and assigning it to that group, plus common mistakes and troubleshooting.
What You'll Need
- A Linux system with a terminal (any distribution works; commands are standard).
- Root or sudo access. You'll need to run most commands with
sudoor as root. - Basic familiarity with the terminal (typing commands and reading output).
Step-by-Step
- Open a terminal. On most systems, you can press Ctrl+Alt+T or find “Terminal” in the applications menu.
- Check current users and groups (optional). Run
cat /etc/passwdto see existing users, andcat /etc/groupfor groups. This helps you avoid name conflicts. - Create a new group. Use the
groupaddcommand. For example, to create a group nameddevelopers:
To verify, runsudo groupadd developersgrep developers /etc/group. You should see a line likedevelopers:x:1001:. - Create a new user. Use
useraddwith the-mflag to create a home directory. For example, to create a user namedjohn:
Thesudo useradd -m john-mflag creates /home/john. Without it, no home directory is made (which is fine for system accounts but not for regular users). - Set a password for the new user. Use the
passwdcommand:
You'll be prompted to enter and confirm the password. This is mandatory — a user without a password cannot log in.sudo passwd john - Add the user to a group. Use the
usermodcommand with the-aGflags (append to group). For example, to addjohnto thedevelopersgroup:
Thesudo usermod -aG developers john-ais crucial — it appends, while-Gspecifies the group. Without-a, you would remove the user from all other groups. - Verify the user's groups. Run
groups johnorid john. You should seedeveloperslisted along with the user's primary group (usually the same name as the username). - Create a user with a specific primary group (optional). If you want the user's primary group to be a custom group instead of a new group with the same name, use the
-gflag:
This setssudo useradd -m -g developers johndevelopersas the primary group. Note that the user will not be in a group namedjohnin this case. - Make the user a sudoer (optional). If the user needs administrative privileges, add them to the
sudogroup (on Debian/Ubuntu) orwheel(on RHEL/CentOS/Fedora):
Be cautious — only grant sudo to trusted users.sudo usermod -aG sudo john # Debian/Ubuntu sudo usermod -aG wheel john # RHEL/CentOS/Fedora - Delete a user or group (if needed). To remove a user while keeping their home directory:
sudo userdel john. To delete the home directory too:sudo userdel -r john. To delete a group:sudo groupdel developers. Note: you cannot delete a group that is a user's primary group unless you change or remove that user first.
Common Mistakes
- Forgetting the
-mflag when creating a normal user — they end up with no home directory, which can cause login issues. - Using
usermod -Gwithout-a— this removes the user from all other groups, which can lock them out of necessary permissions. - Not setting a password — the user cannot log in until you run
passwd. - Creating a user with the same name as an existing group — the system may get confused. Always check for conflicts first.
- Using
useraddinstead ofadduseron Debian/Ubuntu —adduseris a friendlier front-end that asks for a password and creates the home directory automatically. Both work, butadduseris often easier for beginners.
Tips
- Use
adduserinstead ofuseraddon Debian/Ubuntu systems — it's interactive and sets up the home directory and password in one go. - Always verify your changes with
id usernameorgroups username. - If you're creating many users, consider writing a small shell script to loop through a list of names.
- Use the
-cflag withuseraddto add a comment (like a full name):sudo useradd -m -c "John Doe" john. - For system accounts (services like nginx or mysql), use
useradd -rto create a system user without a home directory or login shell.
Troubleshooting
- “useradd: user 'john' already exists” — The username is taken. Choose a different name or remove the existing user if you're sure it's safe.
- “groupadd: group 'developers' already exists” — Use a different group name or add the user to the existing group instead.
- User cannot log in — Check if a password was set. Also, if you used
useraddwithout-m, the home directory might be missing. Create it withsudo mkdir /home/johnand set ownership withsudo chown john:john /home/john. - User is not in the group after adding — The user must log out and log back in for group changes to take effect. Alternatively, run
newgrp developersin their session to switch groups temporarily. - “usermod: group 'sudo' does not exist” — On some distributions, the admin group is named
wheelinstead. Check withgrep sudo /etc/grouporgrep wheel /etc/group. - Permission denied when running sudo — Make sure you are in the sudoers group. Run
sudo visudoto edit the sudoers file, but be careful — a syntax error can break sudo. If you're locked out, you may need to boot into recovery mode.
Frequently Asked Questions
What's the difference between useradd and adduser?
useradd is the low-level binary that directly modifies system files. adduser is a Perl script that calls useradd and then asks for a password, creates the home directory, and copies default files. On Debian/Ubuntu, adduser is more user-friendly; on RHEL-based systems, adduser is just a symlink to useradd.
How do I add a user to multiple groups at once?
Use the -G flag with a comma-separated list, and always keep -a to append: sudo usermod -aG group1,group2,group3 john.
Can I create a user without a home directory?
Yes, just omit the -m flag. This is common for system accounts that don't need interactive login. For example, sudo useradd -r -s /usr/sbin/nologin myservice.
How do I change a user's primary group?
Use the -g flag with usermod: sudo usermod -g developers john. This changes the primary group, but be aware that files the user owns will now show the new group.
What does the -r flag do in useradd?
The -r flag creates a system account. System users get a UID below 1000 (or 500 on some systems) and typically have no home directory or login shell. They're meant for services, not human users.
How do I list all users or groups?
To list users, run cut -d: -f1 /etc/passwd. To list groups, run cut -d: -f1 /etc/group. Or use getent passwd and getent group for a more comprehensive view.