macos Mac OSX Lion DNS 查找顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6841421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mac OSX Lion DNS lookup order
提问by Meik
After upgrading to Mac OSX Lion I figured out that /etc/hosts is not looked up in first place for name resolution anymore. This leads to some side effects like:
升级到 Mac OSX Lion 后,我发现 /etc/hosts 不再首先用于名称解析。这会导致一些副作用,例如:
- Entries in /etc/hosts are resolved painfully slow
- You can't not override existing domains, e.g. 127.0.0.1 www.google.com
- If you get search domain entries from DHCP, let say .lan, and some funny guy configured localhost.lan to something else then 127.0.0.1 in the local DNS you can not reach your localhost anymore.
- /etc/hosts 中的条目解析速度非常慢
- 您不能不覆盖现有域,例如 127.0.0.1 www.google.com
- 如果您从 DHCP 获得搜索域条目,比如说 .lan,并且一些有趣的人将 localhost.lan 配置为其他内容,然后在本地 DNS 中设置为 127.0.0.1,您将无法再访问您的本地主机。
Is this behavior intended? Does it make any sense? And most important, how can I come back to the old behavior.
这种行为是有意的吗?它有任何意义吗?最重要的是,我怎样才能回到旧的行为。
回答by Jean-Baptiste MONIN
I think he matter is Lion handles .local TLD differently because it's reserved for some Multicast DNS features (used by Bonjour). The only way i found to solve this issue is using a different TLD for development hosts (ie: .dev). It works fine for me, hope it's gonna be helpful to others!
我认为重要的是 Lion 处理 .local TLD 的方式不同,因为它是为某些多播 DNS 功能保留的(由 Bonjour 使用)。我发现解决此问题的唯一方法是为开发主机使用不同的 TLD(即:.dev)。对我来说效果很好,希望对其他人有帮助!
回答by guns
With regards to overriding domains in the hosts file, I have found that in some circumstances, Lion queries the IPv6 address for a domain if it senses that a domain is unreachable over the IPv4 network.
关于覆盖 hosts 文件中的域,我发现在某些情况下,Lion 会在检测到域无法通过 IPv4 网络访问时查询域的 IPv6 地址。
I discovered this when I noticed some ads that I had never seen before on Snow Leopard because I had redirected the ad domains to 127.0.0.1
. I fired up wireshark and noticed AAAA
(IPv6 DNS records) queries following the IPv4 A
queries (IPv4). The ad servers indeed have IPv6 addesses and were able to serve me their content.
当我注意到之前在 Snow Leopard 上从未见过的一些广告时,我发现了这一点,因为我将广告域重定向到127.0.0.1
. 我启动了wireshark并注意到AAAA
(IPv6 DNS记录)在IPv4A
查询(IPv4)之后查询。广告服务器确实有 IPv6 地址并且能够为我提供他们的内容。
The solution to this is have a
解决这个问题的方法是
::1 mydomain.com
entry for every
每个条目
127.0.0.1 mydomain.com
entry in your hosts file.
主机文件中的条目。
Interestingly, if you happen to have a local webserver running on 127.0.0.1:80
and your browser receives a response from the webserver (error or otherwise), no AAAA
query is issued, as it seems to be satisfied that a TCP connection was at least possible.
有趣的是,如果您碰巧有一个本地网络服务器正在运行127.0.0.1:80
并且您的浏览器收到来自网络服务器的响应(错误或其他),则不会AAAA
发出任何查询,因为它似乎对 TCP 连接至少是可能的感到满意。
On a related note, if you make heavy use of the hosts file (for adblocking, local web development, etc), you may want to look into running your own local DNS resolver. There is a considerable disk/CPU hit from having to read /etc/hosts
on every request, so it is in your best interest to keep that file very light.
在相关说明中,如果您大量使用主机文件(用于广告拦截、本地 Web 开发等),您可能需要考虑运行您自己的本地 DNS 解析器。必须读取/etc/hosts
每个请求会导致相当大的磁盘/CPU 消耗,因此保持该文件非常轻量级符合您的最佳利益。
One advantage of running something like dnsmasq
locally (besides the significant performance boost) is that you can redirect whole top-level domains back to your local machine. This allows you to have the whole *.dev namespace for development (for instance), without having to individually enter every domain you want resolved locally into /etc/hosts
dnsmasq
在本地运行类似的东西(除了显着的性能提升)的一个优点是您可以将整个顶级域重定向回您的本地机器。这允许您拥有用于开发的整个 *.dev 命名空间(例如),而无需单独输入您想要在本地解析的每个域/etc/hosts
回答by Meik
The problem was that I symlinked the /etc/hosts file. If /etc/hosts is a plain file everything is ok.
问题是我符号链接了 /etc/hosts 文件。如果 /etc/hosts 是一个普通文件,则一切正常。
回答by Dan Pritts
Update(2): OSX 10.10.5 brings the return of mDNSResponder
.
更新(2):OSX 10.10.5 带来了mDNSResponder
.
Update: OSX 10.10 Yosemite has replaced mDNSResponder with "discoveryd". I've not upgraded so I am not sure of the discoveryd behavior w/r/t DNS lookups and /etc/hosts
.
更新:OSX 10.10 Yosemite 已将 mDNSResponder 替换为“discoveryd”。我没有升级,所以我不确定发现的行为 w/r/t DNS 查找和/etc/hosts
.
The system DNS resolver on Lion is the mDNSResponder
process.
Lion 上的系统 DNS 解析器就是这个mDNSResponder
过程。
You may be thinking "but mDNSResponder is the multicast dns responder." You're right; that's what it originally was for, and it still fulfills this function. However, on newer MacOS versions it also does standard host lookups.
您可能会想“但 mDNSResponder 是多播 dns 响应器”。你说得对; 这就是它最初的用途,它仍然完成了这个功能。但是,在较新的 MacOS 版本上,它还执行标准主机查找。
In Lion, it does not appear to automatically re-read /etc/hosts
when it changes, at least not always. Killing mDNSResponder
(and allowing it to be automatically restarted) seems to fix the problem.
在 Lion 中,它似乎不会/etc/hosts
在更改时自动重新读取,至少并非总是如此。杀死mDNSResponder
(并允许它自动重新启动)似乎可以解决问题。
sudo killall mDNSResponder
should do the trick.
应该做的伎俩。
below is my original answer for posterity. I suppose it might still be an issue in some cases.
以下是我对后代的原始答案。我想在某些情况下它可能仍然是一个问题。
Make sure your /etc/hosts
file is a unix style text file, with linefeeds as the ending rather than cr's.
确保您的/etc/hosts
文件是 unix 样式的文本文件,以换行符作为结尾而不是 cr 的结尾。
Editing with TextWrangler or a unix text editor should preserve the file.
使用 TextWrangler 或 unix 文本编辑器进行编辑应保留文件。
If your file is already messed up, try this to fix
如果你的文件已经搞砸了,试试这个来修复
tr '5' '2' < /etc/hosts > /tmp/hosts.$$
mv /etc/hosts /etc/hosts.bad
mv /tmp/hosts.$$ /etc/hosts
# fix up permissions while we are at it
chown root:wheel /etc/hosts
chmod 644 /etc/hosts
credit for this fix to:
将此修复归功于:
http://techpatio.com/2011/guides-how-to/fixed-mac-osx-lion-etc-hosts-bugs-dns
http://techpatio.com/2011/guides-how-to/fixed-mac-osx-lion-etc-hosts-bugs-dns
回答by Opentuned
ive had this issue for a while, as im working a team of devs it became necessary to actually use .local rather then .dev or .localhost, i found this article to be very useful.
我有一段时间遇到这个问题,因为我在一个开发团队工作,因此有必要实际使用 .local 而不是 .dev 或 .localhost,我发现这篇文章非常有用。
iTand.me - Lion local domains and etc hosts..
In summary;
总之;
But if you have to use .local, the most elegant solution I've found is the dscl utility. Using it is very straightforward. To add a host called mydev.local and point it to the localhost, just do this:
但是,如果您必须使用 .local,我发现的最优雅的解决方案是 dscl 实用程序。使用它非常简单。要添加一个名为 mydev.local 的主机并将其指向本地主机,只需执行以下操作:
sudo dscl localhost -create /Local/Default/Hosts/mydev.local IPAddress 127.0.0.1
To see all the currently defined hosts and their IPs
查看所有当前定义的主机及其 IP
sudo dscl localhost -list /Local/Default/Hosts IPAddress
And to remove a host:
并删除主机:
sudo dscl localhost -delete /Local/Default/Hosts/mydev.local
Overall, pretty straightforward and works well. I still would prefer to be able to edit /etc/hosts instead, but this is a better alternative to having to rename all our .local servers.
总的来说,非常简单,效果很好。我仍然希望能够编辑 /etc/hosts,但这是必须重命名所有 .local 服务器的更好选择。
回答by Nathan Long
Prior to moving from Snow Leopard to Lion, I had several app-specific entries in /etc/hosts
, like this:
在从 Snow Leopard 迁移到 Lion 之前,我在 中有几个特定于应用程序的条目/etc/hosts
,如下所示:
127.0.0.1 foo.bar.local
After the update, loading my local apps was VERY slow. I noticed that the delay happened before the request showed up in the log file, and that once it did, the app itself was as fast as usual.
更新后,加载我的本地应用程序非常慢。我注意到延迟发生在请求出现在日志文件中之前,一旦发生,应用程序本身就和往常一样快。
Now I have two lines per app, like this:
现在我每个应用程序有两行,如下所示:
127.0.0.1 foo.bar.local
::1 foo.bar.local
... and everything is fast again.
......一切都很快了。
Apparently this adds IPv6 addresses? I don't quite get it, really, but it works.
显然这增加了 IPv6 地址?我不太明白,真的,但它有效。
回答by Paul J. Martinez
My situation was similar, but the delays, of exactly 5 seconds, only happened for URLs ending with '.local'. When viewing sites that ended in '.dev', there was no delay.
我的情况是类似的,但是只有以“.local”结尾的 URL 才会发生 5 秒的延迟。查看以“.dev”结尾的站点时,没有延迟。
Some of the other developers in my office had this problem, while a few did not. I was hoping for a simple fix and I did not want to rename the site to '.local' due to other dependencies.
我办公室的其他一些开发人员有这个问题,而一些没有。我希望有一个简单的修复,但由于其他依赖关系,我不想将站点重命名为“.local”。
I ran the following command in Terminal and diffed my output with a few other users in the office.
我在终端中运行了以下命令,并将我的输出与办公室中的其他一些用户进行了比较。
scutil --dns
This section was the only difference:
本节是唯一的区别:
resolver #2
domain : 00000000.members.btmm.icloud.com
options : pdns
timeout : 5
order : 150000
My Mac was linked to my iCloud account and I had Back To My Mac enabled. Once I disabled Back To My Mac, the additional resolver went away and the 5 second delay disappeared.
我的 Mac 已链接到我的 iCloud 帐户,并且我启用了“回到我的 Mac”。一旦我禁用了“回到我的 Mac”,额外的解析器就消失了,5 秒的延迟消失了。
回答by pohl
Wow, what a nightmare. I have read absolutely everything on this subject and everything that has been suggested so far was tantilizingly close to what I was experiencing, but none of the solutions worked for me.
哇,真是一场噩梦。我已经阅读了关于这个主题的所有内容,到目前为止所提出的所有内容都非常接近我所经历的,但没有一个解决方案对我有用。
And I figured out why.
我想出了原因。
Unlike others, I was not using /etc/hosts to set up local domains. My /etc/hosts file was stock, containing only the entries needed for the loopback interface and the broadcast host. Moreover, it was a correctly-encoded unix file, as I'm the sort of person who would only edit that from the command-line using emacs. And, thank goodness, I did not have to resort to running my own DNS server like DNSmasq to get around the problem.
与其他人不同,我没有使用 /etc/hosts 来设置本地域。我的 /etc/hosts 文件是库存的,仅包含环回接口和广播主机所需的条目。此外,它是一个正确编码的 unix 文件,因为我是那种只会使用 emacs 从命令行编辑它的人。而且,谢天谢地,我不必求助于像 DNSmasq 这样运行我自己的 DNS 服务器来解决这个问题。
(To be clear, the symptom that brought me here to this issue was that emacs took about 10 seconds to start, but only when I was on wifi. If I turned off wifi, emacs would start up instantly as expected.)
(明确地说,让我想到这个问题的症状是 emacs 需要大约 10 秒才能启动,但仅限于我在 wifi 上。如果我关闭 wifi,emacs 会按预期立即启动。)
My solution: my laptop has a name, "terminator". (Yes, its shiny aluminum exterior made me think of the Arnold Schwarzenegger character.) I just needed to add entries to /etc/hosts for the name of the machine itself:
我的解决方案:我的笔记本电脑有一个名字,“终结者”。(是的,它闪亮的铝制外观让我想起了阿诺德施瓦辛格这个角色。)我只需要在 /etc/hosts 中添加机器名称的条目:
127.0.0.1 terminator
::1 terminator
I found the name of my host by running a simple command in the terminal:
我通过在终端中运行一个简单的命令找到了我的主机名:
hostname
...which came back with the output: "terminator". After changing /etc/hosts to contain those two entries, emacs can now quickly resolve my laptop's name.
...返回输出:“终结者”。更改 /etc/hosts 以包含这两个条目后,emacs 现在可以快速解析我的笔记本电脑的名称。
I hope this helps someone.
我希望这可以帮助别人。
回答by Eddie B
I've had speed issues using OSX Lion as a web development box ... Using a combination of suggestions I resorted to disabling ipv6 networking and routing ipv6 to localhost6 ... things sped up quite a bit ...
我在使用 OSX Lion 作为 Web 开发盒时遇到了速度问题......结合使用建议,我求助于禁用 ipv6 网络并将 ipv6 路由到 localhost6 ......事情加速了很多......
sudo networksetup -setv6off Ethernet
/etc/hosts ...
/etc/hosts ...
127.0.0.1 localhost
127.0.0.1 dev.aliasdomain.com
...
::1 localhost6
回答by thomasafine
I think there's been some bug fixes. I've seen a lot of problems mentioned, and none of these seem to apply currently (for example, putting multiple aliases on a single line now works fine for me).
我认为已经修复了一些错误。我已经看到了很多提到的问题,但这些问题目前似乎都不适用(例如,将多个别名放在一行上现在对我来说很好用)。
At any rate it seems that with Lion, Apple made some drastic changes to mDNSResponder which handles all the DNS lookups, and (with Lion at least) also handles /etc/hosts cacheing. For me forward lookups also now work. But reverse lookups (e.g. looking up 1.2.3.4 instead of google.com) don't work.
无论如何,似乎在 Lion 中,Apple 对 mDNSResponder 进行了一些重大更改,该 mDNSResponder 处理所有 DNS 查找,并且(至少使用 Lion)还处理 /etc/hosts 缓存。对我来说,正向查找现在也可以工作。但是反向查找(例如查找 1.2.3.4 而不是 google.com)不起作用。
After a lot of pain, it looks like mDNSResponder converts this lookup to 4.3.2.1.in-addr.arpa and does a name lookup. This may well be how DNS prefers to operate, but it doesn't work at all with /etc/hosts.
经过一番痛苦之后,看起来 mDNSResponder 将此查找转换为 4.3.2.1.in-addr.arpa 并进行名称查找。这很可能是 DNS 更喜欢的操作方式,但它根本不适用于 /etc/hosts。
Unless of course you add an alias of 4.3.2.1.in-addr.arpa for each host, where 4.3.2.1 is the ip address in the opposite order from which you are used to seeing it. This fixes everything for me. Here's an example /etc/hosts entry:
当然,除非您为每个主机添加别名 4.3.2.1.in-addr.arpa,其中 4.3.2.1 是与您习惯看到它的顺序相反的 IP 地址。这为我解决了一切。这是 /etc/hosts 条目的示例:
1.2.3.4 foo foo.example.com alias.example.com 4.3.2.1.in-addr.arpa
1.2.3.4 foo foo.example.com 别名.example.com 4.3.2.1.in-addr.arpa