Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash gpasswd Usage: Manage group memberships
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
      1. Adding a User to a Group
      2. Removing a User from a Group
      3. Setting a Group Password
      4. Removing a Group
    5. Tips

[Linux] Bash gpasswd Usage: Manage group memberships

Overview

The gpasswd command is used in Linux systems to administer /etc/group and /etc/gshadow files. It allows users to manage group memberships, add or remove users from groups, and set group passwords.

Usage

The basic syntax of the gpasswd command is as follows:

gpasswd [options] [arguments]

Common Options

  • -a, --add USER GROUP: Adds the specified user to the specified group.
  • -d, --delete USER GROUP: Removes the specified user from the specified group.
  • -r, --remove GROUP: Removes the specified group.
  • -P, --password PASSWORD: Sets the password for the group.
  • -h, --help: Displays help information about the command.

Common Examples

Adding a User to a Group

To add a user named john to a group called developers, you would use:

gpasswd -a john developers

Removing a User from a Group

To remove a user named john from the developers group, the command would be:

gpasswd -d john developers

Setting a Group Password

To set a password for a group named admins, you can use:

gpasswd -P mypassword admins

Removing a Group

To remove a group called oldgroup, you would execute:

gpasswd -r oldgroup

Tips

  • Always ensure you have the necessary permissions (usually root) to modify group memberships.
  • Use gpasswd with caution, especially when removing users or groups, as these actions can affect system access.
  • To view current group memberships, you can use the groups command followed by the username.