Incorrect hostname keeps appearing in sharing preferences [duplicate]
There are tons of questions about changing a Mac hostname, and while I went through most of them, I could never find this nuanced answer:
Why do routers always seems to receive the first hostname that was ever set to a machine? Let me show you with these screenshots how this Mac is supposedly correctly setup according to various answers on this site:
Now, when i set up this computer years ago, I think I chose the hostname david-MBP
, and this seems to be what various routers get to this day and sometimes return to the Mac for localhost operations. Later I changed it with the various Terminal commands, and the Mac knows it, but it seems to stick around when communicating with routers.
So now my question is where do OS X keep this old value? Because since I connected with this router in the screenshot above, I never had that hostname, so it's no DHCP cache or anything like that.
There are three different hostnames that macOS/OS X uses:
- ComputerName
- HostName
- LocalHostName
Each of these values can be found in /Library/Preferences/SystemConfiguration/preferences.plist
ComputerName
scutil --get ComputerName
This is Apple "friendly" name for the computer and can be found in System Preferences >> Sharing >> Computer Name. According to the man page for scutil
, it is described as "The user-friendly name for the system."
According to Apple, the ComputerName is The AppleTalk name and the default name used for SLP/DA1. It's a zero-configuraiton protocol that allows you to set up a simple network without a DNS/DHCP server but allow you to reach the different resources via a host name.
HostName
scutil --get HostName
According to the scutil
man page, the HostName is "the name associated with hostname(1) and gethostname(3)". This name can come from any of the following sources:
- The name provided by the DHCP or BootP server for the primary IP address
- The first name returned by a reverse DNS (address-to-name) query for the primary IP address
- The local hostname
- The name “localhost”
(In my virgin install of El Capitan, the HostName
defaulted to LocalHostName
)
LocalHostName
scutil --get LocalHostName
This is for Bonjour2 services on the LAN. This is how, on a small LAN without DNS/DHCP, Apple computers can discover and interact with resources such as file sharing on other machines, printers, etc. This name gets set when you initially configure your Mac.
TL;DR
It is using "the first name you ever set" because during initial setup, it set the LocalHostName which it assumed would forever be. When you re-configured using the GUI, you changed the ComputerName which did not modify the LocalHostName.
If you wish to fix this in your router, simply set your LocalHostName using the command:
scutil --set LocalHostName MyNewComputerName.domain
1SLP/DA Service Location Protocol/Directory Agent. The Service Location Protocol is a service discovery protocol that allows computers and other devices to find services in a local area network without prior configuration.
2Bonjour is Apple's implementation of zero-configuration networking, a group of technologies that includes service discovery, address assignment, and hostname resolution.
The routers will usually acquire the name of the computer when the IP address has been assigned, which is before any sharing or broadcasts are done. The computer name is/can be defined in Network Preferences under DHCP Client ID and is independent from any other computer setting. Essentially it means "when asking for an IP address, identify yourself as /this/". Check if you have it set like this: