groupadd Command with Examples in Linux

This tutorial explains Linux “groupadd” command, options and its usage with examples.

groupadd – Create a new group

Description :

The groupadd command creates a new group account using the values specified on the command line and the default values from the system. The new group will be entered into the system files as needed.

Usage :

groupadd [options] group

Options :

advertisement
advertisement

-g, –gid GID
The numerical value of the group’s ID. This value must be unique, unless the -o option is used. The value must be non-negative.
-h, –help
Display help message and exit.
-o, –non-unique
This option permits to add a group with a non-unique GID.
-p, –password PASSWORD
The encrypted password, as returned by crypt. The default is to disable the password.
-f, –force
This is the force flag. This will cause groupadd to exit with an error when the group about to be added already exists on the system. If that is the case, the group won’t be altered (or added again).
This option also modifies the way -g option works. When you request a gid that it is not unique and you don’t specify the -o option too, the group creation will fall back to the standard behavior (adding a group as if neither -g or -o options were specified).
-K, –key KEY=VALUE
Overrides /etc/login.defs defaults (GID_MIN, GID_MAX and others). Multiple -K options can be specified. Example: -K GID_MIN=100 -K GID_MAX=499

The following configuration variables in /etc/login.defs change the behavior of this tool:

GID_MAX (number), GID_MIN (number)

Range of group IDs used for the creation of regular groups by useradd, groupadd, or newusers. The default value for GID_MIN (resp. GID_MAX) is 1000 (resp. 60000).

MAX_MEMBERS_PER_GROUP (number)

Note: Join free Sanfoundry classes at Telegram or Youtube

Maximum members per group entry. When the maximum is reached, a new group entry (line) is started in /etc/group (with the same name, same password, and same GID). The default value is 0, meaning that there are no limits in the number of members in a group. This feature (split group) permits to limit the length of lines in the group file. This is useful to make sure that lines for NIS groups are not larger than 1024 characters. If you need to enforce such limit, you can use 25.

Examples :

1. Create a new Linux group

The following command creates a new group called abc

advertisement
# groupadd apache

Make sure it is created successfully.

# grep abc /etc/group
abc:x:1004:

2. Create new group with a specific groupid

If you don’t specify a groupid, Linux will assign one automatically.

advertisement
# groupadd abc -g 9090
# grep 9090 /etc/group
abc:x:9090:

3. Override /etc/login.defs default

When it is assigning the automatic group id, it uses the GID_MIN, and GID_MAX value specified in the /etc/login.defs.

# egrep 'GID_MIN|GID_MAX' /etc/login.defs
GID_MIN			 1000
GID_MAX			60000

If you want to set your own values, you can specify that using -K option as shown below. In the example below, groupadd command created the account with group id 9091, which is between the values 8000 – 9999 that we specified in the command line.

# groupadd abc -K GID_MIN=8000 -K GID_MAX=9999
 
# grep abc /etc/group
abc:x:9091:

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.