Skip to content

Instantly share code, notes, and snippets.

@cmfsotelo
Forked from jhbush/createadminuser.sh
Last active March 6, 2020 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cmfsotelo/c205b11b690f526d73ec69437e21edf1 to your computer and use it in GitHub Desktop.
Save cmfsotelo/c205b11b690f526d73ec69437e21edf1 to your computer and use it in GitHub Desktop.
Create User Script
#!/bin/bash
# This script creates a user account under Mac OS X
usage() {
echo " Synopsis"
echo ' create-user userName userFullName userPassword groupId'
echo ""
echo ""
echo " Description"
echo " Creates a new user with the given params and attributes to the desired group"
}
UName=$1
FullName=$2
Password=$3
GroupId=$4
if [[ $UID -ne 0 ]]; then echo "Please run $0 as root." && exit 1; fi
if [ $# -lt 4 ]; then
echo "Wrong arguments supplied!"
usage
exit 2
fi
# Find out the next available user ID
MAXID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)
UserId=$((MAXID+1))
# Create the user account
echo "Creating user $UName"
dscl . -create /Users/"$UName"
echo "Setting default shell to /bin/bash"
dscl . -create /Users/"$UName" UserShell /bin/bash
echo "Setting RealName as $FullName"
dscl . -create /Users/"$UName" RealName "$FullName"
echo "Setting UniqueId as $UserId"
dscl . -create /Users/"$UName" UniqueID "$UserId"
echo "Setting PrimaryGroupID as $GroupId"
dscl . -create /Users/"$UName" PrimaryGroupID "$GroupId"
echo "Creating home directory /Users/$UName"
dscl . -create /Users/"$UName" NFSHomeDirectory /Users/"$UName"
createhomedir -c -u "$UName"
echo "Defining user password"
dscl . -passwd /Users/"$UName" "$Password"
if [ $? != 0 ] ; then
echo "Reverting user creation. Deleting user /Users/$UName"
dscl . delete /Users/"$UName"
exit 1
fi
echo ""
echo "Created user #$UserId: $UName ($FullName)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment