Cybersecurity Updates & Tools

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.