The gpasswd command is used to administer groups in Linux and Unix. It writes group information in /etc/group and /etc/gshadow.
Because the gpasswd command works with some files located in /etc, it needs to be used with root priviledges.
Any group can have group administrators, group members and a password. The passwords for groups aren’t used so much, because they can generate security issues, since a group password needs to be know by all the members of the group.
In this article I will show you how to use the gpasswd command in order to edit the group account information:
How to add user to group:
Syntax: gpasswd -a user group
I will add the user mike34 to the group naboo21
$ sudo gpasswd -a mike34 naboo21
Adding user mike34 to group naboo21
How to remove a user from a grup:
Syntax: gpasswd -d user group
I will remove the recently added user mike34 from the naboo21 group:
$ sudo passwd -d mike34 naboo21
Removing user mike34 from group naboo21
How to set a groups memberlist:
Syntax: gpasswd -M user1 user2 use99 group
$ sudo gpasswd -M mike1,mike2,mike3 naboo21
$ grep naboo21 /etc/group
This command is very usefull when you need to asign users to groups.
How to set an group administrator:
Syntax: gpasswd -A user group
The group administrator is the “group owner”, the user that can change group details.
I will set mike the group administrator.
$ su mike
$ gpasswd naboo21
gpasswd: Permission denied.
$ sudo gpasswd -A mike naboo21
$ gpasswd naboo21
Changing the password for group naboo21
Re-enter new password:
Other usefull gpasswd features:
- to set or change a password on a group, use sudo gpasswd group and insert a new password (twice)
- to remove a group’s password use sudo gpasswd -r group
You can also use the useradd command to add users to groups.