10

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.

aholub
  • 293
  • 3
  • 10
  • 3
    Make sure the entry you added is properly formatted: IP address followed by a space or tab, followed by the name, then a linefeed at the end of the line. Try printing the hosts file with `cat -vet /etc/hosts` to make normally invisible characters visible. The line should look like "192.168.1.249^Iholub.com$" (the "^I" is a tab, and the "$" is the linefeed) or "192.168.1.249 holub.com$". If you see a "^M" (carriage return) just before the "$", you have DOS/Windows formatted text and you need to remove the carriage return(s). – Gordon Davisson Jun 21 '16 at 19:03
  • Gordon, thanks, but all that's correct. Straight ascii file created with vim on a mac (which is essentially BSD unix), so no strange windows stuff :-). The problem runs deeper. – aholub Jun 22 '16 at 03:28
  • Does adding `order hosts, bind` as the first line in `/etc/resolv.conf` help? – Dusan Bajic Jun 22 '16 at 08:23
  • No. /etc/resolve.conf is not present, and adding it has no effect. El Capitan seems to have discarded pretty much everything that used to work in the past. None of the standard Liunx procedures work, and things that worked in Yosemite don't work any more. I'm just going to give up and use dnsmasq, inserted manually into the list of DNS resolvers (using networksetup). Apple will probably break that with the next MacOS release (they've abandoned the name "OS X"), but at least I'll be able to get back to work. – aholub Jun 28 '16 at 23:46
  • @GordonDavisson cat -vet /etc/hosts helped me resolve the issue. I had some random characters in place of tab. – Prabhakar Kasi Mar 02 '17 at 00:16

1 Answers1

12

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.

Josh
  • 363
  • 6
  • 12