Linux groupmod Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux groupmod command, which is used to modify the attributes of an existing group, such as the group name and Group ID (GID). We will learn how to change the name of a group and how to modify its GID using practical examples.

The lab covers the following steps: understanding the purpose and syntax of the groupmod command, modifying a group's name using the groupmod command, and changing a group's GID using the groupmod command. The content is straightforward and the language is concise, providing a clear overview of the lab's objectives and content.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/UserandGroupManagementGroup -.-> linux/groupadd("`Group Adding`") linux/UserandGroupManagementGroup -.-> linux/useradd("`User Adding`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") subgraph Lab Skills linux/groupadd -.-> lab-422706{{"`Linux groupmod Command with Practical Examples`"}} linux/useradd -.-> lab-422706{{"`Linux groupmod Command with Practical Examples`"}} linux/sudo -.-> lab-422706{{"`Linux groupmod Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the groupmod Command

In this step, we will explore the purpose and syntax of the groupmod command in Linux. The groupmod command is used to modify the attributes of an existing group, such as the group name and Group ID (GID).

To understand the syntax of the groupmod command, let's run the following command:

sudo groupmod --help

Example output:

Usage: groupmod [options] GROUP
  -g, --gid GID       change the group ID to GID
  -n, --new-name NEW_NAME   change the name to NEW_NAME
  -o, --non-unique    allow to use a duplicate (non-unique) GID
  -p, --password PASSWORD   the encrypted password of the group
  -R, --root CHROOT_DIR     directory to chroot into
  -P, --prefix PREFIX_DIR   prefix directory where are located the /etc/* files
  -h, --help          display this help message and exit

From the output, we can see that the basic syntax of the groupmod command is:

groupmod [options] GROUP

The most common options used with groupmod are:

  • -g, --gid GID: Change the group ID (GID) of the specified group to the given GID.
  • -n, --new-name NEW_NAME: Change the name of the specified group to the new name.

In the next steps, we will explore how to use these options to modify a group's name and GID.

Modify a Group's Name Using the groupmod Command

In this step, we will learn how to modify the name of an existing group using the groupmod command.

First, let's create a new group named "devops" on our system:

sudo groupadd devops

Now, let's use the groupmod command to change the name of the "devops" group to "developers":

sudo groupmod -n developers devops

Example output:

groupmod: group 'devops' changed to 'developers'

As you can see, the groupmod command with the -n (or --new-name) option allows us to change the name of the group from "devops" to "developers".

To verify the group name change, we can use the getent command:

getent group developers

Example output:

developers:x:1001:

The output confirms that the group name has been successfully changed to "developers".

Change a Group's GID Using the groupmod Command

In this step, we will learn how to change the Group ID (GID) of an existing group using the groupmod command.

First, let's check the current GID of the "developers" group:

getent group developers

Example output:

developers:x:1001:

The output shows that the current GID of the "developers" group is 1001.

Now, let's use the groupmod command with the -g (or --gid) option to change the GID of the "developers" group to 2000:

sudo groupmod -g 2000 developers

Example output:

groupmod: group 'developers' changed

To verify the GID change, let's check the group information again:

getent group developers

Example output:

developers:x:2000:

The output confirms that the GID of the "developers" group has been successfully changed to 2000.

Summary

In this lab, we learned the purpose and syntax of the groupmod command in Linux, which is used to modify the attributes of an existing group, such as the group name and Group ID (GID). We explored how to use the -n (or --new-name) option to change the name of a group, and the -g (or --gid) option to change the group's GID. These commands provide a way to manage group settings on a Linux system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like