Last active
January 6, 2025 10:25
-
-
Save vijinho/8a2cf9641926a41cae5e7990f8d55d51 to your computer and use it in GitHub Desktop.
script to set the hostname in macOS using scutil for HostName, LocalHostname, ComputerName to the same value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to display usage information | |
usage() { | |
echo "Usage: $0 <new-hostname>" | |
exit 1 | |
} | |
# Check if the user has provided a hostname argument | |
if [ $# -eq 0 ]; then | |
usage | |
fi | |
NEW_HOSTNAME=$1 | |
# Ensure the script is run with sudo for administrative privileges | |
if [[ $EUID -ne 0 ]]; then | |
echo "This script must be run as root." | |
sudo "$0" "$@" | |
exit $? | |
fi | |
# Set the hostname, local hostname, and computer name | |
sudo scutil --set HostName "$NEW_HOSTNAME" | |
sudo scutil --set LocalHostName "$NEW_HOSTNAME" | |
sudo scutil --set ComputerName "$NEW_HOSTNAME" | |
# Flush DNS cache to ensure new settings take effect | |
dscacheutil -flushcache | |
echo "Hostname updated successfully to $NEW_HOSTNAME" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment