The gpasswd command is used in linux to remove user from group, add users to group, list group members and set password for a group. Gpasswd command alters '/etc/group' and '/etc/gshadow' files in Linux.
In this tutorial we learn how to manage Linux groups using gpasswd command.
Gpasswd Command Syntax
Lets check the syntax for gpasswd command and its options.
gpasswd [options] group
-a, --add user Add the user to the named group. -d, --delete user Remove the user from the named group. -A, --administrators user,... Set the list of administrative users. -M, --members user,... Set the list of group members.
Add a User to a group using gpasswd comand
Let check how to add a 'user1' to a group 'sales'
$ gpasswd -a user1 sales
Add multiple user to a group
To add multiple users to sales group following below command:
$ gpasswd -M user1,user2,user3 sales
Remove a user from group
We can use -d
option to remove a user from a group.
Following command will remove user 'user2' from the 'sales' group.
$ gpasswd -d user2 sales
Set a user as group administrator
We use -A option to set a user as group administrator.
The following example set 'user1' and 'user2' as the group admin for 'sales' group.
$ gpasswd -A user1,user2 sales
or
$ gpasswd -A user1 linuxgroup
Set password for a group
Its very rare we use group password but I like to show you that there is an option for it.
$ gpasswd linuxgroup Changing the password for group linuxgroup New Password: Re-enter new password:
Conclusion
In this tutorial we learned how to use gpasswd command to manage Linux groups. I hope you enjoyed reading this tutorial and please leave your suggestions in the below comment section.
The example for -M above implies adding user1,user2,user3 in addition to users already there.
The man pages for gpasswd clearly state that option -M is to "Set the list of group members."
This will destroy any existing set of members, so therefore is not an implicit "add" unless the group was empty to begin with.