How do I create user accounts from the Terminal in Mac OS X 10.5?

Solution 1:

Use the dscl command. This example would create the user "luser", like so:

dscl . -create /Users/luser
dscl . -create /Users/luser UserShell /bin/bash
dscl . -create /Users/luser RealName "Lucius Q. User"
dscl . -create /Users/luser UniqueID "1010"
dscl . -create /Users/luser PrimaryGroupID 80
dscl . -create /Users/luser NFSHomeDirectory /Users/luser

You can then use passwd to change the user's password, or use:

dscl . -passwd /Users/luser password

You'll have to create /Users/luser for the user's home directory and change ownership so the user can access it, and be sure that the UniqueID is in fact unique.

This line will add the user to the administrator's group:

dscl . -append /Groups/admin GroupMembership luser

Solution 2:

(This answer should be considered an addendum to fill in some blanks in palmer's procedure)

To pick an unused UniqueID for you new user, you could use:

maxid=$(dscl . -list /Users UniqueID | awk 'BEGIN { max = 500; } { if ($2 > max) max = $2; } END { print max + 1; }')
newid=$((maxid+1))

...then use the sequence of dscl commands palmer gave to create the account, and then create the new user's home directory with:

cp -R /System/Library/User\ Template/English.lproj /Users/luser
chown -R luser:staff /Users/luser
if [[ "$(sw_vers -productVersion)" != 10.[0-5].* ]]; then
    # Set ACL on Drop Box in 10.6 and later
    chmod +a "user:luser allow list,add_file,search,delete,add_subdirectory,delete_child,readattr,writeattr,readextattr,writeextattr,readsecurity,writesecurity,chown,file_inherit,directory_inherit" /Users/luser/Public/Drop\ Box
fi

(there is a createhomedir command, but it didn't work when I tested it.)