How to add user to a group from Mac OS X command line?

275,803

Solution 1

sudo dseditgroup -o edit -a john -t user admin
sudo dseditgroup -o edit -a john -t user wheel

It's also possible to do this with dscl, but to do it properly you need to both add the user's short name to the group's GroupMembership list, and add the user's GeneratedUID to the group's GroupMembers list. dseditgroup takes care of both in a single operation.

Solution 2

For those who are looking for the same answer to newer versions of Mac OS, I've found this: To add a user to a group, you need this command ($USER is the current logged-in user) :

$ sudo dscl . append /Groups/wheel GroupMembership $USER

I was trying to add my user to the wheel group, to be able to manipulate the /Library/WebServer/Documents folder. Besides that, I had to change the permissions to that folder, as by default it is 755. I've changed it to 775 with:

$ sudo chmod -R 775 /Library/WebServer/Documents

This way I can manipulate the folder content without changing the owner of the folder.

ps. Still working on Catalina (10.15.3)

Solution 3

Check out this link:

http://osxdaily.com/2007/10/29/how-to-add-a-user-from-the-os-x-command-line-works-with-leopard/

Adding a user is something easily accomplished using the built in GUI tools that ship with OS X, however any power user can appreciate the possible efficiency gained from using the command line. So in the spirit of efficiency here are the steps necessary to add a user to your Mac OS X system all with our good friend, Terminal.app.

The important bit is here:

Create and set the user’s group ID property.

dscl / -create /Users/toddharris PrimaryGroupID 1000
Share:
275,803
Meltemi
Author by

Meltemi

Updated on September 17, 2022

Comments

  • Meltemi
    Meltemi almost 2 years

    I am trying to add a user to a group from the command line but can't figure out how. Specifically this is on Mac OS X Server version 10.5.8.

    The user is 'john', the groups are 'admin' and 'wheel'.

  • Meltemi
    Meltemi over 13 years
    tried it with sudo dscl . -append /Groups/admin GroupMembership username and though it added the user to "admin" but it also added a bunch of other groups like com.apple.sharepoint.group.1 and com.apple.access_screensharing ect... ?!?
  • gorootde
    gorootde over 13 years
    That is strange. On the other hand, perhaps these are groups that have been associated with that user, or it somehow inherited them from elsewhere.
  • gorootde
    gorootde over 13 years
    Did you read through the man pages for dscl at all?
  • Rohit Muneshwar
    Rohit Muneshwar over 10 years
    Sorry i'm not too cleared with the usage. Lets say i want to add the username bob into wheel, will it be sudo dseditgroup -o edit -a bob -t user wheel ?
  • Gordon Davisson
    Gordon Davisson over 10 years
    @夏期劇場: Correct.
  • Christoffel de Gruyter
    Christoffel de Gruyter about 10 years
    I had to add -p as well so I could enter my password.
  • vault
    vault almost 10 years
    Still valid on osx 10.9.2; copy and paste if you are searching for apache: sudo dseditgroup -o edit -a `whoami` -t user _www
  • WHO's NoToOldRx4CovidIsMurder
    WHO's NoToOldRx4CovidIsMurder about 9 years
    Still works in 10.10.3 (Yosemite).
  • Eray
    Eray about 9 years
    Will these commands affect user's recently groups?
  • Gordon Davisson
    Gordon Davisson about 9 years
    @Eray I don't understand the question; can you clarify?
  • alexw
    alexw over 7 years
    Working in El Capitan (10.11.6) as well!
  • Aaron Williams
    Aaron Williams almost 6 years
  • Sary
    Sary over 5 years
    Can anyone confirm whether this works on 10.14 (mojave) ?
  • mwfearnley
    mwfearnley about 5 years
    It works for me on 10.14.4.
  • LeOn - Han Li
    LeOn - Han Li about 5 years
    Great. works with Mojave 10.14.5. Thanks for sharing!
  • ROOT
    ROOT about 4 years
    Thanks! worked for me.
  • Gwyneth Llewelyn
    Gwyneth Llewelyn over 3 years
    Well well. What do you know. It still works under macOS Big Sur 11.0 Beta (20A5384c)! Thanks!