Apple - What steps are needed to create a new user from the command line?

Here is a shell script I wrote at work to handle this as part of the NetInstall process (creating a local administrator account automatically during imaging process).

#!/bin/sh
. /etc/rc.common
dscl . create /Users/administrator
dscl . create /Users/administrator RealName "Administrator Account"
dscl . create /Users/administrator hint "Password Hint"
dscl . create /Users/administrator picture "/Path/To/Picture.png"
dscl . passwd /Users/administrator thisistheaccountpassword
dscl . create /Users/administrator UniqueID 501
dscl . create /Users/administrator PrimaryGroupID 80
dscl . create /Users/administrator UserShell /bin/bash
dscl . create /Users/administrator NFSHomeDirectory /Users/administrator
cp -R /System/Library/User\ Template/English.lproj /Users/administrator
chown -R administrator:staff /Users/administrator

Some notes to mention:

  • I have this saved as an executable ".sh" file.
  • Since it executes during NetInstall it runs as root, and needs to run as root to work properly. You can also subtract the first two lines, add a "sudo" to the beginning of each subsequent line, and manually run these as individual commands in Terminal.
  • Modify UniqueID from 501 to a number that you will know is safe on all systems (501 is taken by the first account created on a Mac, generally something higher like 550 will probably be safe, depending on how many users you have on your system).
  • PrimaryGroupID of 80 creates an Admin user. Change to PrimaryGroupID of 20 to create a Standard user.
  • I've imaged well over 50 Macs this way with no issues. I use this account to run commands via SSH, to push out patches via ARD and to do local desk-side administration.

To further automate this, the following line can be used to get the next "available" user id if you are running on a mac which already has users set up.

LastID=`dscl . -list /Users UniqueID | awk '{print $2}' | sort -n | tail -1`

NextID=$((LastID + 1))

Then, the corresponding line in bispymusic's answer above could be changed:

 dscl . create /Users/administrator UniqueID $NextID

I have some updates on the above answer.

dscl / -append /Groups/admin GroupMembership newUserName

This command can used for making the user have Administrative access. If this command is not provided the user will automatically will be set as standard user.

dscl . create /Users/newUserName PrimaryGroupID 80

This sets user's primary group id. 80 means admin and 20 means staff. And setting to 20 will not make the user standard. Unless the first command is not mentioned.