Smart Systems for Home

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

Linux Command Line: Create Users and Groups

· 6 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

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 sudo or as root.
  • Basic familiarity with the terminal (typing commands and reading output).

Step-by-Step

  1. Open a terminal. On most systems, you can press Ctrl+Alt+T or find “Terminal” in the applications menu.
  2. Check current users and groups (optional). Run cat /etc/passwd to see existing users, and cat /etc/group for groups. This helps you avoid name conflicts.
  3. Create a new group. Use the groupadd command. For example, to create a group named developers:
    sudo groupadd developers
    To verify, run grep developers /etc/group. You should see a line like developers:x:1001:.
  4. Create a new user. Use useradd with the -m flag to create a home directory. For example, to create a user named john:
    sudo useradd -m john
    The -m flag creates /home/john. Without it, no home directory is made (which is fine for system accounts but not for regular users).
  5. Set a password for the new user. Use the passwd command:
    sudo passwd john
    You'll be prompted to enter and confirm the password. This is mandatory — a user without a password cannot log in.
  6. Add the user to a group. Use the usermod command with the -aG flags (append to group). For example, to add john to the developers group:
    sudo usermod -aG developers john
    The -a is crucial — it appends, while -G specifies the group. Without -a, you would remove the user from all other groups.
  7. Verify the user's groups. Run groups john or id john. You should see developers listed along with the user's primary group (usually the same name as the username).
  8. 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 -g flag:
    sudo useradd -m -g developers john
    This sets developers as the primary group. Note that the user will not be in a group named john in this case.
  9. Make the user a sudoer (optional). If the user needs administrative privileges, add them to the sudo group (on Debian/Ubuntu) or wheel (on RHEL/CentOS/Fedora):
    sudo usermod -aG sudo john   # Debian/Ubuntu
    sudo usermod -aG wheel john   # RHEL/CentOS/Fedora
    Be cautious — only grant sudo to trusted users.
  10. 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 -m flag when creating a normal user — they end up with no home directory, which can cause login issues.
  • Using usermod -G without -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 useradd instead of adduser on Debian/Ubuntu — adduser is a friendlier front-end that asks for a password and creates the home directory automatically. Both work, but adduser is often easier for beginners.

Tips

  • Use adduser instead of useradd on Debian/Ubuntu systems — it's interactive and sets up the home directory and password in one go.
  • Always verify your changes with id username or groups username.
  • If you're creating many users, consider writing a small shell script to loop through a list of names.
  • Use the -c flag with useradd to add a comment (like a full name): sudo useradd -m -c "John Doe" john.
  • For system accounts (services like nginx or mysql), use useradd -r to 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 useradd without -m, the home directory might be missing. Create it with sudo mkdir /home/john and set ownership with sudo 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 developers in their session to switch groups temporarily.
  • “usermod: group 'sudo' does not exist” — On some distributions, the admin group is named wheel instead. Check with grep sudo /etc/group or grep wheel /etc/group.
  • Permission denied when running sudo — Make sure you are in the sudoers group. Run sudo visudo to 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.