Author: trafficenablers@stanventures.com

  • How to Create Users and Groups in Linux Easily

    How to Create Users and Groups in Linux Easily

    Managing accounts is one of the first administrative skills I learned when working with Linux servers. Whether I am preparing a development environment, granting a colleague access, or separating permissions between teams, users and groups help me control who can access files, applications, and system resources.

    In this guide, I will explain How to Create Users and Groups in Linux using practical terminal commands. I will also cover primary and supplementary groups, password creation, home directories, account verification, administrative privileges, shared folders, deletion, and common errors.

    What Are Linux Users and Groups?

    A Linux user is an account that can own files, run programs, and access system resources. Each user receives a unique numerical user ID, commonly called a UID.

    A group is a collection of users who share certain permissions. Groups make administration easier because I can grant access to several users at once instead of changing permissions for every account individually.

    Linux normally stores account information in these files:

    • /etc/passwd contains basic user details.
    • /etc/group contains group names and memberships.
    • /etc/gshadow stores protected group information.

    These files should not normally be edited manually. Linux provides commands that update them safely.

    Primary and Supplementary Groups

    Every Linux user has one primary group. Files created by that user are usually assigned to this group automatically.

    A user can also belong to several supplementary groups. These additional memberships provide access to shared folders, administrative commands, applications, hardware, or services.

    The -g option assigns a primary group, while -G assigns supplementary groups.

    Check Existing Users and Groups First

    Check Existing Users and Groups First

    Before creating an account, I check whether the username or group already exists.

    getent passwd alex

    getent group developers

    If the commands return no output, the names are probably available.

    I can also inspect all local account and group records with:

    cat /etc/passwd

    cat /etc/group

    Using getent is generally safer because it can display accounts from local files and connected identity services.

    Create a New Group in Linux

    To create a group named developers, run:

    sudo groupadd developers

    Verify that the group was created:

    getent group developers

    The output should display the group name and its assigned group ID.

    Create a Group With a Specific GID

    Linux normally assigns the next available group ID automatically. When matching permissions across several systems, I may need a specific GID.

    sudo groupadd -g 2500 developers

    The selected number must not already belong to another group. Check it before proceeding:

    getent group 2500

    Create a New Linux User

    Create a New Linux User

    The useradd command creates a user account. I include -m so Linux also creates a home directory.

    sudo useradd -m alex

    Set a secure password:

    sudo passwd alex

    The terminal will ask for the new password twice. Password characters will not appear while typing, which is normal.

    Choose a Login Shell

    To create the account with Bash as its login shell, run:

    sudo useradd -m -s /bin/bash alex

    The -s option defines the program that starts when the user opens a terminal session.

    For a service account that should not allow interactive login, use:

    sudo useradd -r -s /usr/sbin/nologin appservice

    The -r option creates a system account.

    Create a User With a Primary Group

    To create a user and assign developers as the primary group, run:

    sudo useradd -m -s /bin/bash -g developers alex

    sudo passwd alex

    The group must exist before running this command.

    Verify the result:

    id alex

    The output displays the user’s UID, primary GID, and supplementary memberships.

    Add an Existing User to a Group

    Add an Existing User to a Group

    To add an existing user to a supplementary group, use:

    sudo usermod -aG developers alex

    The -a option means append, while -G specifies supplementary groups.

    Never omit -a unless you intentionally want to replace the user’s existing supplementary memberships. Running usermod -G alone can remove access to other groups.

    Verify the updated membership:

    groups alex

    You can also use:

    id alex

    Add a User to Multiple Groups

    Separate several group names with commas and no spaces:

    sudo usermod -aG developers,docker,projectteam alex

    Each listed group must already exist.

    Activate the New Membership

    Group changes may not affect an existing login session immediately. The user should log out and sign in again.

    For temporary access in the current terminal, run:

    newgrp developers

    A new shell will start with the selected group active.

    Create a User on Ubuntu and Debian

    Ubuntu and Debian provide the friendlier adduser utility:

    sudo adduser alex

    This interactive command creates the home directory, selects common defaults, asks for a password, and optionally collects account details.

    Create a group with:

    sudo addgroup developers

    Add the user to it:

    sudo adduser alex developers

    Both adduser and useradd can create accounts, but adduser guides beginners through the process while useradd provides direct control through options.

    Grant Administrative Privileges

    Grant Administrative Privileges

    Administrative access should only be given when necessary.

    On Ubuntu and Debian, add the user to the sudo group:

    sudo usermod -aG sudo alex

    On Fedora, Rocky Linux, AlmaLinux, and similar systems, use the wheel group:

    sudo usermod -aG wheel alex

    After signing in again, test administrative access:

    sudo whoami

    A successful command should return root.

    Use a Group for a Shared Directory

    Groups become especially useful when several people need to work in the same directory.

    Create the shared folder:

    sudo mkdir -p /srv/project

    Assign its group ownership:

    sudo chown :developers /srv/project

    Set collaborative permissions:

    sudo chmod 2775 /srv/project

    The leading 2 enables the setgid permission. New files and subdirectories created inside the folder inherit the developers group, making collaboration more consistent.

    Change a User’s Primary Group

    To change the primary group of an existing account, run:

    sudo usermod -g developers alex

    Confirm the change:

    id alex

    Changing the primary group does not automatically update ownership of older files. Review and adjust existing files when required.

    Delete Users and Groups Safely

    Delete Users and Groups Safely

    Delete a user while keeping the home directory:

    sudo userdel alex

    Delete the account and its home directory:

    sudo userdel -r alex

    Before using -r, back up any important files.

    Delete an unused group with:

    sudo groupdel developers

    Linux will refuse to remove a group if it is still configured as a user’s primary group.

    Troubleshoot Common Problems

    The User Already Exists

    If Linux reports that the user already exists, verify the account:

    getent passwd alex

    Choose another username or modify the existing account.

    The Group Does Not Exist

    Create the group before assigning it:

    sudo groupadd developers

    Then repeat the useradd or usermod command.

    Permission Is Denied

    User and group management requires root privileges. Add sudo before the command or sign in through an authorized administrative account.

    Group Access Is Still Not Working

    Ask the user to log out and sign in again. Also verify the folder’s ownership and permissions:

    ls -ld /srv/project

    id alex

    Group membership alone does not grant access when directory permissions block the group.

    Frequently Asked Questions

    1. How to Create Users and Groups in Linux at the Same Time?

    Create the group first with groupadd, then create the user with useradd -m -g groupname username, and finally set the password using passwd.

    2. How Do I See Every Group a User Belongs To?

    Run groups username for a simple list or id username for UID, GID, and complete membership details.

    3. What Is the Difference Between -g and -G?

    The lowercase -g sets one primary group, while uppercase -G assigns one or more supplementary groups.

    4. Does useradd Automatically Create a Home Directory?

    Not on every system. Use the -m option to ensure the account receives a home directory.

    Wrapping Up Your Linux Account Setup

    When I manage Linux accounts, I follow a repeatable process: check existing records, create the required group, create the user with a home directory, assign the correct memberships, set a secure password, and verify every change.

    Learning How to Create Users and Groups in Linux also makes file permissions, shared directories, application access, and server security easier to understand. By using groupadd, useradd, usermod, passwd, id, and getent carefully, I can maintain organized accounts without manually editing sensitive system files.