Linux gpasswd Command
Linux gpasswd is a management tool for the work group files /etc/group and /etc/gshadow on Linux, used to add a user to a group or remove one from a group.
Syntax
gpasswd [可选项] 组名
Optional parameters:
- -a: Add a user to a group;
- -d: Remove a user from a group;
- -A: Specify an administrator;
- -M: Specify group members; its purpose is similar to -A;
- -r: Delete the password;
- -R: Restrict user login to the group; only members of the group can join it using newgrp.
Example
If the system has an account named peter that is not itself a member of the group groupname, using newgrp only requires entering a password.
gpasswd groupname
This lets the user temporarily join the group, so files subsequently created by peter will also belong to groupname. This approach temporarily allows peter to create files under a different group rather than peter's own group.
Therefore, using gpasswd groupname to set a password allows people who know the group password to temporarily switch to having the groupname group's privileges.
gpasswd -A peter users
In this way, peter becomes the administrator of the users group and can perform the following operations:
gpasswd -a mary users
gpasswd -a allen users
Note: To add a user to a group, you can use the command usermod -G group_name user_name, which adds a user to the specified group but clears the previously added groups.
So when you want to add a user to a group while preserving previously added groups, use the gpasswd command to add the user:
gpasswd -a user_name group_name
Linux Command Reference