Monday, June 13, 2011

Create user account from the OS X command line

Here is a procedure for adding a new user in OS X from the command line.
The problem is that there is no useradd or adduser in OS X, so the user must
be added to the Directory Service by hand.

  1. First make sure to find an unused uid (unique ID) for the user,
    dscl . -list /Users UniqueID
    will list all the existing users, an unused number above 500 is good.

    In general to list a property from the directory use:
    dscl . -list {users|groups} {u|g}id
    or to see the id's of an existing user use:
    id auser

  2. The Add the directory entry:
    dscl . -create /Users/luser
    dscl . -create /Users/luser UserShell /bin/bash
    dscl . -create /Users/luser RealName "Lucius Q. User"
    dscl . -create /Users/luser UniqueID "503"
    dscl . -create /Users/luser PrimaryGroupID 20
    dscl . -create /Users/luser NFSHomeDirectory /Users/luser

    The PrimaryGroupID=20 is usually staff. 80 is admin, it can be added with:
    dscl . -append /Groups/admin GroupMembership luser

  3. Set the password:
    dscl . -passwd /Users/luser password
    passwd luser

  4. Finally, create the home directory with the appropriate permissions:
    mkdir /Users/luser
    chown luser:staff luser

    or even better, create and populate the directory with
    createhomedir -c -u luser

Further reading:

No comments:

Post a Comment