Linux groupadd Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to manage users and groups on a Linux system using the groupadd command. You will create a new group, add users to the group, and modify the group properties. The lab covers the following steps: creating a new group, adding users to a group, and modifying group properties. The commands and examples provided in the lab will help you understand the practical usage of the groupadd command and its related functionalities for user and permission management on a Linux system.

Linux Commands Cheat Sheet


Skills Graph

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

Create a New Group

In this step, you will learn how to create a new group on the Linux system using the groupadd command.

First, let's create a new group named "developers" using the following command:

sudo groupadd developers

Example output:

No output if the group is created successfully.

The groupadd command creates a new group with the specified name. In this case, we created a group named "developers".

Next, let's verify that the group was created successfully by listing all the groups on the system:

sudo groups

Example output:

labex adm cdrom sudo dip plugdev lxd lpadmin sambashare developers

As you can see, the "developers" group is now listed among the groups.

Add Users to a Group

In this step, you will learn how to add users to a group on the Linux system.

First, let's create a new user named "john" using the following command:

sudo useradd -m john

Example output:

No output if the user is created successfully.

Now, let's add the "john" user to the "developers" group using the usermod command:

sudo usermod -a -G developers john

Example output:

No output if the user is added to the group successfully.

The usermod command is used to modify user account information. The -a option appends the user to the supplementary groups, and the -G option specifies the groups to which the user should be added.

To verify that the "john" user has been added to the "developers" group, we can use the groups command:

groups john

Example output:

john : john developers

As you can see, the "john" user is now a member of the "developers" group.

Modify Group Properties

In this step, you will learn how to modify the properties of a group on the Linux system.

Let's start by creating a new group named "finance" using the groupadd command:

sudo groupadd finance

Example output:

No output if the group is created successfully.

Now, let's modify the group ID (GID) of the "finance" group using the groupmod command:

sudo groupmod -g 1050 finance

Example output:

No output if the group properties are modified successfully.

The groupmod command is used to modify the properties of a group. In this case, we're changing the group ID (GID) of the "finance" group to 1050.

To verify the changes, let's list the group information using the getent command:

getent group finance

Example output:

finance:x:1050:

As you can see, the GID of the "finance" group has been changed to 1050.

Summary

In this lab, you learned how to create a new group using the groupadd command, add users to a group using the usermod command, and modify group properties. You created a new group named "developers", added a user named "john" to the "developers" group, and then created a new group named "finance". This hands-on experience with group management in a Linux environment will be valuable for system administration tasks.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like