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

大兔子大兔子 提交于 2021-02-18 21:02:03

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/37951379/etc-hosts-ignored-in-mac-el-capitan-10-11-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!