The simplest form — create a group with the next available GID:
Verify it was created:
What happened:
- A new group developers was created
- The system assigned the next available GID (1002 in this example)
- The
xfield means the password is in/etc/gshadow - The trailing
:means no members yet — add withusermod -aG
groupname:password:GID:members
In environments where GIDs must match across systems (NFS, LDAP sync, shared filesystems), specify the GID explicitly:
Flags used:
-g 5000— Set a specific GID
getent group | sort -t: -k3 -n
before assigning a specific one.
System groups are used by daemons and services, not human users. They get a GID below the normal user range (typically below 1000):
Flags used:
-r— Create a system group (GID from system range)
The system GID range is defined in /etc/login.defs
via SYS_GID_MIN and SYS_GID_MAX
(typically 201–999 on RHEL, 100–999 on Debian/Ubuntu).
useradd -r appuser, also create a matching system group with
groupadd -r appuser to keep uid/gid aligned.
The -f (force) flag exits successfully even if the
group already exists, and forces creation even if the GID is already in use
(by choosing the next available GID instead):
Flags used:
-f— Force/idempotent behavior; exit 0 if group already exists-g 2500— Preferred GID (ignored if taken when used with -f)
-f in provisioning scripts to make
group creation idempotent — the script won't fail on re-runs when the group
already exists.
Groups can have passwords, allowing users not in the group to
temporarily join via newgrp. This is rare in modern practice but
exists in the protocol:
How group passwords work:
- A user not in the group can run
newgrp contractorsand supply the password to temporarily switch their primary group - Group passwords are stored in
/etc/gshadow - Group administrators (set via
gpasswd -A) can add/remove members without needing root
usermod -aG by admins.
groupadd only creates the group — it does not add members. Here is the full workflow from creation to populated group:
Key flags:
usermod -aG groupname username— Add user to supplementary group- The
-a(append) flag is critical — without it,-Greplaces ALL supplementary groups, removing the user from everything else
sudo gpasswd -a alice dbadmins
also adds a user to a group and is slightly more explicit about intent.
Good practice before assigning a specific GID — see what is already in use:
getent group instead of
cat /etc/group — it pulls from all configured sources including
LDAP and NIS, giving you a complete picture of all groups on the system.
Creating multiple groups efficiently — useful for provisioning new servers or setting up a standardized environment:
group module handles this idempotently:
- name: Create group group: name=webteam gid=3000 state=present