Oct 7, 2023 4 min read

How to Create Groups on Linux (groupadd Command)

Create Groups on Linux (groupadd Command) with our step-by-step tutorial. Groups are a vital component of the system's user privilege management.

Create Groups on Linux (groupadd Command)
Table of Contents

Introduction

Before we begin talking about how to create groups on Linux (groupadd Command), let's briefly understand – What is groups?

Groups are a vital component of the system's user privilege management. They allow users to be organized into logical units, facilitating easier administration and controlled access to resources. A group consists of multiple users, simplifying file sharing, permissions, and communication within the group.

Understanding Linux groups is essential for system security, user management, and efficient collaboration. Learn more about Linux groups and their significance in managing user privileges effectively.

In this tutorial, you will create groups on Linux (groupadd Command). We will also address a few FAQs on how to create groups on Linux (groupadd Command).

Advantages of Groups

  1. Efficient Collaboration: Groups on Linux enable seamless communication and file sharing among members, fostering effective teamwork.
  2. Simplified Permissions: Assigning group permissions instead of individual ones streamlines access control, saving time and effort.
  3. Easy User Management: Adding or removing users from groups simplifies user administration, ensuring smooth user management.
  4. Enhanced Security: Controlling access through groups enhances system security by managing privileges more effectively.
  5. Centralized Resource Control: Groups allow for centralized management of resources, making it easier to allocate permissions and monitor usage.

groupadd Command Syntax

The groupadd command has the following general syntax:

groupadd [OPTIONS] GROUPNAME

New groups can only be created by root or a user with sudo access.

When groupadd is run, it creates a new group with the settings supplied on the command line as well as the default values in the /etc/login.defs file.

Creating a Group in Linux

Type groupadd followed by the new group name to establish a new group.

To create a new group named mygroup, for example, type:

groupadd mygroup

The command updates the /etc/group and /etc/gshadow files to include the new group.

After the group has been created, you can begin adding users to it.

If a group with the same name already exists, the system will display the following error message:

groupadd: group 'mygroup' already exists

Use the -f (--force) option to conceal the error notice and force the command to exit successfully if the group exists:

groupadd -f mygroup

Creating a Group with Specific GID

Groups are identifiable in Linux and Unix-like operating systems by their name and a unique GID (a positive integer).

When a new group is created, the system allocates the next available GID from the group ID range supplied in the login.defs file by default.

To create a group with a specific GID, use the -g (--gid) option.

For example, to create the group mygroup with the GID 1010, type:

groupadd -g 1010 mygroup

You may check the GID of a group by listing all groups and using grep to filter the results:

getent group | grep mygroup
Output

mygroup:x:1010:

You will receive the following error if a group with the supplied GID already exists:

groupadd: GID '1010' already exists

The groupadd command can be used with the -o (--non-unique) argument to create a group with a non-unique GID:

groupadd -o -g 1010 mygroup

Creating a System Group

The system and regular (normal) groups have no real technological differences. System groups are typically used for certain system operations, such as making backups or doing system maintenance.

Groups of systems GIDs are taken from the login.defs file's range of system group UDs, which differs from the range used for normal groups.

To create a system group, use the -r (--system) option. To create a new system group named mysystemgroup, for example, type:

groupadd -r mysystemgroup

Overriding the Default /etc/login.defs Values

You can override the default values in the /etc/login.defs file by using the -K (--key) option followed by KEY=VAL.

Basically, the maximum and minimum values of the normal and system group IDs for automated GID selection when creating a new group are the only things you may change.

Let's pretend you wish to make a new group with a GID between 1200 and 1500. To do so, enter the min/max numbers as follows:

groupadd -K GID_MIN=1200 -K GID_MAX=1500 mygroup

Creating a System Group with Password

Adding a password to a group serves no use and may pose a security risk because the password must be known by several users.

You can set a password for the new group by using the -p (--password) option followed by password:

groupadd -p grouppassword mygroup

FAQs to Create Groups on Linux (groupadd Command)

Can I specify a specific Group ID (GID) while creating a group?

Yes, you can specify a GID using the -g option with the groupadd command. For instance, to create a group with GID 1000, use: groupadd -g 1000 mygroup.

How can I add users to a group after creating it?

To add users to a group, you can use the usermod command with the -aG option. For example, to add the user "john" to the group "mygroup", use: usermod -aG mygroup john.

How do I set a group's password?

Linux groups typically do not have passwords. Instead, they rely on user authentication. Group passwords are not commonly used within the Linux environment.

How can I list all the groups on my Linux system?

To list all the groups, you can use the cat command to view the /etc/group file, which contains group information. Execute: cat /etc/group.

Is it possible to delete a group?

Yes, you can delete a group using the groupdel command followed by the group name. Example: groupdel mygroup.

How can I modify group properties after creation?

To modify group properties, you can use the groupmod command. For instance, to change the group name from "mygroup" to "newgroup", use: groupmod -n newgroup mygroup.

Is there a limit to the number of users in a group?

While there is no hard limit on the number of users in a group, practical limitations may exist due to system resources. It's recommended to keep group sizes manageable for easier administration and resource allocation.

Conclusion

The groupadd command in Linux can be used to establish new groups.

Any Linux distribution, including Ubuntu, CentOS, RHEL, Debian, Fedora, and Arch Linux, can use these instructions.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.