Linux gpasswd Command
Linux gpasswd is a tool for managing group files /etc/group and /etc/gshadow in Linux, used to add or remove a user from a group.
Syntax
gpasswd [options] groupname
Option Parameters:
-a: Add user to the group;
-d: Remove user from the group;
-A: Specify administrator;
-M: Specify group members, similar to -A;
-r: Remove password;
-R: Restrict user login to the group, only members of the group can use newgrp to join the group.
Example
If there is a user account named peter on the system, and the account is not a member of the groupname group, using newgrp requires entering a password.
gpasswd groupname
This allows the user to temporarily join the group, and any files created by peter will belong to the groupname group. This method can temporarily allow peter to create files using a different group, rather than peter's own group.
Therefore, setting a password with gpasswd groupname allows anyone who knows the group password to temporarily switch to having the groupname group functionality.
gpasswd -A peter users
This makes peter the administrator of the users group, allowing the following operations:
gpasswd -a mary users
gpasswd -a allen users
Note: Adding a user to a group can be done using usermod -G group_name user_name
, which can add a user to a specified group, but it will clear any previously added groups.
To add a user to a group while retaining previously added groups, use the gpasswd command to add the user:
gpasswd -a user_name group_name