MacOS – El Capitan: DNS cache won’t flush

dnsmacos

I'm trying to get my El Capitan's (10.11.6) DNS cache to flush, to try and solve slow localhost domain name resolution, but nothing I do has any effect. My computer name, hostname and hard disk names used to be as follows:

Santa's MacBook Pro
santas-macbook-pro-2.local
Santa's MacBook

(I didn't choose to put the "-2" at the end of the hostname; that's what the Mac had in the Sharing preference pane.)

I tried editing the etc/hosts file as follows:

127.0.0.1    localhost santas-macbook-pro-2.local
255.255.255.255    broadcasthost
::1             localhost santas-macbook-pro-2.local
fe80::1%lo0    localhost

but even after a restart the resolution was still as slow as ever.

Tried renaming the computer and hostname to trial and trial.local (updating etc/hosts accordingly) and the hard disk to tt. Got OnyX to flush various caches including system and kernel extensions, restarted, still slow.

In running the little inetTester I get Santa's MacBook Pro.local returned.

I've tried flushing the DNS cache following the instructions here, restarted, and I still get Santa's MacBook Pro.local returned.

It seems discoveryutil doesn't exist any more (in any case it's not on my machine), so I can't even look at the contents of the cache.

What on earth do I have to do to get this machine to forget about being Santa's MacBook Pro?

Best Answer

You are mixing a lot of completely or partly different/unrelated things here.

  1. To properly and quickly name resolve your hard disk/SSD, you also have to add its name to the hosts file:

    127.0.0.1    localhost santas-macbook-pro-2.local tt-this_is_a_VM_volume_and_therefore_slower_than_a_real_disk 
    

    Now you can check the response time by pinging it:

    ping tt-this_is_a_VM_disk_and_therefore_slower_than_a_real_disk 
    PING tt-this_is_a_VM_disk_and_therefore_slower_than_a_real_disk (127.0.0.1): 56 data bytes
    64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.054 ms
    64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.089 ms
    64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.071 ms
    
    --- tt-this_is_a_VM_disk_and_therefore_slower_than_a_real_disk ping statistics ---
    3 packets transmitted, 3 packets received, 0.0% packet loss
    round-trip min/avg/max/stddev = 0.054/0.071/0.089/0.014 ms
    

    The average round-trip time is ~0.071 ms. So the time needed to resolve it is less than ~0.071/2 ms. With a real HDD/SSD you can expect even faster responses.

    Please use the short tt (the name of your volume) instead of my long tt-this_.... name to get an even quicker response.

  2. To check your DNS cache open Console.app (enter mDNSResponder in the Search field) and Terminal.app.

    Check the time and enter in Terminal: sudo killall -INFO mDNSResponder

    Roughly the first half of the output in Console with the time stamp noted previously is the DNS cache. The cache part starts with ------------ Cache ------------- and ends with a summary like Cache currently contains 391 entities; 33 referenced by active questions.

    To kill the cache use sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

    Re-check the cache by entering sudo killall -INFO mDNSResponder again.

  3. discoveryutil was introduced in OS X 10.10.0 and replaced by its predecessor mDNSResponder in OS X 10.10.4.

  4. To modify your local hostname and computer name use scutil:

    sudo scutil --set LocalHostName trial
    sudo scutil --set ComputerName Trial
    

    If you still get a Bonjour name (e.g. Santa's MacBook Pro.local) your hostname is probably set incorrectly. Either change the hostname by entering:

    sudo scutil --set HostName trial
    

    or remove it completely (recommended) by modifying the file preferences.list:

    sudo  nano /Library/Preferences/SystemConfiguration/preferences.plist
    

    and remove your current malformed HostName near the bottom of the file:

        <key>HostName</key>
        <string>Santa's MacBook Pro.local</string>
    

        <key>HostName</key>
        <string></string>
    

    You may also have to remove the preferences backup file preferences.plist.old. Reboot afterwards.

  5. Name resolution is roughly done in this order:

      files (e.g. /etc/hosts) > mDNS > DNS)  \
       ↕︎                         (↓ )   (↓)   | rather remote hostnames
    DNS cache (memory) (←-----------------)  /
       ↑                                       \
    memory                                      | rather local hostnames (including hosts file)
       ↑                                        |
     file (preferences.plist)                  /
    
  6. inetTester is a tool to quickly check the elapsed time spent from the Java layer to access the localhost domain name. This is not the time to get (resolve) the local hostname. The hostname is stored somewhere in memory and a system call takes nanoseconds to get it.

  7. The name of your main volume (previously Santa's MacBook, now tt) is in no way related with the DNS cache or a slow local hostname resolution.

Regarding item 1 in the list. I wear Dark Vader's helmet party hat and got nasty temporarily.