groupadd

On this page 7

groupadd is the low-level Linux administration command that creates a new local group on the system, assigns it a group identifier (GID) and writes the entry into /etc/group and /etc/gshadow. In Linux permissions, groups are the middle layer of the classic owner, group and others model, so groupadd is usually the first step when several accounts need shared access to files, devices or administrative privileges.

The command belongs to the shadow-utils package (called passwd or shadow on some distributions) together with useradd, usermod and userdel. It only creates the group definition; it does not add members by default and does not change any file ownership, which is why it is normally combined with usermod -aG, gpasswd and chgrp.

How groupadd creates a group

When groupadd runs, it validates the requested name, picks or verifies a GID and then appends one line to each group database under a lock. It must run with root privileges, typically through sudo, because both target files are writable only by root and /etc/gshadow is not world-readable.

The sequence is short but strict. groupadd first reads /etc/login.defs for the GID ranges, then takes the lock files /etc/group.lock and /etc/gshadow.lock, writes temporary copies and renames them into place. This atomic replace pattern prevents a half-written group file if the system loses power or two administrators run account tools at the same moment.

The table below shows what each file receives after a command such as groupadd -g 2001 developers.

FileRoleExample content
/etc/groupPublic group databasedevelopers:x:2001:
/etc/gshadowGroup passwords and administratorsdevelopers:!::
/etc/login.defsRead only: GID ranges and defaultsGID_MIN 1000
Lock filesPrevent concurrent edits/etc/group.lock
Files changed or read by groupadd

The x in the password field of /etc/group means the real value lives in /etc/gshadow, where ! marks a locked group password. Group passwords are rarely used today; access is granted through membership lists rather than through the newgrp password prompt.

Key groupadd options and GID ranges

Most invocations need only a name, but a handful of options control how the GID is chosen and where the group is written. The following options are available in current shadow-utils releases.

OptionEffectTypical use
-g GIDUses a specific numeric GIDMatching GIDs across servers
-rCreates a system group from the system rangeService accounts and daemons
-fExits successfully if the group already existsIdempotent scripts
-oAllows a duplicate GID with -gRare legacy aliasing
-K KEY=VALUEOverrides a login.defs value onceCustom GID range for one call
-R CHROOT_DIRApplies changes inside a chrootBuilding images or rescue work
-U usersAdds listed users as membersRecent shadow-utils versions only
Common groupadd options

Without -g, groupadd allocates the lowest free GID above the highest one in use within the configured range. Regular groups come from GID_MIN to GID_MAX, while -r draws from the system range below 1000, usually allocated downward from SYS_GID_MAX.

RangeValuesPurpose
Root group0Reserved for root
System groups101-999Daemons, devices, packaged services
Regular groups1000-60000People, teams and projects
Special values65534nogroup or nobody, overflow GID
Default GID ranges on most modern distributions

Exact limits are distribution-specific and are defined in /etc/login.defs; older Red Hat releases started regular GIDs at 500. Group names should start with a lowercase letter or underscore, contain only lowercase letters, digits, hyphens and underscores, and stay within 32 characters for compatibility.

groupadd covers only group creation; neighboring commands handle membership, renaming, deletion and ownership. Knowing the boundary avoids the common belief that creating a group automatically grants anyone access to anything.

CommandWhat it doesRelation to groupadd
groupaddCreates a group and GIDStarting point
groupmodRenames a group or changes its GIDModifies what groupadd created
groupdelRemoves a groupReverses groupadd
usermod -aGAppends a user to supplementary groupsAdds members afterward
gpasswdManages members and group administratorsAlternative membership tool
chgrpChanges the group owner of filesApplies the group to resources
addgroupDebian and Ubuntu interactive wrapperCalls groupadd with policy defaults
Where groupadd fits among group-related commands

On Debian-based systems, addgroup reads /etc/adduser.conf and enforces naming policy, which makes it friendlier for interactive use. In scripts and configuration management, groupadd is preferred because its behavior and exit codes are consistent across distributions.

Using groupadd in practice

A typical shared-directory setup uses groupadd together with membership and permission commands. The sequence below creates a team group, adds members, assigns a directory and sets the setgid bit so new files inherit the group.

  1. Create the group: sudo groupadd -g 2001 developers.
  2. Add members: sudo usermod -aG developers alice for each user.
  3. Assign the directory: sudo chgrp developers /srv/project.
  4. Set permissions: sudo chmod 2775 /srv/project for group write and setgid inheritance.
  5. Ask members to log in again so the new membership takes effect.

Membership is read at login time and stored in the process credentials, so running sessions do not see a new group. Users can start a new login shell, run newgrp developers for one shell, or verify the result with id and getent group developers.

In automation, groupadd -f or a prior getent group check keeps a play idempotent. When the same group exists on many servers sharing NFS storage, always pass an explicit -g, because mismatched GIDs make files appear to belong to the wrong group on other hosts.

Limitations and common mistakes

groupadd edits only the local files database. Groups defined in LDAP, FreeIPA or Active Directory through SSSD are invisible to it, and creating a local group with the same name or GID as a directory group causes confusing name resolution.

The exit status is useful for diagnosing failures in scripts. The most frequent errors and their fixes are summarized below.

SymptomLikely causeFix
Exit code 9Group name already existsUse -f or choose another name
Exit code 4Requested GID already in usePick a free GID or add -o deliberately
Exit code 3Invalid name or argumentUse lowercase letters, digits, hyphens
Exit code 10Cannot lock or update group fileRemove stale locks, check disk and permissions
Permission deniedNot running as rootRun the command with sudo
Frequent groupadd errors
  • Using usermod -G without -a replaces all supplementary groups instead of adding one.
  • Editing /etc/group by hand skips locking and gshadow; prefer vigr if manual edits are unavoidable.
  • A user belongs to at most 65,536 supplementary groups in recent kernels, but NFS with AUTH_SYS passes only 16.

The simple explanation

A Linux computer controls who may open which files. Besides individual people, it also knows about named teams called groups. groupadd is the command an administrator types to create a new team and give it a number the system uses internally.

Creating the team is like printing a new badge type for an office. Nobody carries the badge yet, and no door accepts it yet. The administrator still has to hand badges to people and tell each door which badge opens it. Once that is done, everyone on the team can share the same folders without sharing passwords.

How it is used in practice

A small web agency runs a Linux server where four developers maintain client websites under /var/www. Previously every file belonged to a single shared account, so nobody could tell who changed what. The administrator creates a dedicated group with sudo groupadd -g 3100 webdev, adds each developer with usermod -aG webdev, and assigns the web root with chgrp -R webdev /var/www.

She then sets the directories to mode 2775 so that new files inherit the webdev group and remain editable by the whole team. Because a second server mounts the same content over NFS, she creates the group there with the same GID of 3100. Each developer now signs in with a personal account, the logs show individual accountability, and access is revoked simply by removing a person from the group.