/etc/hosts ignored in mac El Capitan (10.11.5)

aholub picture aholub · Jun 21, 2016 · Viewed 8.6k times · Source

I'm attempting to redirect an existing web address to a remote machine on my subnet. To do that, I put the following into /etc/hosts

192.168.1.249 holub.com

and flush the DNS cache with

sudo dscacheutil -flushcache;sudo killall -HUP mDNSResponder

However, when I ping holub.com (the most reliable way that I know to see how the address is actually resolved), I see the real web address (204.13.10.74), not the one specified in /etc/hosts (192.168.1.249). Interestingly, the mysqladmin utility does not resolve the address correctly, but the Chrome browser does resolve the address correctly. I'm guessing that Chrome has some sort of internal workaround.

In general, it looks like /etc/hosts is being used after the actual DNS lookup, not before as it should be, so an external address is not overridable.

I've tried using various IPv6 equivalents to my local address (0:0:0:0:0:ffff:c0a8:01f9 ::ffff:192.168.1.249 ::192.168.1.249), but that doesn't help. Rebooting (instead of flushing the cache) doesn't help either.

I have found one unsatisfactory workaround. If I disable the DNS reponder with

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist

then /etc/hosts is used, but then I can't access the internet.

If anybody's encountered this problem, I'd love to know if there's a workaround.

Answer

Josh picture Josh · Nov 29, 2016

I came across this while searching for an answer to a similar problem and wanted to post my findings for anyone else in the same position.

As a team of three, we found that edits to my /etc/hosts file appeared to work, while editing the other two's host files seemingly did not. Upon further digging, we realized I was on OS X 10.10, while they were on newer versions.

We found, after trying about a million things, that additions to the hosts file in 10.11 and up apparently could not have more than one space between the IP and the domain, for example:

DID NOT WORK:

1.2.3.4     some.site.com

DID WORK:

1.2.3.4 some.site.com

After making this change, we immediately started seeing expected results without any cache clears, reboots, or otherwise.

I know in your example you are only showing one space, but in the off chance that's now how it appeared in your actual file I wanted to share this anyway.