What is the difference between -g and -G options in useradd
-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.
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.