How to Create Groups in Linux?
- A group in Linux refers to a logical collection of user accounts that are grouped for administrative and security purposes.
- Groups allow for easier management and organization of users with similar access rights and permissions and Groups are identified by a Group ID (GID), which is a unique numerical identifier assigned to each group.
- In Linux, each user belongs to at least one group, known as their primary group, and can also be a member of multiple secondary groups.
Before exploring the topic of Linux add group, we have to know how to check which groups a user belongs to in Linux. You can use the groups command or the id command to achieve this.
-
The groups command in Linux allows you to retrieve a list of groups to which a user belongs.
-
The id command in Linux provides detailed information about a user such as a user's UID, GID (Group ID), and a list of secondary groups.
The username is the name of the user whose group must be found.
In the context of Linux add group, we should also be able to get the information like group name and group ID about a particular group or groups we can use the following methods,
-
The /etc/group file is where information about the groups is stored. We can use the cat command to view the details of all the groups present in the system.
-
The getent command can be used to retrieve the entries from databases configured on the system, including the /etc/group file. To get information about a particular group, we can use the following command,
Linux provides the groupadd command to create and manage groups effortlessly. In this article, we will explore the process of creating groups in Linux, along with various options and scenarios.
groupadd Command Syntax
In the context of the topic, linux adds groups, and the primary command used to create groups in Linux is groupadd. It allows you to create new groups with ease. The syntax for the groupadd command is as follows:
The group_name is the name of the new group to be created. The options are used to customize the process of creating groups. Some options are,
- The -o or --non-unique option can be used to create a group with a GID that is already in use. By default, Linux does not allow the creation of groups with non-unique GIDs
- The -f or --force option forces the creation of a group even if some of the specified options or group attributes are invalid or conflicting.
- The -R or --root CHROOT_DIR option specifies an alternative root directory for the group and its associated files.
- The -h or --help option provides information about the groupadd command and its available options.
- The -V or --version option displays the version of the groupadd command.
We will explore other options in the following sections.
Creating a Group in Linux
To perform the Linux add group or create a group in Linux, open a terminal and enter the groupadd command, followed by the desired name for the group. For example, to create a group named developers, use the following command:
This command will create a new group called developers on your Linux system. Please remember to use the command with sudo, if you are not in the root terminal.
If you get an error in the following format, it means that the linux add group command failed and there is already a group with the same name in the system.
You can use the groupdel group_name to delete the group and create another. Please remember that the group name and GID must be unique.
Creating a Group with Specific GID
By default, after the linux add group command, Linux assigns a unique Group ID (GID) to each group. However, there may be cases where you need to create a group with a specific GID. To achieve this, you can utilize the -g option followed by the desired GID when using the groupadd command. Here's an example:
In this case, the group developers will be created with the group ID of 1002.
If you get an error in the following format, it means that there is already a group with the same GID in the system.
You have to delete the group or use another ID for the group.
Creating a System Group
Linux also allows you to create system groups. System groups are primarily used for managing system-related tasks and services. To create a system group, you can use the -r option along with the linux add group command. Here's an example of creating a system group called sysadmin,
The -r flag marks the group as a system group, and in this case, it creates a system group called sysadmin. System groups often have GIDs in the range starting from values below 1000.
Overriding the Default /etc/login.defs Values
The /etc/login.defs is an important configuration file in Linux systems that consists of key-value pairs, where each line represents a specific configuration option and is referred to when using the linux add group command. The sample content of the file is,
However, you can override these default values by using the groupadd command with the -K KEY=VALUE or the -A option for the newly created group. The KEY is the name of the property and VALUE will be the new value.
For instance, to set a specific password aging policy the -K option with the required property and value. Here's an example that modifies the minimum GID to be assigned to the group,
This command only allows the group ID to be greater than or equal to 1500 for the developers group.
The chsh command in Linux is used to change the default shell (command interpreter) for a user. By default, when a user logs in, their shell determines the command line environment and the behavior of their interactions with the system. The chsh command allows users to modify their default shell to a different shell available on the system.
Creating a System Group with a Password
By default, system groups in Linux are created without a password. However, if you wish to assign a password to a system group, you can use the -p option of the groupadd command or the gpasswd command.
Let us create a group called developers with a password Linux using the -p option of the groupadd command. The command is,
A new group called developers with the password mysecretpassword will be created.
We can achieve the same using the recommended gpasswd command for managing group passwords. The gpasswd command offers more flexibility and features for managing group passwords, including the ability to change passwords, add or remove users from the group, and more. Follow the steps,
1. Create a group:
2. Assign a password for the group using the "gpasswd" command:
The gpasswd command prompts you to enter the desired password for the developers group.
Conclusion
- Creating groups in Linux can be done using the groupadd command.
- The groupadd command provides various customizations through the options.
- We can specify the GID, create system groups, and assign passwords using respective options with the groupadd command.
- The /etc/login.defs is a configuration file for users and groups. We can also override this file using the -K option.
- Understanding various options of the groupadd command can help in the effective management of groups on your Linux system.