【问题标题】:curl: (6) Could not resolve host: google.com; Name or service not knowncurl:(6)无法解析主机:google.com;名称或服务未知
【发布时间】:2014-09-18 00:34:28
【问题描述】:

当我尝试将网页加载到终端时,它会给出curl: (6) Could not resolve host 错误。

我的电脑中有互联网,并尝试通过我的家庭互联网连接。所以我这里没有任何代理。

[root@localhost kevin]# curl http://google.com
curl: (6) Could not resolve host: google.com; Name or service not known

clean all 并再次尝试但没有运气。

但如果我使用 IP 而不是域名,它就可以正常工作。

[root@localhost kevin]# curl http://173.194.46.0

请问有什么线索吗?

【问题讨论】:

  • 运行命令nslookup google.com得到什么结果?
  • 另外,如果你运行cat /etc/resolv.conf,你会得到什么结果?
  • 这里是 nslookup [root@localhost run]# nslookup google.com Server: 192.168.1.254 Address: 192.168.1.254#53的结果
  • 这里是 resolv.conf [root@localhost run]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 192.168.1.254

标签: linux curl terminal fedora


【解决方案1】:

问题是:

  1. 启用 IPV6
  2. 错误的 DNS 服务器

这是我修复它的方法:

禁用 IPV6

  • 打开终端
  • 输入su并输入以超级用户身份登录
  • 输入root密码
  • 键入cd /etc/modprobe.d/ 将目录更改为/etc/modprobe.d/
  • 输入 vi disableipv6.conf 以在此处创建一个新文件
  • Esc + i 将数据插入文件
  • 在文件中输入install ipv6 /bin/true,避免加载IPV6相关模块
  • 输入Esc + :,然后输入wq保存并退出
  • 输入reboot 重启fedora
  • 重启后打开终端并输入lsmod | grep ipv6
  • 如果没有结果,则表示您正确禁用了 IPV6

添加 Google DNS 服务器

  • 打开终端
  • 输入su并输入以超级用户身份登录
  • 输入root密码
  • 键入cat /etc/resolv.conf 以检查您的 Fedora 使用的 DNS 服务器。这通常是您的调制解调器 IP 地址。
  • 现在我们必须找到一个强大的 DNS 服务器。幸运的是,Google 维护了一个开放的 DNS 服务器。
  • 转到this page 并找出“Google 公共 DNS IP 地址”是什么
  • 今天是8.8.8.88.8.4.4。但在未来,这些可能会发生变化。
  • 键入vi /etc/resolv.conf 以编辑resolv.conf 文件
  • Esc + i 将数据插入文件
  • 通过在每一行的开头插入# 来注释文件中的所有内容。请勿删除任何内容,因为将来可能会有用。
  • 在文件的下面两行输入

    域名服务器 8.8.8.8
    域名服务器 8.8.4.4

    -输入Esc + :,然后输入wq保存并退出

  • 现在您已完成,一切正常(无需重新启动)。
  • 但是每次重新启动计算机时,您的 /etc/resolv.conf 都会被默认替换。所以我会让你想办法避免这种情况。

这是我的博客文章: http://codeketchup.blogspot.sg/2014/07/how-to-fix-curl-6-could-not-resolve.html

【讨论】:

  • 还有其他公共 DNS 服务器——作为长期 google DNS 用户——替代品是 1.1.1.1 和来自 Cloudflare 的 1.0.0.1 的辅助服务器。您可以将它们替换为 8.8.8.8 和 8.8.4.4 。仅取决于您是否希望 Google 或 Cloudflare 记录您在 Internet 上的去向……我也相信 1.1.1.1 具有很小的、几乎可以忽略不计的速度优势。
  • 您可以通过提供 --ipv4-4 命令行参数来告诉 curl 使用 ipv4 进行名称解析。作为在系统范围内禁用 ipv6 的替代方法。
  • 添加 Google DNS 有帮助。不必费心禁用 IPV6。出于某种原因,我的 /etc/resolv.conf 中有名称服务器 127.0.0.53 O_O。它给我带来了很大的痛苦。谢谢!
【解决方案2】:

也许你有一些非常奇怪和限制性的 SELinux 规则?

如果没有,请尝试 strace -o /tmp/wtf -fF curl -v google.com 并尝试从 /tmp/wtf 输出文件中发现发生了什么。

【讨论】:

  • 这是结果[root@localhost run]# strace -o /tmp/wtf -fF curl -v google.com bash: strace: command not found...
  • 检查您的/etc/nsswitch.conf。如果主机行显示类似hosts: files dns DNS 不起作用。但是,如果它说类似hosts: files 的内容,那么说明 DNS 正在工作(请参阅主机命令的输出)
  • 这是我可以在那里找到的。 hosts: files mdns4_minimal [NOTFOUND=return] dns myhostname
  • @NayanaAdassuriya 所以从你的结果来看,DNS 不工作!首先主动尝试卷曲!欲了解更多信息webhostingtalk.com/showthread.php?t=1185626
  • @Sathish 我有一个在 chroot 中以 SELinux 许可模式运行的 ubuntu。 root 用户可以卷曲文件,但其他人给出了问题的错误。这里有什么问题?
【解决方案3】:

我今天有类似的问题。但更奇怪。

  • 主机 - 工作host pl.archive.ubuntu.com
  • dig - 适用于默认和所有其他 DNS 的 dig pl.archive.ubuntu.comdig @127.0.1.1 pl.archive.ubuntu.com
  • 卷曲 - 不起作用!但对于某些地址,它确实如此。诡异的! Ruby、APT 等也是如此。
$ curl -v http://google.com/
*   Trying 172.217.18.78...
* Connected to google.com (172.217.18.78) port 80 (#0)
> GET / HTTP/1.1
> Host: google.com
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 302 Found
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
< Referrer-Policy: no-referrer
< Location: http://www.google.pl/?gfe_rd=cr&ei=pt9UWfqXL4uBX_W5n8gB
< Content-Length: 256
< Date: Thu, 29 Jun 2017 11:08:22 GMT
<
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.pl/?gfe_rd=cr&ei=pt9UWfqXL4uBX_W5n8gB">here</A>.
</BODY></HTML>
* Connection #0 to host google.com left intact

$ curl -v http://pl.archive.ubuntu.com/
* Could not resolve host: pl.archive.ubuntu.com
* Closing connection 0
curl: (6) Could not resolve host: pl.archive.ubuntu.com

启示

最终我在 curl 上使用了strace,发现它连接到nscd deamon。

connect(4, {sa_family=AF_LOCAL, sun_path="/var/run/nscd/socket"}, 110) = 0

解决方案

我已经重新启动了 nscd 服务(名称服务缓存守护进程),它帮助解决了这个问题!

systemctl restart nscd.service

【讨论】:

  • 嗨,我知道这有点老了,但是对于特定网站,我在 Centos 7 上也面临同样的问题。我能够成功地为该站点使用 dig, host 但 curl 不起作用(尽管它适用于其他网站)。当我运行 strace 时,我找不到任何 nscd 服务(从你的回答中得到提示)。你能在这里帮忙吗?还有什么我可以检查的吗?
  • 进一步检查后,我能够在有问题的主机/网站的 IP 上卷曲。我在 /etc/resolve.conf 中添加了 8.8.8.8 并解决了问题
【解决方案4】:

尝试 nslookup google.com 以确定是否存在 DNS 问题。 192.168.1.254 是您的本地网络地址,看起来您的系统正在使用它作为 DNS 服务器。这也是您的网关/调制解调器路由器吗? 当您尝试 ping google.com 时会发生什么。您可以在 Internet 网络浏览器上浏览它吗?

【讨论】:

  • 这是结果。我可以让你的网络浏览器毫无问题地加载任何网页。 [kevin@localhost ~]$ nslookup google.com Server: 192.168.1.254 Address: 192.168.1.254#53 Non-authoritative answer: Name: google.com Address: 74.125.130.113 Name: google.com Address: 74.125.130.100 Name: google.com Address: 74.125.130.101 Name: google.com Address: 74.125.130.102 Name: google.com Address: 74.125.130.138 Name: google.com Address: 74.125.130.139
  • 工作正常[root@localhost kevin]# ping www.google.com PING www.google.com (173.194.127.144) 56(84) bytes of data. 64 bytes from www.google.com (173.194.127.144): icmp_req=1 ttl=52 time=40.6 ms 64 bytes from www.google.com (173.194.127.144): icmp_req=2 ttl=52 time=40.9 ms 64 bytes from www.google.com (173.194.127.144): icmp_req=3 ttl=52 time=40.4 ms
【解决方案5】:

如答案所示,无需禁用 IPv6。 curl失败的原因仅仅是因为缺少DNS解析。

对此有一个线性解决方案。

如果您关心 /etc/resolv.conf 中的内容,请附加它:

echo 'nameserver 1.1.1.1' | sudo tee -a /etc/resolv.conf >/dev/null

我通常不关心,只是替换文件的内容:

echo 'nameserver 1.1.1.1' | sudo tee /etc/resolv.conf >/dev/null

【讨论】:

    猜你喜欢
    • 2013-08-10
    • 2019-12-18
    • 2022-01-17
    • 2021-08-18
    • 2019-05-04
    • 2017-09-08
    • 1970-01-01
    • 2014-01-05
    相关资源
    最近更新 更多