# Managing Groups in Linux

Linux group management is an essential part of Linux system administration, allowing us to organize users into groups with pre-set permissions for easier management of file permissions and system resources.

## Types of Groups

There are 2 categories of groups in the Linux operating system:

* **Primary group** - When a new user is created on Linux using the [`useradd`](https://karlygash-yakiyayeva.dev/managing-users-in-linux) command, a group with the same name as the username is also created, and the user is added as the group's sole member. This group is the user's primary group. A user has only one primary group at any given time.
    
* **Secondary (supplementary) groups** - Additional groups are used to extend permissions and access rights of a user beyond what is provided by the primary group. For example, if users need access to a shared directory or need to execute tasks that require different group permissions, they can be added to the relevant secondary groups. Users can be members of multiple secondary groups.
    

### **Key Files for Group Management**

* `/etc/group`: This file is a critical configuration file that defines the groups to which users belong. It's a text file that contains one entry per line, each describing a group.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711701408298/3b1b8be9-7c45-4e73-b9c9-fa868dbf28c1.png align="center")
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">The password field stores a password for the group. It is usually empty (represented by an <code>x</code> or <code>*</code>), indicating that the group password is not used. Group passwords are a deprecated feature for managing group membership and access.</div>
    </div>
    
* `/etc/gshadow`: This file complements the `/etc/group` file by providing a secure way to store group passwords and manage group membership. Similar to `/etc/group`, it contains one entry per line for each group, but with a focus on security-sensitive information.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711702503796/f68c1311-7d45-4226-a63d-f80b5439a942.png align="center")
    

## Other Common Groups

There are several common group names you might encounter in Linux:

* **sudo** – A member of this group can use the [sudo command](https://karlygash-yakiyayeva.dev/linux-sudo-command-explained) to elevate their privileges
    
* **cdrom** – Allows the user to mount the optical drive
    
* **adm** – Allows the user to monitor Linux system logs
    
* **lpadmin** – Allows the user to configure printers
    
* **plugdev** – Allows the user to access external storage devices
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711733578679/1f6d51db-ab5d-47ac-ac29-774d9105bee6.png align="center")

## Hands-on Exercise Overview

This hands-on shows:

* how to add, delete, and modify groups;
    
* how to add users to and remove from the existing groups.
    

## Hands-on Exercise

1. To find out which groups a user is a member of, run:
    
    ```bash
    groups
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711707354813/856afb5a-19c7-4b93-93b6-cab356855cfb.png align="center")
    
2. To find out which groups another user is a member of, run:
    
    ```bash
    groups username
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711707421699/1099476c-6589-4b41-9fe5-667b49f2baa2.png align="center")
    
3. To create a new group in Linux, run:
    
    ```bash
    sudo groupadd groupname
    ```
    
4. To confirm the new group is in the Linux */etc/group* file:
    
    ```bash
    getent group groupname
    ```
    
    * `getent`: The command used to retrieve entries from databases configured in the system.
        
    * `group`: This specifies the database to query. In this case, `group` refers to the group database, which contains information about the groups on the system.
        
    * `groupname`: This is the name of the group you want to query.
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711707955816/20369651-a79a-42ae-86bb-61cb09334161.png align="center")
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">When you create a new group, it’s a normal group with GID higher than 1000. You can also create a system group that automatically takes a group ID between SYS_GID_MIN and SYS_GID_MAX as defined in <code>/etc/login.defs</code>.</div>
    </div>
    
5. To create a system group, run
    
    ```bash
    sudo groupadd -r groupname
    ```
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">System groups are created for system purposes, such as running system services or daemon processes. They help in managing permissions and access rights for system processes and files.</div>
    </div>
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711731547747/401b29aa-2ba1-4299-8a8c-d84c981a89f8.png align="center")
    
6. To modify a group name, use the `groupmod` command:
    
    ```bash
    sudo groupmod -n new-name old-name
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711732423406/ff1248b7-16e6-41c7-a94e-b7934a1e237a.png align="center")
    
7. To add a user to an existing group while keeping them in their current groups, use the `usermod` command with the `-a` (append) and `-G` (groups) options:
    
    ```bash
    sudo usermod -aG groupname username
    ```
    
    Confirm if the user has been added by using the `groups` command.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711708450883/4b5c80e5-5932-42bf-9608-62d68aa7a1b2.png align="center")
    
8. An alternative for adding the user to an existing group is using the `gpasswd` command:
    
    ```bash
    sudo gpasswd -a username groupname
    ```
    
    * `gpasswd` \- a command used for managing `/etc/group` and `/etc/gshadow` group files. It allows administrators to modify group passwords and memberships, including adding or removing users from groups and setting or changing group passwords.
        
    * `-a`: Option that stands for "add", indicating that you want to add a user to a group.
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711709272134/7773ff2d-5335-4634-8dc2-0489e7f59c06.png align="center")
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">After adding the current user to the specific group, that user must log in and log out for the changes to take effect.</div>
    </div>
    
9. To remove a user from a specific group, use `gpasswd` command with `-d` option:
    
    ```bash
    sudo gpasswd -d username groupname
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711710387371/95db6966-6a6c-4442-b7e4-5214d27fed5c.png align="center")
    
10. To delete a group, use the `groupdel` command:
    
    ```bash
    sudo groupdel groupname
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711710687686/2aedbe60-21e4-44c2-b99d-fafedb093bc6.png align="center")
    

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">When a new file is created, the file will be created with the permissions that will include ownership of the primary group.</div>
</div>

## References

1. [Linux Crash Course - Managing Groups](https://www.youtube.com/watch?v=GnlgAD8-GhE&t=977s)
    
2. [User Management in Linux by phoenixNAP](https://phoenixnap.com/kb/user-management-linux#ftoc-heading-9)
    
3. [Create New Groups in Linux With Groupadd Command](https://linuxhandbook.com/groupadd-command/)
    
4. [Linux sudo command explained](https://karlygash-yakiyayeva.dev/linux-sudo-command-explained)
    
5. [How to Add a User to a Linux Group](https://phoenixnap.com/kb/add-user-to-linux-group#ftoc-heading-13)
