To make it easier to manage groups, linux provides some common commands that allow you to add, delete, and modify groups in a more standardized way.

Add Group

We can add groups through the groupadd command.

Here are some common options for groupadd command:

Option Meaning
-g or ‑‑gid Specify the group’s ID. The specified value must be greater than or equal to the value of the GID_MIN variable in the /etc/login.defs configuration file and less than or equal to the value of the GID_MAX variable in the same file.
-r or ‑‑system If this option is specified, the created group is a system group. The system group’s id range is between the values of the SYS_GID_MIN and SYS_GID_MAX variables in the /etc/login.defs configuration file.

Add Groups

Let’s add new groups:

1
2
3
4
groupadd dongs_group
groupadd dongs_another_group
groupadd dongs_third_group
tail -3 /etc/group
img

Output the three newly created groups

Modify Group

We can modify the group information through the groupmod command.

Here are some common options for groupmod command:

Option Meaning
-g or ‑‑gid Specify the group’s new ID.
-n or ‑‑new-name Specify the group’s new name.

Modify Group Name

Let’s modify the group’s name:

1
2
3
tail -1 /etc/group
groupmod -n dongs_surplus_group dongs_third_group
tail -1 /etc/group
img

Output the last group before and after its name modification

Delete Group

Let’s delete the group:

1
2
3
tail -1 /etc/group
groupdel dongs_surplus_group
tail -1 /etc/group
img

Output the last group before and after its deletion

Group Password

We can use the gpasswd command to specify or change the login password for the group.

Here are some common options for gpasswd command:

Option Meaning
-a or ‑‑add Add the user to group.
-d or ‑‑delete Delete the user from group.
-r or ‑‑remove-password Remove the password from the group.

Create Password for Groups

1
2
3
4
groupadd dongs_secret_group
cat /etc/gshadow | grep 'dongs_secret_group:'
gpasswd dongs_secret_group
cat /etc/gshadow | grep 'dongs_secret_group:'    # Run this separately after the completion of the previous commands
img

Output the gshadow file before and after the newly created group's password modification

New Group

With the newgrp command, we can have the user log in to another group.

Log in to Another Group

Log in and log out of another group:

1
2
3
4
5
id
newgrp dongs_secret_group
id
exit    # Exit the logged in group
id
img

Output the user's id and group ids before and after the user's entry and exit of the another group

References GROUPADD(8), GROUPMOD(8), GROUPDEL(8), GPASSWD(1), NEWGRP(1)

Buy me a coffeeBuy me a coffee