Skip to content

Instantly share code, notes, and snippets.

@subfission
Last active September 8, 2019 08:33
Show Gist options
  • Select an option

  • Save subfission/abcc2a20df288b5c19d903858bea891e to your computer and use it in GitHub Desktop.

Select an option

Save subfission/abcc2a20df288b5c19d903858bea891e to your computer and use it in GitHub Desktop.
Sets the hostname for Mac OS in all the various places and ways.
#!/bin/bash
#
# set_macos_hostname.sh
# Sets the hostname for Mac OS in all the various places and ways.
#
# Author: Zach Jetson
#
usage() {
printf "[!] Error: missing hostname in argument\n\n"
printf "usage: $0 HOSTNAME\n\n"
echo "HOSTNAME The hostname to set for your computer"
exit 1
}
if [ -z "$1" ]; then
usage
else
HOSTNAME=$1
fi
if [ "$EUID" -ne 0 ]; then
echo "Update check requires root privileges"
exec sudo /bin/bash "$0" "$1"
exit 1
fi
scutil --set HostName "$HOSTNAME" 2>/dev/null
scutil --set LocalHostName "$HOSTNAME" 2>/dev/null
scutil --set ComputerName "$HOSTNAME" 2>/dev/null
dscacheutil -flushcache
echo "New hostname set: $(hostname -f)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment