Beginners Guide for Gpasswd Command on Linux

Linux TLDR
Last Updated:
Reading time: 2 minutes

The gpasswd command is used to administer “/etc/group” and “/etc/gshadow” file but that does not give you the complete context.

In other words, using the gpasswd command, you can add or remove users from a group, add or remove passwords from a group, promote a user as group admin, and set the list of group members.

On a positive note, remember that setting a password for a group is never a good idea for security reasons because all users are required to know the group password.

Tutorial Details

DescriptionGpasswd
Difficulty LevelLow
Root or Sudo PrivilegesYes
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesgpasswd
Internet RequiredNo

Syntax of the Gpasswd Command

The gpasswd command requires two arguments: one is the option, and the other is the group name.

$ gpasswd [OPTION] [GROUP]

Demo Group for Gpasswd Practise

To demonstrate the gpasswd command’s practical usage, we will create a temporary group (ex: “demogroup“) using the following command.

$ sudo addgroup demogroup

Output:

Creating a group using gpasswd command

Verify the group is created by searching the “demogroup” name on the “/etc/group” file using the grep command.

$ grep demogroup /etc/group

Output:

Checking the group file to verify the new group is created

Adding a User to a Group

The following command will add the specified user (ex: “linuxtldr“) to the newly created group (ex: “demogroup“).

$ sudo gpasswd -a linuxtldr demogroup

Output:

Adding user to new group using gpasswd command

Use the groups command with the username to verify the user is added to that group.

$ groups linuxtldr

Output:

Listing all groups current user is associated with

You can add multiple users to the specified group by using a comma (“,“) as a separator.

Removing the User from the Group

The following command will remove the specified user (ex: “linuxtldr“) from the specified group (ex: “demogroup“).

$ sudo gpasswd -d linuxtldr demogroup

Output:

Removing user from the previously added group

Verify that the users are removed from the specified group using the following command.

$ groups linuxtldr

Output:

Re-checking the user group status

To remove multiple users from the specified group, use a comma (“,“) as a separator.

Setting the Password for the Group

You can set a password for a group name (ex: “demogroup“) with the “gpasswd” command.

$ sudo gpasswd demogroup

Output:

Setting a password to the group using gpasswd command

Removing Password from Group

Use the “-r” or “--remove-password” flag to remove the password from the specified group.

$ sudo gpasswd -r demogroup

Output:

Removing password from the group using gpasswd command

Promote the User as Group Adminstrator

The following command will assign the specified user as the group administrator.

$ sudo gpasswd -A linuxtldr demogroup

Output:

Promoting the user as a group administrator using gpasswd command

To demote the user from the administrator position, use the same command, replacing the old user with the new one.

Setting the List of Group Members

Use the “-M” or “--members” flag to add multiple users to the specified group using the following command.

$ sudo gpasswd -M linuxtldr,jr demogroup

Output:

Adding multiple users to group using gpasswd command

Removing the Demo Group

After you are done with the practice, you can remove the “demogroup” group we created at the beginning of this article by using the following command.

$ sudo groupdel demogroup

Output:

Deleting the group using gpasswd command

That was the last example.

I hope you enjoyed the article.

If you have any recommendations, feel free to write them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.