Solved Adding User to Groups Question

I've been setting up my new workstation and wanted to add groups to my user such as 'operator' and 'video' but came across some unexpected behavior that I hope someone shed some light on.

Currently, the output using uname -a is:

Code:
FreeBSD fisher.loga.us 11.2-RELEASE-p4 FreeBSD 11.2-RELEASE-p4 #0: Thu Sep 27 08:16:24 UTC 2018     root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC  amd64


Case1:

Getting to the nitty-gritty, if I issue the following command(notice only space between groups) pw usermod cres24 -G wheel operator cups the result is as follows after a reboot:

root@fisher:~ % id cres24
Code:
uid=1002(cres24) gid=1002(cres24) groups=1002(cres24),0(wheel)

In this case, only the first group (wheel) is added to the user.


Case 2:

If I issue the following command (notice commas between groups) pw usermod cres24 -G wheel,operator,video,cups the result is as follows after a reboot:

root@fisher:/ # id cres24
Code:
uid=1002(cres24) gid=1002(cres24) groups=1002(cres24),0(wheel),5(operator),44(video),193(cups)

In this case, all the groups (wheel,operator,video,cups) are added to the user.


The issue comes in when I read the man page (pw):
Code:
-G    grouplist  Set secondary group memberships for an account.  grouplist
           is a    comma, space, or tab-separated list of group names or
           group numbers.  The user is added to    the groups specified
           in grouplist, and removed from all groups not specified.
           The current login session is    not affected by    group member-
           ship    changes, which only take effect    when the user recon-
           nects.  Note: do not    add a user to their primary group with
           grouplist.

It seems that only separating the group list with commas works as opposed to space or tab-separated list. Of course, I could be and probably are reading too much into the man page or just misinterpreting it.

Please let me know what you think.

73,

Scott - w5plt
 
Case 1 requires you to put your list in quotes: ... -G "operator wheel cups"

What's happening is that the shell parses the command line first before passing the arguments to the command. For the shell, a space is the argument separator. So, the shell splits it up like so, into 6 separate arguments to the pw command:
Code:
"pw" "usermod" "cres24" "-G" "wheel" "operator" "cups"

By adding the quotes around it, you tell the shell to pass everything in them as a single argument, like so:
Code:
"pw" "usermod" "cres24" "-G" "wheel operator cups"

So the pw command sees "wheel operator cups" as a single argument, and it separates it internally into 3 separate arguments for the -G function.

For Case 2, there are no spaces, so the shell parses it like so:
Code:
"pw" "usermod" "cres24" "-G" "wheel,operator,cups"

And the -G function in pw splits it up into the 3 arguments internally.
 
Phoenix,

Thanks for taking the time to explain what I was observing. You explanation is spot on and helped me further my understanding using FreeBSD! I hope this helps others as well..

Thanks again and 73,

Scott - w5plt
 
Back
Top