我遇到了一个奇怪的 DNS 问题。我在 Gentoo Linux 上运行双 ipv4/ipv6 环境。
Ping 某些站点的结果是 ping 127.0.0.1。例如
#> ping authserver.mojang.com
PING authserver.mojang.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=64 time=0.045 ms
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=2 ttl=64 time=0.043 ms
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=3 ttl=64 time=0.058 ms
--- authserver.mojang.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.043/0.048/0.058/0.010 ms
然而,Dig 正确返回了以下内容:
# dig authserver.mojang.com
; <<>> DiG 9.9.3-P2 <<>> authserver.mojang.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15800
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;authserver.mojang.com. IN A
;; ANSWER SECTION:
authserver.mojang.com. 5 IN A 54.235.119.47
;; Query time: 14 msec
;; SERVER: 2001:4860:4860::8888#53(2001:4860:4860::8888)
;; WHEN: Sat Nov 09 15:34:40 GMT 2013
;; MSG SIZE rcvd: 66
我糊涂了!我的网络浏览器返回了正确的网站,并且启动 Windows 的同一台计算机也能正常工作。
我没有改变 /etc/hosts(我改变了我的实际计算机名称以发布在这里):127.0.0.1 localhost.localdomain localhost
# IPv4 and IPv6 localhost aliases
127.0.0.1 my.computer.com my localhost
::1 my.computer.com my localhost
我的resolv.conf:
# Generated by dhcpcd from eth0
options inet6
# Google
nameserver 2001:4860:4860::8888
nameserver 2001:4860:4860::8844
nameserver 8.8.8.8
nameserver 8.8.4.4
domain my.domain.com
编辑: 我的 /etc/nsswitch.conf (未动过):
# /etc/nsswitch.conf:
# $Header: /var/cvsroot/gentoo/src/patchsets/glibc/extra/etc/nsswitch.conf,v 1.1 2006/09/29 23:52:23 vapier Exp $
passwd: compat
shadow: compat
group: compat
# passwd: db files nis
# shadow: db files nis
# group: db files nis
hosts: files dns
networks: files dns
services: db files
protocols: db files
rpc: db files
ethers: db files
netmasks: files
netgroup: files
bootparams: files
automount: files
aliases: files
根据建议,我在 ping 上运行了 strace:strace 日志
它没有连接到 ncsd:
connect(4, {sa_family=AF_FILE, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory)
然后它尝试连接到 Google 的 DNS:
connect(4, {sa_family=AF_INET6, sin6_port=htons(53), inet_pton(AF_INET6, "2001:4860:4860::8888", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 28) = 0
poll([{fd=4, events=POLLOUT}], 1, 0) = 1 ([{fd=4, revents=POLLOUT}])
sendto(4, "\16\273\1\0\0\1\0\0\0\0\0\0\nauthserver\6mojang\3c"..., 39, MSG_NOSIGNAL, NULL, 0) = 39
poll([{fd=4, events=POLLIN}], 1, 5000) = 1 ([{fd=4, revents=POLLIN}])
ioctl(4, FIONREAD, [55]) = 0
recvfrom(4, "\16\273\201\200\0\1\0\1\0\0\0\0\nauthserver\6mojang\3c"..., 1024, 0, {sa_family=AF_INET6, sin6_port=htons(53), inet_pton(AF_INET6, "2001:4860:4860::8888", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [28]) = 55
close(4) = 0
还是行不通!
根据建议,使用 -e 读取最后的 DNS 查找如下:
socket(PF_INET6, SOCK_DGRAM|SOCK_NONBLOCK, IPPROTO_IP) = 4
connect(4, {sa_family=AF_INET6, sin6_port=htons(53), inet_pton(AF_INET6, "2001:4860:4860::8888", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 28) = 0
poll([{fd=4, events=POLLOUT}], 1, 0) = 1 ([{fd=4, revents=POLLOUT}])
sendto(4, "M\354\1\0\0\1\0\0\0\0\0\0\nauthserver\6mojang\3c"..., 39, MSG_NOSIGNAL, NULL, 0) = 39
poll([{fd=4, events=POLLIN}], 1, 5000) = 1 ([{fd=4, revents=POLLIN}])
ioctl(4, FIONREAD, [55]) = 0
recvfrom(4, "M\354\201\200\0\1\0\1\0\0\0\0\nauthserver\6mojang\3c"..., 1024, 0, {sa_family=AF_INET6, sin6_port=htons(53), inet_pton(AF_INET6, "2001:4860:4860::8888", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [28]) = 55
| 00000 4d ec 81 80 00 01 00 01 00 00 00 00 0a 61 75 74 M....... .....aut^? |
| 00010 68 73 65 72 76 65 72 06 6d 6f 6a 61 6e 67 03 63 hserver. mojang.c^? |
| 00020 6f 6d 00 00 01 00 01 c0 0c 00 01 00 01 00 00 00 om...... ........^? |
| 00030 05 00 04 17 15 2d ec .....-. ^? |
close(4) = 0
恐怕我对 DNS 协议了解不够,无法对其进行解码。完整跟踪在这里:完整跟踪
答案1
您描述的症状(dig 给出正确答案,而 gethostbyname 没有给出)意味着系统中的某处存在缓存的错误答案。
跑步
strace ping authserver.mojang.com
应该能帮助您了解数据可能缓存的位置。nscd 通常是这种情况下的罪魁祸首。查找连接调用,这是对 dns 服务器的调用。
connect(4, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("134.79.110.9")}, 28) = 0
这个是 nscd 套接字。
connect(4, {sa_family=AF_FILE, path="/var/run/nscd/socket"...}, 110) = 0
您将看到发送消息,其中包含您尝试联系的 DNS 名称,并且将使用本地主机 IP 地址进行响应。这是成功 DNS 查找的示例。
sendto(4, "@\330\1\0\0\1\0\0\0\0\0\0\3www\4slac\10stanford\3e"..., 39, MSG_NOSIGNAL, NULL, 0) = 39
recvfrom(4, "@\330\205\200\0\1\0\2\0\4\0\7\3www\4slac\10stanford\3e"..., 1024, 0, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("134.79.110.9")}, [16]) = 309