FreeBSD/FreeNAS: How to change group for multiple users

freebsdgroupsshelltruenas

I need to change group membership for a bunch of users.

How do I

  1. list all users?
  2. change multiple users to nogroup primary group?
  3. add secondary/auxiliary groups to multiple users?

Basically I need to change all the users that are currently in the clients group to nogroup and add clients as an auxiliary group to them.

Can this be done via shell without individually editing each user?

Best Answer

  1. list all users?
pw usershow -a
  1. change multiple users to nogroup primary group?

There's a hundred different ways to do this... I would:

foreach u ("list" "of" "users")
    pw usermod -n $u -g NewPrimaryGroup
end

You could even get the list of users from a subcommand, like pw groupshow OldGroupName | sed -e "s/.*://" -e "s/,/\ /"

  1. add secondary/auxiliary groups to multiple users?

Again, a few ways to do this...

If you know the full list of secondary groups that the users should be in:

pw usermod -n UserName -G "Secondary Group List"

If you don't know the full list, or just want to add users to a group:

pw groupmod -n SecondaryGroup -m NewUserName

And removing is similairly

pw groupmod -n SecondaryGroup -d OldUserName

Basically I need to change all the users that are currently in the clients group to nogroup and add clients as an auxiliary group to them.

foreach u (`pw groupshow clients | sed -e "s/.*://" -e "s/,/\ /"`)
    pw usermod $u -g nogroup
    pw groupmod clients -m $u
end

(The above snippet is untested, but looks good after 3 seconds of double-checking, also written for csh as that's FreeBSD's default shell for users)

Related Topic