8

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:

System Preferences showing Sharing settings Terminal showing hostname informations

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.

Router configuration with erroneous settings

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.

  • Did the host david-macbook-pro ever have the name david-MBP? – klanomath Oct 20 '15 at 8:11
  • Yes, I wrote it a bit contorted: when i set up the Mac, I did use that host name. Over the course of time, I changed it numerous times with any common command, and on the machine it changed. But the one that get read by routers really seems to stick. – mcdado Oct 20 '15 at 8:14
  • So it's not a OS X problem but one of the router OS (probably some Linux descendant). Once the hostname is set the router doesn't update it properly in its hosts database! – klanomath Oct 20 '15 at 8:21
  • My last point in the question though is that this router met my Mac like a month or so ago, but my Mac has been in this setup for months/years. So there is some setting somewhere in OS X lurking around. – mcdado Oct 20 '15 at 8:24
  • Check your nvram with nvram -p | grep david – klanomath Oct 20 '15 at 8:34
4

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


1 SLP/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.

2 Bonjour is Apple's implementation of zero-configuration networking, a group of technologies that includes service discovery, address assignment, and hostname resolution.

2

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:

dhcp client id

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .