How To

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.

Cyber Defence

Recent Posts

touch Command in Linux: Create Files and Update Timestamps

The touch command in Linux does two things: it creates new empty files, and it updates the…

37 minutes ago

rsync Command in Linux: Sync Files, Mirror, and Transfer Remotely

The rsync command in Linux synchronizes files and directories between two locations, locally or over SSH. Unlike scp,…

1 day ago

patch Command in Linux: Apply Diff Files and Reverse Changes

The patch command in Linux applies a set of changes from a diff file to one or…

1 day ago

basename Command in Linux: Strip Directory Paths and Suffixes

The basename command in Linux extracts the last component of a file path, removing the leading directory…

1 day ago

timeout Command in Linux: Kill Long-Running Commands Safely

The timeout command in Linux runs a command with a time limit and terminates it when the limit is reached. It is part of GNU coreutils, available on virtually every Linux distribution. It is most useful for commands with no built-in timeout option, such as ping, curl, tcpdump, or a custom script that might hang indefinitely. How the timeout Command Works in Linux The syntax is: bashtimeout [OPTIONS] DURATION COMMAND [ARG]...…

1 day ago

rmmod Command in Linux: Remove Kernel Modules and Blacklisting

The rmmod command in Linux removes a loaded module from the running kernel. Because the kernel has…

2 days ago