Skip to content

Instantly share code, notes, and snippets.

@geoffrepoli
Last active October 6, 2016 18:52
Show Gist options
  • Save geoffrepoli/0e5cdd1a9a34776e5d730ecfd72990a2 to your computer and use it in GitHub Desktop.
Save geoffrepoli/0e5cdd1a9a34776e5d730ecfd72990a2 to your computer and use it in GitHub Desktop.
Changes HostName and LocalHostName to match ComputerName while accounting for non-ASCII characters
#!/bin/bash
comp_name=$(scutil --get ComputerName)
if [[ "$comp_name" = *[![:ascii:]]* ]]; then
echo "Error: Computer name contains non-ASCII characters. Please modify with the command \`scutil --set ComputerName <value>\` before running this script"
exit 1
else
host_name=$(echo "$comp_name" | sed -e "s/'//g;s/ /-/g;s/\///g" | awk '{print tolower($0)}')
echo "Changing hostname and Bonjour name to $host_name ..."
scutil --set LocalHostName "$host_name"
scutil --set HostName "$host_name"
echo "Success: hostname and Bonjour name updated"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment