Since I couldn't find documentation on how a Mac machine gets its name, I thought I'd write up these notes for my own reference later.
A macOS machine has a few different names, which might or might not be equal.
Every machine has a pretty user-facing name. This is the ComputerName
.
It's what you access by System Preferences / Sharing / Computer Name, by networksetup -getcomputername
, or by scutil --get ComputerName
. For instance, you could name your machine "apollo".
Every machine also has a LocalHostName
. This is the name used to form the system's multicast DNS name, which will be a string like "apollo.local.". This is the name seen by other machines via AirDrop and Bonjour-distributed services.
If it's not set explicitly, this name is generated by character-simplifying the ComputerName
.
Every machine may also have what I will call a network name, which is a plain old unicast DNS name which it recommends for reaching the system on the local network.
This is the name which appears in macOS's Sharing pane in System Preferences, as part of the SSH connection string for Remote Login, the VNC URL for Screen Sharing, and the SMB URL for File Sharing. (This name may also define the NetBIOS name reported as currently being used -- I'm not sure.)
Since this is the name that macOS advises you to give other systems in order for them to reach you, what matters is that this is what will work for those systems as opposed to what this machine wants to be called. So this network name is not defined by local configurations but by the surrounding network environment.
Specifically, macOS seems to use the following rule. On startup, a machine decides its network name by using the first item on this list that is available:
- The name it was assigned by its DHCP server (I'm not 100% sure of this.)
- The name returned by doing a reverse-DNS lookup on its IP address, from its DNS server
If there's a network name, then the Sharing pane recommends using it. Otherwise, it recommends using a numerical IP address.
Finally, every machine also has a hostname.
This is the name returned by the hostname -s
command, which is also the value of bash's $HOSTNAME
variable (therefore appearing on the prompt). The rule for determining this seems to be to take the first of these which is available:
- The inferred network name
- The value of
HostName
- The value of
LocalHostName
These various names live in various places. This script will expose them all:
#!/bin/sh
echo
echo '==== System Configuration, ComputerName:'
scutil --get ComputerName
echo
echo '==== System Configuration, LocalHostName:'
scutil --get LocalHostName
echo
echo '==== System Configuration, HostName:'
scutil --get HostName
echo
echo '==== hostname command:'
hostname
echo
echo '==== System Configuration, NetBIOS name:'
echo 'show State:/Network/NetBIOS foo' | scutil
echo
echo '==== SystemConfiguration plist file reports various Name':
plutil -p /Library/Preferences/SystemConfiguration/preferences.plist | grep '"HostName"'
plutil -p /Library/Preferences/SystemConfiguration/preferences.plist | grep '"LocalHostName"'
plutil -p /Library/Preferences/SystemConfiguration/preferences.plist | grep '"ComputerName"'
echo
echo '==== nvram reports computer-name'
nvram -p | grep computer-name
echo
echo '==== System Configuration, Setup:/System :'
echo 'show Setup:/System foo' | scutil
echo
echo '==== System Configuration, Setup:/Network/HostNames :'
echo 'show Setup:/Network/HostNames foo' | scutil
# echo
# echo '==== System Configuration, Setup:/Network/HostNames/LocalHostName :'
# echo 'show Setup:/Network/HostNames LocalHostName' | scutil
echo
echo '==== defaults system, value for NetBIOSName:'
defaults read /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName
echo
echo '==== NetworkSetup reports ComputerName:'
networksetup -getcomputername
echo
echo '==== Active Directory computer object name:'
dsconfigad -show | grep -i 'Computer' | awk -F "= " '{printf $NF}'
In summary:
- If you want to rename your system, first try doing it from System Preferences / Sharing / Computer Name.
- If this doesn't work, and you're still seeing an undesired name for your sharing services, then you probably need to reconfigure your router to give you a different name.