
groupadd Command in Linux
groupadd is a Linux command that allows you to create new user groups on the system. Groups are an essential part of the system because they help manage permission and access for multiple users more efficiently.
When you create a group, you can assign users to it. Once its done, you can then share the same permission to all users in the group.
Table of Contents
Here is a comprehensive guide to the options available with the groupadd command in linux −
Syntax of groupadd Command
The general syntax to use the groupadd command on Linux is provided below −
groupadd [options] group_name
Where,
- group_name − This is the name of the group you want to create.
- [options] − These are optional flags you can use to modify the commands behavior.
groupadd Command Options
The following are few options that can be used with the groupadd command in order to change its behavior −
Option | Description |
---|---|
-f, --force | Forces the command to exit with a success status even if the group already exists. Changes the group name if the GID is already in use. |
-g, --gid GID | Specifies a group ID (GID) for the new group. If not used, the system assigns a GID automatically. |
-h, --help | Display help information related to the command. |
-K, --key KEY=VALUE | Overrides the defaults in /etc/login.defs. Allows setting configuration options with key=value pairs. |
-o, --non-unique | Allows creating a group with a non-unique GID, meaning multiple groups can share the same GID. |
-p, --password PASSWORD | Specifies the password for the group. |
-P, --prefix PREFIX_DI | Designates the directory to serve as the root directory for the group. |
-r, --system | Creates a system group. |
-R, --root CHROOT_DIR | Specifies the directory to use as the root directory for the group. |
-U, --users USERS | Forms a group named after the user and includes the user in that group. |
Examples of groupadd Command in Linux
Lets explore some basic examples of groupadd command in Linux system −
- Creating a Basic Group
- Specifying a Group ID (GID)
- Creating a System Group
- Creating a Group with a Non-Unique GID
- Using a Specific Root Directory
- Using an Encrypted Password for a New Group
- Creating a Group with a Specific User
- Creating a Group with a Specific Configuration Key
Creating a Basic Group
One of the advantages of groupadd command on Linux is to create a basic group that helps organize users who need similar permissions. For that purpose, you can use the command with the group name you want to create. Heres an example that create a group named developers in Linux −
sudo groupadd developers

To confirm the group is successfully created, you can use the following command by replacing the group name with the group you have created earlier.
grep 'group_name' /etc/group

Specifying a Group ID (GID)
By default, the groupadd command sets the group ID by itself for your group. However, you can also specify a group ID by yourself by using the -g option with the command.
For example, if you want to create a group named testers with a GID of 1007, you can use the following command −
sudo groupadd -g 1007 testers

Creating a System Group
System groups are typically used for system processes and have Group IDs (GIDs) lower than 1000. You can create a system group by using the -r option with the groupadd command. Heres an example that creates a system group named sysadmins on Linux −
sudo groupadd -r sysadmins

Creating a Group with a Non-Unique GID
In some cases, you might need to create a group with a group ID that is already in use by another group. This can be done using the -o or --non-unique option with the groupadd command. Heres an example that creates a group named devops with a non-unique GID of 1009 −
sudo groupadd -o -g 1001 devops

Using a Specific Root Directory
In a chroot environment, you might need to create groups within a specific directory structure rather than the default system directories. This can be done using the -P or --prefix option with the groupadd command. Heres an example that creates a group named experts within the /mnt/chroot directory −
sudo groupadd -P /mnt/chroot experts
Using an Encrypted Password for a New Group
In case you want to set an encrypted password for a new group, you can use the -p or --password option with the groupadd command.
Heres an example that creates a group named securegroup with an encrypted password −
sudo groupadd -p encrypted_password securegroup

Creating a Group with a Specific User
You can create a group and add a specific user to it by using the -U or --user option. Heres an example that creates a group named linux_team and adds the user jonny to it −
sudo groupadd -U jonny linux_team

Creating a Group with a Specific Configuration Key
You can also override the defaults in /etc/login.defs by using the -K or --key option. Heres an example that creates a group named configgroup with a specific configuration key −
sudo groupadd -K GID_MIN=2000 configgroup

Thats how you can use the groupadd command in Linux systems.
Conclusion
The groupadd is a versatile Linux command used for creating new user groups for managing permissions and access for multiple users efficiently. Creating groups will help you in assigning users to them and sharing the same permissions across all users in the group.
In this tutorial, we explained the syntax, various options and practical examples of using groupadd command. Whether you need to create basic groups, specify GIDs, or set encrypted passwords, this command provides the flexibility to meet your systems needs.