![为什么我可以进行主机查找但不能进行 curl 呢?](https://linux22.com/image/568528/%E4%B8%BA%E4%BB%80%E4%B9%88%E6%88%91%E5%8F%AF%E4%BB%A5%E8%BF%9B%E8%A1%8C%E4%B8%BB%E6%9C%BA%E6%9F%A5%E6%89%BE%E4%BD%86%E4%B8%8D%E8%83%BD%E8%BF%9B%E8%A1%8C%20curl%20%E5%91%A2%EF%BC%9F.png)
有人见过这种情况吗?请注意,这种情况不仅发生在 google.com 上,而且在我尝试的每个域名上都会发生。这是一种无线连接(WEP),但我不确定这有什么关系:
$ curl -v google.com
# This takes about 60s to return
* getaddrinfo(3) failed for google.com:80
* Couldn't resolve host 'google.com'
* Closing connection #0
curl: (6) Couldn't resolve host 'google.com'
$ wget google.com
--2011-11-28 14:44:08-- http://google.com/
Resolving google.com... failed: Name or service not known.
wget: unable to resolve host address `google.com'
$ ping google.com
PING google.com (209.85.148.147) 56(84) bytes of data.
64 bytes from fra07s07-in-f147.1e100.net (209.85.148.147): icmp_req=2 ttl=54 time=136 ms
64 bytes from fra07s07-in-f147.1e100.net (209.85.148.147): icmp_req=3 ttl=54 time=34.0 ms
64 bytes from fra07s07-in-f147.1e100.net (209.85.148.147): icmp_req=4 ttl=54 time=34.3 ms
64 bytes from fra07s07-in-f147.1e100.net (209.85.148.147): icmp_req=5 ttl=54 time=42.5 ms
64 bytes from fra07s07-in-f147.1e100.net (209.85.148.147): icmp_req=6 ttl=54 time=44.7 ms
64 bytes from fra07s07-in-f147.1e100.net (209.85.148.147): icmp_req=7 ttl=54 time=34.5 ms
^C
--- google.com ping statistics ---
8 packets transmitted, 6 received, 25% packet loss, time 7007ms
rtt min/avg/max/mdev = 34.063/54.376/136.026/36.758 ms
$ host google.com
google.com has address 209.85.148.106
google.com has address 209.85.148.147
google.com has address 209.85.148.99
google.com has address 209.85.148.103
google.com has address 209.85.148.104
google.com has address 209.85.148.105
google.com mail is handled by 30 alt2.aspmx.l.google.com.
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.
$ host google.com 192.168.1.201
Using domain server:
Name: 192.168.1.201
Address: 192.168.1.201#53
Aliases:
google.com has address 209.85.148.103
google.com has address 209.85.148.104
google.com has address 209.85.148.105
google.com has address 209.85.148.106
google.com has address 209.85.148.147
google.com has address 209.85.148.99
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.
google.com mail is handled by 30 alt2.aspmx.l.google.com.
$ cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.1.201
$ cat /etc/hosts
127.0.0.1 localhost
::1 localhost
$ netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.1.254 0.0.0.0 UG 0 0 0 wlan0
127.0.0.0 127.0.0.1 255.0.0.0 UG 0 0 0 lo
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
基本上任何应用程序(包括 Firefox)都无法进行名称查找。此外,如果我将 wifi 离线并插入以太网电缆,则一切正常。
答案1
也许您已经制定了一些非常奇怪且限制性的 SELinux(或 grsecurity...)规则?
如果没有,请尝试strace -o /tmp/wtf -fF curl -v google.com
从输出文件中找出/tmp/wtf
发生了什么。
答案2
使用这个:https://www.centos.org/modules/newbb/viewtopic.php?topic_id=39343
我找到了一个可以帮助我解决问题的关键命令:
[root@localhost ~]# wget -6 URL Failed
[root@localhost ~]# wget -4 URL Worked
这与默认 ipv6 堆栈有关,导致某些实用程序出现问题。禁用 ipv6 即可解决。
答案3
检查你的/etc/nsswitch.conf
。如果hosts
出现类似
hosts: files dns
我和你一样困惑。但如果它说的是
hosts: files
那么 DNS 正在运行的事实(参见host
命令的输出)将不会对 curl 有帮助,因为 curl 通过标准操作系统库进行名称解析,而标准操作系统库已被告知不要使用 DNS。
答案4
您的 /etc/resolv.conf 文件中可能存在错误,nslookup 可以容忍该错误,但 curl 不能。
提出的问题是“为什么我可以进行主机查找但不能进行 curl?”
这是可能的,因为 curl 使用 getaddrinfo() 来解析 FQDN,而 nslookup 则不解析。相反,我相信 nslookup 使用其他函数或库,或者通过其自己的自定义代码来解析 /etc/resolv.conf。我没有查看源代码来验证这一点,但您可以通过在 /etc/resolv.conf 中的名称服务器令牌前面添加空格来证明这一点。nslookup 可以解析它,但 getaddrinfo() 不能。
Example /etc/resolv.conf
nameserver 8.8.8.8
如果您的 resolv.conf 出现此错误,或者 nslookup 可以容忍但 getaddrinfo() 不能容忍的其他错误,那么您可以使用 nslookup 解析 FQDN,但无法在该 FQDN 上使用 curl。
修复:以 root 身份编辑 /etc/resolv.conf 并删除名称服务器行上的任何前导空格。