How to use groupadd command on Ubuntu Linux with examples

The groupadd command is used by system administrators to add/create groups on Linux systems

Ubuntu is a great place to start for new users and students learning how to use and manage Linux systems. It’s a great platform for beginners to start learning Linux.

Ubuntu is an open-source Linux operating system that runs on desktops, laptops, servers, and other devices.

About groupadd command:

The groupadd command is used by system administrators to add/create groups on Linux systems. It allows superusers to perform basic group management on Ubuntu and other Linux platforms.

Like using your mouse and keyboard to add groups or manage settings in the GUI. the groupadd is the way to do it on the command line.

Syntax:

The syntax is the rule and format of how the groupadd command can be used. the syntax’s options can be reordered. but a straight format must be followed.,.

Below is an example syntax of how to use the groupadd command.

groupadd [options] GROUP

Options:

The command line options are switches or flags that determined how the commands are executed or controlled. they modify the behavior of the command. they are separated by spaces and followed after the commands options.

Below are some options for the groupadd command:

    GROUPReplace GROUP with the name of the group you want to change its settings. If the group doesn’t already exist, the command will fail and not execute.
-f, –force
Use the -f or –force option to exit with a success message if the group already exists. When used with -g option and the group exist, a new GID is created
-g, –gid GIDUse the -g or –gid GID for the new group you’re creating.
-o, –non-uniqueUse the -o or –non-unique option to allow to use a duplicate (non-unique) GID
-p, –password PASSWORDUse the -P or –password PASSWORD option to change the password to this (encrypted) PASSWORD
-R, –root CHROOT_DIRUse the -R or –root CHROOT_DIR option sets directory to chroot into
-h, –helpdisplay this help message and exit

Examples:

Below are some examples of how to run and use the groupadd on Ubuntu Linux.

If you want to create a new group named members, you run the commands below.

groupadd members

If you wish to create a members group with a GID of 8080, you run the commands below. by default, if you don’t specify a GID for a group, Linux automatically assigns one.

groupadd -g 8080 members

If you’re not logged in as a root account, you may have to use the sudo command it it.

sudo groupadd -g 8080 members

When it is assigning the automatic group id, it uses the GID_MIN, and GID_MAX values specified in the /etc/login.defs config file. f you want to set your values, you can specify that using the -K option as shown below

sudo groupadd -K GID_MIN=8080 -K GID_MAX=8090 members

In the example above, the groupadd command created the account with group id 8081, which is between the values 8000 – 9999 that we specified in the command line.

When you run groupadd with the –help option, you’ll see the help text below:

Usage: groupadd [options] GROUP

Options:
  -f, --force                   exit successfully if the group already exists,
                                and cancel -g if the GID is already used
  -g, --gid GID                 use GID for the new group
  -h, --help                    display this help message and exit
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -o, --non-unique              allow to create groups with duplicate
                                (non-unique) GID
  -p, --password PASSWORD       use this encrypted password for the new group
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
      --extrausers              Use the extra users database

That’s it!

Hope you like it and please come back soon for more Ubuntu Linux commands!