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

199

90

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 10.5.8

like to add user to 'admin' and 'wheel' groups

Meltemi

Posted 2010-11-23T02:45:56.840

Reputation: 5 707

Answers

278

sudo dseditgroup -o edit -a $username_to_add -t user admin
sudo dseditgroup -o edit -a $username_to_add -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.

Gordon Davisson

Posted 2010-11-23T02:45:56.840

Reputation: 28 538

4Still valid on osx 10.9.2; copy and paste if you are searching for apache: sudo dseditgroup -o edit -a `whoami` -t user _www – vault – 2014-07-16T13:30:41.133

Still works in 10.10.3 (Yosemite). – Matthew Elvey – 2015-04-28T04:45:50.613

Will these commands affect user's recently groups? – Eray – 2015-06-08T16:51:28.903

@Eray I don't understand the question; can you clarify? – Gordon Davisson – 2015-06-08T19:05:03.920

1Working in El Capitan (10.11.6) as well! – alexw – 2016-11-23T20:16:18.173

This is an excellent resource: https://blog.travismclarke.com/post/osx-cli-group-management/

– Aaron Williams – 2018-08-29T07:46:38.260

13Sorry 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 ? – 夏期劇場 – 2014-03-02T03:46:40.667

4@夏期劇場: Correct. – Gordon Davisson – 2014-03-02T04:41:03.773

2I had to add -p as well so I could enter my password. – Vinicius Pinto – 2014-04-27T18:23:07.553

8

For those who are looking for the same answer to newer versions of Mac OS, i've found out this:

To add a user to a group, you need this command:

$ sudo dscl . append /Groups/wheel GroupMembership username

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.

Brosig

Posted 2010-11-23T02:45:56.840

Reputation: 181

Can anyone confirm whether this works on 10.14 (mojave) ? – Sary – 2019-02-08T08:28:18.450

1It works for me on 10.14.4. – mwfearnley – 2019-05-14T10:44:47.693

Great. works with Mojave 10.14.5. Thanks for sharing! – LeOn - Han Li – 2019-06-04T19:14:54.967

5

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

user3463

Posted 2010-11-23T02:45:56.840

Reputation:

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... ?!? – Meltemi – 2010-11-23T03:01:32.497

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. – None – 2010-11-23T03:13:57.147

Did you read through the man pages for dscl at all? – None – 2010-11-23T03:17:37.937