Skip to content

Instantly share code, notes, and snippets.

@chizmw
Last active August 29, 2015 14:24
Show Gist options
  • Save chizmw/e6f6d6a727814cfdf368 to your computer and use it in GitHub Desktop.
Save chizmw/e6f6d6a727814cfdf368 to your computer and use it in GitHub Desktop.
mac-adduser
#!/bin/bash
# username needs to be specified
if [ -z "$1" ]; then
echo "usage: $(basename $0) username";
exit;
fi
username=$1;
# we need to be running as root
if [ $(id -u) -ne 0 ]; then
echo "usage: sudo $(basename $0) username";
exit;
fi
# we don't want to try to create a username that already exists
if [ -n "$(id -u "perlydev" 2>/dev/null)" ]; then
echo "'$username' already exists";
id -p $username;
exit;
fi
. /etc/rc.common
# NAP macbooks have AD accounts with silly high ID values; let's ignore those
LastID=`dscl . -list /Users UniqueID | awk '{if ($2 < 50000) print $2}' | sort -n | tail -1`
NextID=$((LastID + 1))
# generate a random password
Password=$(env LC_CTYPE=C LC_ALL=C tr -dc 'a-zA-Z0-9' < /dev/urandom |head -c10);
# let's give them a random profile photo
curl -s -o /tmp/profile.jpg http://lorempixel.com/400/400/people/
# create the user account, with some 'fluff'
dscl . create /Users/$username
dscl . create /Users/$username RealName "$username's account"
dscl . create /Users/$username hint "Password Hint"
dscl . create /Users/$username picture "/tmp/profile.jpg"
dscl . passwd /Users/$username $Password
dscl . create /Users/$username UniqueID $NextID
dscl . create /Users/$username PrimaryGroupID $NextID
dscl . create /Users/$username UserShell /bin/bash
dscl . create /Users/$username NFSHomeDirectory /Users/$username
createhomedir -c -u $username
echo "$username created with password: $Password";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment