18

With Linux/Unix, I can use useradd -G developers vivek to add vivek to the developer group. How can I do the same thing with Mac OS X?

Then, how do I check if vivek is in developers group?

2

3 Answers 3

15

I've used this one-liner:

sudo dscl localhost -append /Local/Default/Groups/thegroupname GroupMembership theusername
32

To add a user to a group:

dseditgroup -o edit -a vivek -t user developers

(Note: you can also do this with dscl as in @hobs' answer, but you really should both add the user's short name to the GroupMembership list, and also add the user's GeneratedUID to the GroupMembers list. dseditgroup does both with a single operation.)

To check membership:

dseditgroup -o checkmember -m vivek developers

Or:

dsmemberutil checkmembership -U vivek -G developers

To remove a user from a group:

dseditgroup -o edit -d vivek -t user developers

(Again, you can do this with dscl, but need to remove the user from both GroupMembership and GroupMembers. dseditgroup does both.)

4
  • Confirmed. dscl only adds without checking user is in group already. it just adds. & it may create duplicate membership for the same user on group.
    – risyasin
    Apr 10, 2015 at 7:38
  • 1
    Ok this is frustrating. When I try to use the first command it says "Username and password must be provided". When I use "-u michael" the man page says it should prompt me for a password, but it instead prints the previous message again!
    – Michael
    Sep 16, 2016 at 19:49
  • 1
    @Michael if you specify an admin user with -u, it should prompt for the password. What version of OS X are you using? Also, try with sudo (i.e. sudo dseditgroup -o ...) and that should work. Sep 17, 2016 at 16:38
  • dseditgroup -o edit -a vivek -t user developers is the key. Lot of people suggest dseditgroup -o edit -a vivek -t developers but only dseditgroup -o edit -a vivek -t user developers is worked Oct 14, 2017 at 4:29
8

Two ways: From the terminal

Everything here should be done on the command line First start dscl like this:

$ dscl localhost

Then cd to the Groups node:

cd /Local/Default/Groups

You can see all the groups using ls. Once you have found the group you are interested in, you can view all its properties with cat groupname. When you are ready to add a user (username) to the group (groupname), use the following command:

append groupname GroupMembership username

If you want to remove a member from a group, use the following command (be careful!):

delete groupname GroupMembership username

When you are finished with your changes, exit dscl by typing exit.

And the apple official way:

Choose Apple menu > System Preferences, and then click Accounts. Open Accounts preferences Step 2

If the preferences pane is locked, click the lock icon and enter an administrator name and password. Step 3

Click the Add (+) button, located below the list of accounts, to create a new account. Step 4

Select a Group from the New Account pop-up menu. Step 5

Enter a name for the group, and then click Create Group. Step 6

Select the checkboxes by the accounts to be members in the group.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .