groupadd (create a new group)
The groupadd command creates a new group account on the system. Groups are a fundamental part of the Linux permission model, allowing you to manage access for multiple users simultaneously. Creating logical groups for departments or projects is a key administrative task.
Example 1: Create a new group
# Create a new group named 'developers'
sudo groupadd developers
Explanation
This command creates a new group named developers. By default, it will be assigned the next available Group ID (GID) above the system-defined maximum for standard groups.
Example 2: Create a group with a specific GID
# Create a group 'testers' with a specific Group ID (GID) of 1050
sudo groupadd -g 1050 testers
Explanation
The -g option allows you to specify a unique GID for the new group. This is essential in environments where GIDs must be consistent across multiple servers for shared storage like NFS.
Example 3: Create a system group
# Create a new system group named 'nginx'
sudo groupadd -r nginx
Explanation
The -r flag creates a system group. These groups are intended for services and daemons and are typically assigned a GID from a lower range (e.g., below 1000) to distinguish them from user groups.
Example 4: Forcing the creation of a group with a non-unique GID
# Create a group 'tempstaff' with a GID that might already exist
sudo groupadd -o -g 1001 tempstaff
Explanation
The -o (non-unique) option allows you to create a group with a GID that is already in use by another group. This is generally not recommended as it can complicate permissions, but it may be required in some specific configurations.
Example 5: Creating a group and adding a user in one step (using usermod)
# Create a new group 'projectx'
sudo groupadd projectx
# Add user 'jdoe' to the newly created group
sudo usermod -aG projectx jdoe
Explanation
While groupadd itself doesn't add users, it's commonly used in conjunction with usermod. This two-step process first creates the group and then adds an existing user to it.
Example 6: Verifying group creation
# Create a group named 'auditors'
sudo groupadd auditors
# Verify the group was created by checking the /etc/group file
grep auditors /etc/group
Explanation
After creating a group, you can verify its existence by using grep to search for the group name in the /etc/group file. This file contains information about all local groups on the system.
Example 7: Creating a group for a specific application
# Create a system group for the 'tomcat' application server
sudo groupadd -r tomcat
Explanation
Creating a dedicated system group for an application like Tomcat is a good security practice. You can then ensure that all files and processes related to Tomcat are owned by this group, limiting access from other users.
Example 8: Using a password with a group (less common)
# Create a group 'secrets' and assign it a password
sudo groupadd -p '!!' secrets
Explanation
The -p option sets a password for the group, though this is rarely used in modern Linux systems. The newgrp command would then be used by a user to temporarily change their primary group to secrets, requiring the password. Using sudo rules is now the preferred method for privilege management.
UNIX Variants
Solaris: The command is the same: groupadd.
FreeBSD: The command is pw groupadd.