Linux IntroductionChapter 4. User, Group and Permission

gpasswd (Add and Delete Users to Group)

gpasswd (Add and Delete Users to Group)

Managing Group Membership with gpasswd Command

The gpasswd (Group PASSWorD) command is often used to manage group members. With the -a option, you can add a user to a group. With the -d option, you can delete a user from a group. With the -M option, you can register all group members in a group.

Using this command with no option, you can also create a password for a group. As password creation isn’t a frequently used functionality of the command, you may find it easy to memorize with the fact that the gpasswd command was originally designed to manage the /etc/group file including group passwords.

Add a user to a group (-a option)

To add a user to a group, run the gpasswd -a [user name] [group name] command as the superuser. For example, to add user_a to a sudo group, run the command below. (You can get the same result as that of the usermod -aG command. You just need to be careful because the order of arguments is different).

Command Line - INPUT
gpasswd -a user_a sudo

To check the group status, run the groups command. You can see that user_a is now a member of the sudo group.

Command Line - INPUT
groups user_a
Command Line - RESPONSE
user_a : group_a sudo group_b group_c

Delete a user from a group (-d option)

To delete a user from a group, run the gpasswd -d [user name] [group name] command as the superuser. For example, to delete user_a from the sudo group, run the command below.

Command Line - INPUT
gpasswd -d user_a sudo
Command Line - RESPONSE
Removing user user_a from group sudo

To check the group status, run the groups command. You can see that user_a is no longer a member of the sudo group.

Command Line - INPUT
groups user_a
Command Line - RESPONSE
user_a : group_a group_b group_c

You can also learn this topic offline. Click AmazonKindle.

Linux Introduction
Course Content