# Groups

Groups are collections of users. They are used to simplify access control and permissions management.&#x20;

Users within the same group share common permissions to files and directories. A group also has a unique group ID (GID).&#x20;

When a user creates a file, the file’s group ownership is set to the user’s primary group by default.

There are 2 categories of groups in the Linux operating system i.e. **Primary** and **Secondary** groups.&#x20;

#### Primary Group

The *Primary Group* is a group that is automatically generated while creating a user with a unique user ID simultaneously a group with an ID the same as the user ID is created and the user gets added to the group and becomes the first and only member of the group. This group is called the primary group.&#x20;

#### Secondary Group

A *secondary group* is a group that can be created separately with the help of commands and we can then add users to it by changing the group ID of users.

Now we will discuss the important commands to manage users in Linux.

1. Group Creation

```bash
groupadd group_name
Ex: sudo groupadd testgp
```

The below command created a group with the name provided. The group while creating gets a group ID and we can get to know everything about the group as its name, ID, and the users present in it in the file “/etc/group”.

2. Group Password Setup

```bash
gpasswd group_name
Ex: sudo gpasswd testgp
```

Above command is used to set the password of the group. After executing the command, we have to enter the new password which we want to assign to the group. The password has to be given twice for confirmation purposes.

3. Group Password file

```bash
cat /etc/gshadow
```

To access information about groups and their passwords, you can view the password file, /etc/gshadow. However, keep in mind that this file is not intended for regular viewing.

4. Adding User to Old group

To add a user to an existing group, you can utilize the usermod command. By specifying the group name, you can add a user to the desired group. However, note that when a user is added to a new group, they are automatically removed from their previous groups.

```bash
usermod -G group_name  username
Ex: usermod -G testgp ubuntu
```

{% hint style="info" %}
If we add a user to a group then it automatically gets removed from the previous groups.
{% endhint %}

5. #### **Add User to Group Without Removing from Existing Groups**

```bash
usermod -aG *group_name  *username
Ex: usermod -aG testgp ubuntu
```

This command is used to add a user to a new group while preventing him from getting removed from his existing groups.

6. #### **Command to Add Multiple Users to a Group**

{% code overflow="wrap" %}

```bash
gpasswd -M *username1, *username2, *username3 ...., *usernamen *group_name
Ex: gpasswd -M ubuntu1, ubuntu2, ubuntu3 testgp
```

{% endcode %}

To add multiple users to a group simultaneously, you can utilize the gpasswd command with the -M option. This command allows you to specify a list of usernames separated by comma&#x73;**.**

7. #### **Deleting a User from a Group**

```bash
gpasswd -d *username1  *group_name
Ex: gpasswd -d ubuntu  testgp
```

The user is then removed from the group though it is still a valid user in the system but it is no longer a part of the group.

8. #### **Command to Delete a Group**

```bash
groupdel *group_name
Ex: groupdel  testgp
```

To delete a group from the system, use the groupdel command. This action removes the group while retaining the users who were members of the group.

{% hint style="info" %}
Administrators can organize user accounts, assign appropriate access permissions, and ensure a secure and efficient Linux system.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devops-3.gitbook.io/devops/docs/module-1-linux-introduction/groups.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
