3 UNIX / Linux groupadd Command Examples

What is groupadd?
3 groupadd examples
Syntax and Options
Related Commands

What is groupadd?

groupadd command is used to create group accounts. It updates the /etc/group file accordingly.

3 groupadd Examples

1. Create a new Linux group

The following example creates a new group called apache

$ groupadd apache

Make sure it is created successfully.

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

2. Create new group with a specific groupid

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

If you want to create a group with a specific group id, do the following.

# groupadd apache -g 9090

# grep 9090 /etc/group
apache: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 8888 – 9999 that we specified in the command line.

# groupadd apache -K GID_MIN=8888 -K GID_MAX=9999

# grep apache /etc/group
apache:x:9091:

Syntax and Options

Syntax:

groupadd [options] group
Short Option Long Option Option Description
-f –force If the group already exits, this open will exit with a success status. When used with -g, and the specified GID already exists, another (unique) GID is used.
-g –gid Specify the number value of group id. This is a unique value. The value is non-negative. The default is to use the smallest ID value greater than 999 and greater than every other group. IDs between 0 and 999 are reserved for system accounts.
-h –help Display help message and exit.
-K –key Overrides /etc/login.defs defaults (GID_MIN, GID_MAX and others). Multiple -K options can be specified.
-o –non-unique Allow to add a group with a non-unique GID.
-p –password The encrypted password, as returned by crypt. By default password is disabled. This option is not recommended because the password (or encrypted password) will be visible by users listing the processes. You should make sure the password respects the systems password policy.
-r –system Create a system group. The numeric identifiers of new system groups are chosen in the SYS_GID_MIN-SYS_GID_MAX range, defined in login.defs, instead of GID_MIN-GID_MAX.

Exit values for groupadd command:

  • 0 success
  • 2 invalid command syntax
  • 3 invalid argument to option
  • 4 GID not unique (when -o not used)
  • 9 group name not unique
  • 10 cant update group file


Related Commands

chfn
chsh
passwd
gpasswd
groupdel
groupmod
login.defs
useradd
userdel
usermod