groupadd Command Cheat Sheet
The groupadd command creates a new group account using the values specified on the command line plus the default values from the system. It modifies /etc/group and /etc/gshadow.
Synopsis
groupadd [options] group
Basic Usage
Create a Regular Group
sudo groupadd developers
Create a System Group (-r)
System groups are used for background processes and daemons (like apache, docker). They usually have GIDs < 1000.
sudo groupadd -r docker
Advanced Options
Specify Custom GID (-g)
Force the group to have a specific numerical ID.
sudo groupadd -g 1500 admins
Handling Conflicts:
If GID 1500 is already taken, groupadd will fail.
To force it (not recommended, creates duplicate GID):
sudo groupadd -o -g 1500 alias_group
Override Defaults (-K)
Override values from /etc/login.defs (like GID_MIN, GID_MAX).
sudo groupadd -K GID_MIN=5000 -K GID_MAX=6000 custom_group
Password Protected Groups (-p)
⚠️ Security Warning: The password provided here must be encrypted. Do not use plaintext.
# Generate hash first (e.g., using openssl)
HASH=$(openssl passwd -1 "mypassword")
sudo groupadd -p "$HASH" secured_group
Files Affected
/etc/group: Stores group info (Name, Password placeholder, GID, Member list)./etc/gshadow: Stores secure group info (Encrypted password, Administrators, Members)./etc/login.defs: Configuration for default GID ranges.
Troubleshooting
"groupadd: group 'xyz' already exists"
The group name is already in use. Check with:
getent group xyz
"groupadd: GID '1000' already exists"
The ID you requested is taken.
1. Check who owns it: getent group 1000
2. Choose another ID or let the system pick one (omit -g).
"cannot lock /etc/group; try again later."
Another process is modifying the user/group database.
1. Check for running locks: ls /etc/*.lock
2. Wait a moment or check running processes (ps aux | grep user).
Automation / Scripting
When writing setup scripts (e.g., Ansible, Bash), checking if a group exists before creating it prevents errors.
Bash Idiom
# Create group only if it doesn't exist
getent group myapp >/dev/null || sudo groupadd -r myapp
Configuration
Defaults are stored in /etc/login.defs. Key variables:
SYS_GID_MIN/SYS_GID_MAX: Range for system groups (e.g., 100-999).GID_MIN/GID_MAX: Range for regular groups (e.g., 1000-60000).
Exit Status
| Code | Meaning |
|---|---|
| 0 | Success |
| 2 | Command syntax error |
| 3 | Argument error option |
| 4 | GID not unique (when -o not used) |
| 9 | Group name not unique |
| 10 | Cannot update group file (permission/lock) |
Examples
Docker Setup
Standard pattern for enabling non-root Docker usage.
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker # Activate group in current shell
Shared Directory Group
Create a group for shared project access.
sudo groupadd project_x
sudo chown :project_x /srv/project
sudo chmod 770 /srv/project
Notes
groupaddis a low-level utility. Some distros provideaddgroup(a friendlier Perl wrapper), butgroupaddis the standard everywhere.- Group names can be up to 32 characters long.