Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Adds a user via command line, and sets the machine as "Setup Done"
- # Ensure that it runs as root
- if [ "$(id -u)" != "0" ]; then
- echo "${0} must be run as root, or using sudo"
- exit 1
- fi
- if [ -n "${1}" -a -n "${2}" -a -n "${3}" -a -n "${4}" ]; then
- echo "Using parameters from command line:"
- echo " USERNAME: ${1}"
- echo " FULLNAME: ${2}"
- echo " PASSWORD: ****"
- echo " EXTRA_GROUPS: ${4}"
- elif [ -n "${1}" -o -n "${2}" -o -n "${3}" -o -n "${4}" ]; then
- echo "Usage: ${0} [<USERNAME> <FULLNAME> <PASSWORD> <EXTRA_GROUPS>]"
- exit 1
- fi
- # Lower-cases a string
- function to_lower { echo "${1}" | tr '[:upper:]' '[:lower:]'; }
- # Required information
- echo "Creating a new user..."
- USERNAME="${1}"
- while [ -z "${USERNAME}" ]; do
- echo -n "Username: "
- read USERNAME
- USERNAME="$(to_lower "${USERNAME}")"
- if [ -n "${USERNAME}" -a -d "/Users/${USERNAME}" ]; then
- echo "Username '${USERNAME}' already exists"
- USERNAME=""
- fi
- done
- FULLNAME="${2}"
- while [ -z "${FULLNAME}" ]; do
- echo -n "Full name: "
- read FULLNAME
- done
- PASSWORD="${3}"
- while [ -z "${PASSWORD}" ]; do
- echo -n "Password: "
- read -s PASSWD1
- echo ""
- echo -n "Retype password: "
- read -s PASSWD2
- echo ""
- if [ "${PASSWD1}" == "${PASSWD2}" ]; then
- PASSWORD="${PASSWD1}"
- else
- echo "Passwords do not match"
- fi
- done
- EXTRA_GROUPS="${4}"
- while [ -z "${EXTRA_GROUPS}" ]; do
- echo -n "Is this an administrative user? (y/n) "
- read ADMIN
- ADMIN="$(to_lower "${ADMIN}")"
- if [ "${ADMIN}" == "y" ]; then
- EXTRA_GROUPS="admin"
- elif [ "${ADMIN}" == "n" ]; then
- EXTRA_GROUPS="staff"
- else
- echo "Please enter 'y' or 'n'"
- fi
- done
- # Create a UID that is not currently in use
- echo "Creating an unused UID for new user..."
- MAXID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)
- if [ ${MAXID} -lt 500 ]; then MAXID=500; fi
- USERID=$((MAXID+1))
- # Create the user account by running dscl
- 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}
- # Add user to any specified groups
- echo "Adding user to specified groups..."
- for GROUP in ${EXTRA_GROUPS} ; do
- dseditgroup -o edit -t user -a ${USERNAME} ${GROUP}
- if [ "${GROUP}" == "admin" -a ! -f "/var/db/.AppleSetupDone" ]; then
- touch /var/db/.AppleSetupDone
- fi
- done
- # Create the home directory
- echo "Creating home directory..."
- createhomedir -c 2>&1 | grep -v "shell-init"
- # Patch to not prompt for iCloud
- PDIR="/Users/${USERNAME}/Library/Preferences"
- SUP="${PDIR}/com.apple.SetupAssistant"
- [ -d "${PDIR}" ] || { sudo -u ${USERNAME} -- mkdir -p "${PDIR}"; }
- defaults write "${SUP}" DidSeeCloudSetup -bool TRUE
- defaults write "${SUP}" GestureMovieSeen none
- defaults write "${SUP}" LastSeenCloudProductVersion "$(sw_vers -productVersion)"
- chown ${USERNAME}:staff "${SUP}.plist"
- echo "Created user #${USERID}: ${USERNAME} (${FULLNAME})"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement