How To

groupadd Command in Linux: Create Groups and Set GID Options

In Linux, groups organize user accounts and define shared access to files and resources. Every group member inherits the read, write, or execute permissions assigned to that group on a given file or directory.

The groupadd command creates new groups. Only root or a user with sudo privileges can run it.

How the groupadd Command Works in Linux

The syntax is:

bashgroupadd [OPTIONS] GROUPNAME

groupadd uses default values from /etc/login.defs and adds entries to two files: /etc/group (group name, GID, and member list) and /etc/gshadow (encrypted group passwords if set).

To create a group:

bashsudo groupadd mygroup

The command produces no output on success. Verify it with:

bashgetent group mygroup
mygroup:x:1001:

The four fields are: name, password placeholder (x means the hash is in /etc/gshadow), GID, and member list. Use getent rather than grepping /etc/group directly — getent queries the Name Service Switch (NSS), which includes LDAP and NIS group databases in addition to local files.

Suppress the “already exists” error with -f. In scripts where the group may already exist:

bashsudo groupadd -f mygroup

This exits successfully whether the group was just created or already existed.

Create a Group with a Specific GID and System Groups

Set a specific GID with -g. By default, groupadd assigns the next available GID from the GID_MIN/GID_MAX range in /etc/login.defs. To set a specific value:

bashsudo groupadd -g 1010 mygroup

If that GID is already taken, groupadd returns an error. To allow a non-unique GID — useful in NFS environments where GIDs must match across machines — combine with -o:

bashsudo groupadd -o -g 1010 mygroup

Create a system group with -r. System groups use a separate GID range (SYS_GID_MIN through SYS_GID_MAX, typically below 1000) to avoid conflicts with user account GIDs:

bashsudo groupadd -r mysystemgroup

System groups are used for services and daemons. The separate range prevents accidental conflicts between service accounts and regular user groups.

Override the GID range with -K. When a specific GID range is required for consistency or NFS policy:

bashsudo groupadd -K GID_MIN=1200 -K GID_MAX=1500 mygroup

Verify, Override Defaults, and Manage Group Membership

After creating a group, add users to it with usermod -aG. The -a flag appends the new group without removing existing memberships:

bashsudo usermod -aG mygroup username

Omitting -a replaces all supplementary groups and can remove a user from other groups unexpectedly.

Check a user’s current memberships:

bashid username

Users must log out and back in for new group memberships to take effect. PAM evaluates group assignments at login. Running sessions do not inherit changes automatically.

Avoid group passwords. The -p option accepts an encrypted hash, not plain text. Group passwords are a security anti-pattern: they require sharing a credential among multiple users, making revocation and auditing difficult. Manage access by adding users to the group with usermod -aG instead.

The groupadd command follows the same syntax on Ubuntu, Debian, Fedora, and RHEL-based systems. Leave a comment below if you run into any issues.

Cyber Defence

Recent Posts

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

2 days ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

2 days ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

3 days ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

6 days ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

6 days ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

6 days ago