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.
|Root or Sudo Privileges||Yes|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
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 Adding group `demogroup' (GID 1006) ... Done.
Verify the group is created by searching the “
demogroup” name on the “
/etc/group” file using the grep command.
$ grep demogroup /etc/group demogroup:x:1006:
Adding a User to a Group
The following command will add the specified user (ex: “
linuxtldr“) to the newly created group (ex: “
$ sudo gpasswd -a linuxtldr demogroup Adding user linuxtldr to group demogroup
Use the groups command with the username to verify the user is added to that group.
$ groups linuxtldr linuxtldr : linuxtldr sudo admin newadmin demogroup
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: “
$ sudo gpasswd -d linuxtldr demogroup Removing user linuxtldr from group demogroup
Verify that the users are removed from the specified group using the following command.
$ groups linuxtldr linuxtldr : linuxtldr sudo admin newadmin
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 “
$ sudo gpasswd demogroup Changing the password for group demogroup New Password: Re-enter new password:
Removing Password from Group
Use the “
-r” or “
--remove-password” flag to remove the password from the specified group.
$ sudo gpasswd -r demogroup
Promote the User as Group Adminstrator
The following command will assign the specified user as the group administrator.
$ sudo gpasswd -A linuxtldr demogroup
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
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
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.