Linux gpasswd Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the gpasswd command in Linux to manage user groups. The gpasswd command allows you to add users to a group, remove users from a group, and manage group passwords. You will start by understanding the basic usage of the gpasswd command, then explore how to add a user to a group and remove a user from a group using practical examples.

The lab covers the following steps:

  1. Understand the gpasswd Command
  2. Add a User to a Group Using gpasswd
  3. Remove a User from a Group Using gpasswd

The gpasswd command is a standard Linux command and does not require any additional installation.

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/groupdel("`Group Removing`") linux/UserandGroupManagementGroup -.-> linux/userdel("`User Removing`") subgraph Lab Skills linux/groups -.-> lab-422702{{"`Linux gpasswd Command with Practical Examples`"}} linux/groupadd -.-> lab-422702{{"`Linux gpasswd Command with Practical Examples`"}} linux/groupdel -.-> lab-422702{{"`Linux gpasswd Command with Practical Examples`"}} linux/userdel -.-> lab-422702{{"`Linux gpasswd Command with Practical Examples`"}} end

Understand the gpasswd Command

In this step, we will learn about the gpasswd command in Linux, which is used to administer the /etc/group file and the /etc/gshadow file. The gpasswd command allows you to add users to a group, remove users from a group, and manage group passwords.

To view the available options for the gpasswd command, you can run:

$ gpasswd --help
Usage: gpasswd [options] GROUP
Options:
  -a, --add USER                add USER to GROUP
  -d, --delete USER             remove USER from GROUP
  -h, --help                    display this help message and exit
  -M, --members USER[,USER...]  set the list of members of GROUP
  -r, --remove-password         remove the password from GROUP
  -R, --restrict                restrict access to GROUP
  -A, --administrators USER[,USER...]
                                set the list of administrators for GROUP
  -P, --password PASSWORD       use this password for the group

The most commonly used options are:

  • -a, --add USER: Add a user to the specified group.
  • -d, --delete USER: Remove a user from the specified group.
  • -M, --members USER[,USER...]: Set the list of members for the group.

Let's explore some examples of using the gpasswd command.

Add a User to a Group Using gpasswd

In this step, we will learn how to add a user to a group using the gpasswd command.

First, let's create a new group called "developers":

$ sudo groupadd developers

Now, let's add the user "labex" to the "developers" group:

$ sudo gpasswd -a labex developers
Adding user labex to group developers

Example output:

Adding user labex to group developers

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

$ groups labex
labex : labex developers

The output shows that the user "labex" is now a member of the "developers" group.

Remove a User from a Group Using gpasswd

In this step, we will learn how to remove a user from a group using the gpasswd command.

Let's first verify that the user "labex" is a member of the "developers" group:

$ groups labex
labex : labex developers

Now, let's remove the user "labex" from the "developers" group:

$ sudo gpasswd -d labex developers
Removing user labex from group developers

Example output:

Removing user labex from group developers

To verify that the user has been removed from the group, we can use the groups command again:

$ groups labex
labex : labex

The output shows that the user "labex" is no longer a member of the "developers" group.

Summary

In this lab, we learned about the gpasswd command in Linux, which is used to administer the /etc/group file and the /etc/gshadow file. We explored how to add a user to a group, remove a user from a group, and manage group passwords using the gpasswd command. We created a new group called "developers" and added a user "labex" to it, then verified the user's membership in the group. Finally, we learned how to remove a user from a group using the gpasswd command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like