Linux Create Group Using Groupadd Command

Linux groups are required for easy management and administration of users. Linux resources (say file or directories) are set with read, write or execute permissions for the associated group which has users who share those privileges.

In this tutorial, we learn how to create a group in Linux using groupadd command.

Groupadd Command

Groupadd command is used to create a group in Linux and its has few options which is explained below in the tutorial with examples.

Syntax:

groupadd [options] groupname

1) Creating a group

The groupadd command can be executed simply by providing a new group name as argument.

The following command will add a group 'developers' to the system and later you can add a user to this group.

# groupadd developers

Group information will be stored in '/etc/groups' file and you can use grep command to search for string.

# grep developers /etc/group
developers:x:506:

2) Set Custom GID for Group

The GID of the added group is decided by the system. But if you want to provide some specific GID, it can be provided with -g or --gid option.

# groupadd -g 3456 developers

# grep developers /etc/group
developers:x:3456:

3) Create a system group

The GIDs allotted to new groups are allocated between GID_MIN and GID_MAX values from login.defs file. Usually, the value of GID_MIN is 500 or 1000 in most systems. The GIDs below GID_MIN are reserved for system groups. If a system group is needed to be created, use -r option.

# groupadd -r developers

# grep developers /etc/group
developers:x:102:

Note that the GID allocated is 102 (less than 500)

4) Force success for existing group

If we try to add some already existing group,

# groupadd javaproject
groupadd: group javaproject exists

# echo $?
9

Exits with error. Yes, that was an obvious one. But if you want to exit the command with success status, when the group exists, use -f or --force option.

# groupadd -f javaproject

# echo $?
0

5) Create Non-unique GID

For allocating a non-unique GID to a group, -o option is used

# groupadd -g 505 developers
groupadd: GID 505 is not unique

# groupadd -o -g 505 developers

# grep 505 /etc/group
bill:x:505:
developers:x:505:

The default values for login are defined in '/etc/login.defs' file. For overriding key-value pairs in this file, -K option is used.

# groupadd -K GID_MIN=700 developers

# grep developers /etc/group
developers:x:700:

If you are interested, the contents of login.defs file is:

# *REQUIRED*
# Directory where mailboxes reside, _or_ name of file, relative to the
# home directory. If you _do_ define both, MAIL_DIR takes precedence.
# QMAIL_DIR is for Qmail
#
#QMAIL_DIR Maildir
MAIL_DIR /var/spool/mail
#MAIL_FILE .mail

# Password aging controls:
#
# PASS_MAX_DAYS Maximum number of days a password may be used.
# PASS_MIN_DAYS Minimum number of days allowed between password changes.
# PASS_MIN_LEN Minimum acceptable password length.
# PASS_WARN_AGE Number of days warning given before a password expires.
#
PASS_MAX_DAYS 99999
PASS_MIN_DAYS 0
PASS_MIN_LEN 5
PASS_WARN_AGE 7

#
# Min/max values for automatic uid selection in useradd
#
UID_MIN 500
UID_MAX 60000

#
# Min/max values for automatic gid selection in groupadd
#
GID_MIN 500
GID_MAX 60000

#
# If defined, this command is run when removing a user.
# It should remove any at/cron/print jobs etc. owned by
# the user to be removed (passed as the first argument).
#
#USERDEL_CMD /usr/sbin/userdel_local

#
# If useradd should create home directories for users by default
# On RH systems, we do. This option is overridden with the -m flag on
# useradd command line.
#
CREATE_HOME yes

# The permission mask is initialized to this value. If not specified,
# the permission mask will be initialized to 022.
UMASK 077

# This enables userdel to remove user groups if no members exist.
#
USERGROUPS_ENAB yes

# Use MD5 or DES to encrypt password? Red Hat use MD5 by default.
MD5_CRYPT_ENAB yes

Conclusion

In this tutorial we have learned how to create a group in Linux and other options of groupadd command. Groupadd command should work on all Linux distributions including Ubuntu, Debian, Centos and Mint.

I hope you enjoyed reading and please leave your suggestions in the below comment section.

Bobbin Zachariah 8:37 am

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.