9

After I look through the help. I didn't found much difference between them.

-g, --gid GROUP

The group name or number of the user's initial login group. The group name must exist. A group number must refer to an already existing group.

-G, --groups GROUP1[,GROUP2,...[,GROUPN]]]

A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. The default is for the user to belong only to the initial group.

If they are the same. Why both they exist?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Joe.wang
  • 443

3 Answers3

17

-g sets the initial, or primary, group. This is what appears in the group field in /etc/passwd. On many distributions the primary group name is the same as the user name.

-G sets the supplementary, or additional, groups. These are the groups in /etc/group that list your user account. This might include groups such as sudo, staff, etc.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
9

They are not the same. The -g option specifies the "primary" group that a user should belong to, while the -G option specifies one or many supplementary ("secondary") groups.

On a work machine I have access to I have

$ id
uid=1001(me) gid=1001(me) groups=1001(me),27(sudo),110(lxd),1005(theproject)

This shows that my "primary" group is me (same as my username). However, I'm also member of a number of secondary groups.

Membership of the sudo group, for example, allows me to use the sudo command to do scary stuff with the system, due to the way this is configured for sudo in /etc/sudoers on this particular machine.

Membership of the theproject group allows me to modify group-readable/writable files in a project I'm working on with others.

File created will (ordinarily) automatically be assigned to the primary group.

Kusalananda
  • 333,661
2

The group applied via -g is the primary group, so for example when you create a file it will default to making it with your primary group as the group associated with that file. You can however temporarily change which group it uses as default with the sg or newgrp commands. All groups in the -G are secondary groups. More about this here