Skip to content

Instantly share code, notes, and snippets.

@ChristopherA
Last active October 25, 2022 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristopherA/647e8ae63fbfd9c5792b75c5d56c9764 to your computer and use it in GitHub Desktop.
Save ChristopherA/647e8ae63fbfd9c5792b75c5d56c9764 to your computer and use it in GitHub Desktop.
Setup new macOS machine

Setup macOS machine

My scripts for most the basic version of my standard configuration of macOS machines for development, in particular for VMware Fusion Pro

Setup first admin account

During the post-install setup, name the first account <Computername> Administrator with the shortname of admin, and a secure password.

Then open terminal, and use this command. (NOTE: curl piped to sudo bash is a bad idea, but as this is the first script on a new computer it isn't the worst choice. But be review the source script is what you want to execute!)

This script sets up the computer name and some other preferences:

curl -L https://gist.githubusercontent.com/ChristopherA/647e8ae63fbfd9c5792b75c5d56c9764/raw/1-setup-initial-admin-account.sh | sudo bash
#!/bin/bash
# Bash Strict mode as per:
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -o errexit
set -o nounset
set -o pipefail
# This script, run from the initial admin user, will create a 2nd user
# ==== Check to see if being run as root
if [[ $UID -ne 0 ]]; then echo "This script needs root permissions -- please use sudo $0" && exit 1; fi
###functions
function machinename () {
osascript <<EOT
tell application "System Events"
activate
set nameentry to text returned of (display dialog "Please Input New Computer Name" default answer "" with icon 2)
end tell
EOT
}
function renameComputer(){
#Set New Computer Name
echo "The New Computer name is: $ComputerName"
scutil --set HostName $ComputerName
scutil --set LocalHostName $ComputerName
scutil --set ComputerName $ComputerName
defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string $ComputerName
echo Rename Successful
}
###Script
ComputerName=$(machinename)
renameComputer
exit 0
#!/bin/bash
# Bash Strict mode as per:
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -o errexit
set -o nounset
set -o pipefail
# This script, run from the initial admin user, will create a 2nd user
# ==== Check to see if being run as root
if [[ $UID -ne 0 ]]; then echo "This script needs root permissions -- please use sudo $0" && exit 1; fi
# === Typically, this is all you need to edit ===
USERNAME="christophera"
FULLNAME="Christopher Allen"
PASSWORD=$(osascript -e 'Tell application "System Events" to display dialog "Enter new account password:" with title "New Password" with hidden answer default answer ""' -e 'text returned of result' 2>/dev/null)
# A list of (secondary) groups the user should belong to
# This makes the difference between admin and non-admin users.
# Leave only one uncommented
#SECONDARY_GROUPS="staff" # for a non-admin user
SECONDARY_GROUPS="admin _lpadmin _appserveradm _appserverusr" # for an admin user
# Find out the next available user ID
#MAXID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)
#USERID=$((MAXID+1))
USERID=501
# Create the user account
dscl . -create /Users/$USERNAME
dscl . -create /Users/$USERNAME UserShell /bin/zsh
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 . -append /Users/$USERNAME Picture "/Library/User Pictures/Flowers/Lotus.tif"
dscl . -passwd /Users/$USERNAME $PASSWORD
# Add use to any specified groups
for GROUP in $SECONDARY_GROUPS ; do
dseditgroup -o edit -t user -a $USERNAME $GROUP
done
#Create the home directory
createhomedir -c -u $USERNAME
echo "Created user #$USERID: $USERNAME ($FULLNAME)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment