Linux gpasswd Command

Linux 命令大全Linux Command Collection

Linux gpasswd is a management tool for the workgroup files /etc/group and /etc/gshadow on Linux, used to add a user to a group or remove a user from a group.

Syntax

gpasswd [可选项] 组名

Optional parameters:

  • -a: Add a user to the group;
  • -d: Remove a user from the group;
  • -A: Specify an administrator;
  • -M: Specify group members, similar to the purpose of -A;
  • -r: Delete the password;
  • -R: Restrict users from logging into the group; only members of the group can join it using newgrp.

Examples

For example, the system has a peter account, which is not a member of the groupname group. To use newgrp, the password is required.

gpasswd groupname

This allows the user to temporarily become a member of the group; afterwards, files created by peter will also have the group groupname. So this method can temporarily let peter use a different group when creating files, instead of the group peter belongs to.

Therefore, using gpasswd groupname to set a password allows those who know the group password to temporarily switch to having the functionality of the groupname group.

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 useusermod -G group_name user_nameThis command can add a user to a specified group, but the previously added groups will be cleared.

Therefore, if you want to add a user to a group while retaining previously added groups, please use the gpasswd command to add the user:

gpasswd -a user_name group_name

Linux 命令大全Linux Command Collection

Other Extensions