Mac OSX adduser script

Typically, it is rather annoying to add a user to OSX through the command line as it requires the administrator to specify each piece of user info one by one, as shown in the Mac Commandline Tools page. As a result, we've worked up an OSX specific script to easily add users through the command line.


 * Simply download this file: [[Media:OSX-adduser.sh| adduser]]
 * Make any necessary execution and permissions changes.
 * It is preferred to save it in /usr/sbin with other administrator tools.

If you're curious to see how it works, here are the contents of the script:

echo "Enter your desired user name: " read USERNAME echo "Enter a full name for this user: " read FULLNAME echo "Enter a password for this user: " read PASSWORD echo "Is this an administrative user? (y/n)" read GROUP_ADD if [ "$GROUP_ADD" = n ] ; then SECONDARY_GROUPS="staff" # for a non-admin user elif [ "$GROUP_ADD" = y ] ; then SECONDARY_GROUPS="admin _lpadmin _appserveradm _appserverusr" # for an admin user else echo "You did not make a valid selection!" fi echo "Creating an unused UID for new user..." if $UID -ne 0 ; then echo "Please run $0 as root." && exit 1; fi MAXID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1) USERID=$((MAXID+1)) echo "Creating necessary files..." dscl. -create /Users/$USERNAME dscl. -create /Users/$USERNAME UserShell /bin/bash dscl. -create /Users/$USERNAME RealName "$FULLNAME" dscl. -create /Users/$USERNAME UniqueID "$USERID" dscl. -create /Users/$USERNAME PrimaryGroupID 20 dscl. -create /Users/$USERNAME NFSHomeDirectory /Users/$USERNAME dscl. -passwd /Users/$USERNAME $PASSWORD echo "Adding user to specified groups..." for GROUP in $SECONDARY_GROUPS ; do    dseditgroup -o edit -t user -a $USERNAME $GROUP done echo "Creating home directory..." createhomedir -c > /dev/null echo "Ignore that last output..." echo "Created user #$USERID: $USERNAME ($FULLNAME)"
 * 1) !/bin/bash
 * 2) Add User OSX Command Line
 * 1) Add User OSX Command Line
 * 1) An easy add user script for Max OSX.
 * 2) Although I wrote this for 10.7 Lion Server, these commands have been the same since 10.5 Leopard.
 * 3) It's pretty simple as it uses and strings together the (rustic and ancient) commands that OSX
 * 4) already uses to add users.
 * 1) === Typically, this is all the info you need to enter ===
 * 1) A list of (secondary) groups the user should belong to
 * 2) This makes the difference between admin and non-admin users.
 * 1) Create a UID that is not currently in use
 * 1) Find out the next available user ID
 * 1) Create the user account by running dscl (normally you would have to do each of these commands one
 * 2) by one in an obnoxious and time consuming way.
 * 1) Add user to any specified groups
 * 1) Create the home directory