In Linux, the simplest way to assign responsibilities to the users is to create a group with specified permissions and add the users to the group. Groups allow us to organize and administer the users by setting a predefined set of privileges for the group. This guide will follow the below-mentioned timeline for this article:
- What are Groups in Linux?
- How to Create Groups in Linux?
- Additional Tips About Groups in Linux
Let’s get started.
What are Groups in Linux?
A group helps assign different roles and permission to the group members collectively instead of setting the permissions to everyone. Every group information is stored in the default group file “/etc/group”. In Linux, the groups are of two types:
- System groups (Primary)
- Normal groups (Secondary)
This group is created for a specific system when creating a new user. The files created by the user are automatically added to its system group.
The groups created by the users are normal groups, and a user can be a member of several normal groups. These groups are created to manage the files and applications for the group members giving desired permission (read, write, and execute) to every member.
Let’s start creating groups.
How to Create Groups in Linux?
Creating a group in Linux is the basic administrative task for which the user should log in as the root user or have sudo privileges. Let’s execute the different approaches to create a group.
Groups are created in Linux using the “groupadd” command, which has the following syntax:
$ groupadd [options] groupname
The components of the syntax are as follows:
- groupadd: It represents the groupadd command used to create the group.
- options: It is replaced with the specified option of the groupadd command to perform a specific task.
- groupname: Replace this with the desired group name.
Let’s check the options in the “groupadd” command for creating the groups in Linux.
The groupadd command comes with the following options/flags in Linux:
|-g||Create a group with a specific GID using the “g” option.|
|-K||Override the default /etc/login/.defs file.|
|-o||Create the group with an existing (duplicate) GID.|
|-p||This option allows the user to create a group that is password protected.|
|-r||Use the “r” for creating the system group.|
Example 1: Create a Normal Group in Linux
Creating a group is a basic administrative task in Linux. The groupadd command is utilized for creating a group. For instance, to create a new group named “testgroup”, execute the below-written command:
$ sudo groupadd testgroup
The existing group information is stored in the “/etc/group” and “/etc/gshadow” system files. After creating the group “testgroup”, its information is stored in these default system files.
To verify the successful creation of the “testgroup”, use the following command:
$ getent group | grep testgroup
The output shows the group name and Group ID (GID) of the “testgroup” which shows the group is successfully created.
Example 2: Create a System Group in Linux
The usage of the groupadd command “r” flag allows the creation a system group. For example, to create a system group named “TestSystemGroup”, the following command is used:
$ sudo groupadd -r TestSystemGroup
Example 3: Create a Group Using a Specific GID
The “g” option helps to create a group with the desired Group ID (GID). For instance, to create a new group named “SampleGroup” with Group ID (GID) “5678”, execute the below command in the terminal:
Note: The getent command is used to check the group ID.
$ sudo groupadd -g 5678 SampleGroup $ getent group | grep SampleGroup
The output shows the “SampleGroup” is successfully created with the specified “5678” GID.
Example 4: Create a Group Using an Existing Group ID (GID)
The “o” option, along with “g” enables the user to create a new group with the group ID that already exists in the system. For example, to create a group “TestGroup2” with GID “5678” that exists in the system, the below command is utilized:
$ sudo groupadd -og 5678 TestGroup2
Let’s verify the groups having the GID “5678” in the system with the help of the following command:
$ getent group | grep 5678
The output displays two groups having the same GID “5678,” that will create a group of groups. When you use the “5678” GID to implement a command on a specific group, it will perform the function for both groups, which is undesirable.
Example 5: Create a Password-Protected System Group
The “p” option represents the password-protected feature of the groupadd command. To create a group named “TestPasswordGroup” by setting the group password “abc123”, execute the below-mentioned command in the terminal:
Note: The getent command is used to verify the group creation.
$ sudo groupadd -p abc123 TestPasswordGroup $ getent group | grep TestPasswordGroup
Additional Tips About Groups in Linux
Let’s discuss the common uses of the group, like deleting a group and adding a user to the group.
Delete a Group in Linux
The “groupdel” command is used to delete a group from the system. For instance, to delete a group named “SampleGroup”, the below-mentioned command is executed:
$ sudo groupdel SampleGroup
Add a User to a Specific Group
Sometimes, we need to add a new user to a group, for that, the “usermod” command is used. For example, to add a user “peter” to a group named “testgroup”, the below-mentioned command is executed:
$ sudo usermod -a -G testgroup peter
End of this informative guide.
The “groupadd” command creates a group in Linux. To create a normal group, use the“sudo groupadd <groupname>” command, and for creating a system group, execute the “sudo -r groupadd <groupname>” command in the terminal. Moreover, as discussed in this guide, a group can be created with a specific Group ID, a duplicate (Existing) Group ID, and password protection. This post has provided a brief guide to create groups in Linux.