Why is my hostname resolution taking so long? Several months ago I noticed that my text editor (emacs) and IDE (IntelliJ) were taking a really long time to start up. The time appeared to vary based on the DNS servers OS X was using.

I was able to isolate the issue when a project's test suite was running slowly. I found the (higher level) culprit to be a call to socket.getfqdn(). 

Running the following command in the terminal on OS X 10.10.2, demonstrates the problem:

$ time python -c 'import socket; socket.getfqdn()'
python -c 'import socket; socket.getfqdn()'  0.02s user 0.00s system 0% cpu 5.122 total


I traced the code that runs when socket.getfqdn() is called and the delay is caused by getaddrinfo(3). I wrote a small program that isolates the problem and gai_strerror(3) provides this message:

$ time ./hostinfo
Hostname: MacBook-Pro.local
getaddrinfo: nodename nor servname provided, or not known
./hostinfo  0.00s user 0.00s system 0% cpu 5.101 total


It seems like the delay is waiting for the DNS query to time out. The above results were using Google's public DNS servers. If I use my ISP's DNS servers, however, the time increases to 30 seconds:

$ time python -c 'import socket; socket.getfqdn()'
python -c 'import socket; socket.getfqdn()'  0.01s user 0.01s system 0% cpu 30.114 total


(curiously the C program hostinfo still takes just above 5 seconds)

What is causing this issue? Is my hostname invalid or causing problems?

$ hostname
MacBook-Pro.local


This problem doesn't happen on a Macbook Air on the same network.

The major difference I can see is that on the problematic machine, the following DNS confiugration is listed:

$ scutil --dns
DNS configuration

resolver #1
  search domain[0] : Home
  nameserver[0] : 8.8.8.8
  nameserver[1] : 8.8.4.4
  flags    : Request A records
  reach    : Reachable

DNS configuration (for scoped queries)

resolver #1
  search domain[0] : Home
  nameserver[0] : 8.8.8.8
  nameserver[1] : 8.8.4.4
  if_index : 4 (en0)
  flags    : Scoped, Request A records
  reach    : Reachable


On the Macbook Air, several extra entries are included that relate to mDNS. For example:

resolver #2
  domain   : local
  options  : mdns
  timeout  : 5
  flags    : Request A records
  order    : 300000


This seems to be significant. Interestingly, the timeout listed above is about the same as the runtimes above.

I should note that I'm connected to the internet using WiFi and the problem only exists when trying to resolve my computer's host name.
I was able to fix this issue by explicitly setting the HostName using scutil to be the same value as LocalHostName:

$ scutil --set HostName $(scutil --get LocalHostName)


Now:

$ sudo scutil --set HostName MacBook-Pro
$ time python -c 'import socket; print(socket.getfqdn())'
MacBook-Pro
python -c 'import socket; print(socket.getfqdn())'  0.01s user 0.00s system 86% cpu 0.016 total


I was confused before because of the following:

$ scutil --get LocalHostName
MacBook-Pro

$ hostname
MacBook-Pro.local


But:

$ scutil --get HostName
HostName: not set