91

when I try to load a web page to terminal it gives curl: (6) Could not resolve host error.

I have internet in my PC and trying from my home internet connection. So as I there is no any proxy involve here.

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

clean all and tried again but no lucky.

But if I use IP instead of the domain name, it works fine.

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

any clue please?

5
  • 1
    What result you get by running the command nslookup google.com?
    – Rhim
    Jul 26, 2014 at 9:09
  • 1
    Also, what result do you get if your run cat /etc/resolv.conf?
    – mti2935
    Jul 26, 2014 at 10:02
  • here is the result for nslookup [root@localhost run]# nslookup google.com Server: 192.168.1.254 Address: 192.168.1.254#53 Jul 26, 2014 at 11:25
  • here is resolv.conf [root@localhost run]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 192.168.1.254 Jul 26, 2014 at 11:26
  • [Unrelated to the topic but funny]. When you say "I have internet in my PC" I didnt know that "the internet could hide inside a pc especially yours XD.
    – Andy McRae
    Oct 7, 2022 at 8:23

10 Answers 10

107

Issues were:

  1. IPV6 enabled
  2. Wrong DNS server

Here is how I fixed it:

IPV6 Disabling

  • Open Terminal
  • Type su and enter to log in as the super user
  • Enter the root password
  • Type cd /etc/modprobe.d/ to change directory to /etc/modprobe.d/
  • Type vi disableipv6.conf to create a new file there
  • Press Esc + i to insert data to file
  • Type install ipv6 /bin/true on the file to avoid loading IPV6 related modules
  • Type Esc + : and then wq for save and exit
  • Type reboot to restart fedora
  • After reboot open terminal and type lsmod | grep ipv6
  • If no result, it means you properly disabled IPV6

Add Google DNS server

  • Open Terminal
  • Type su and enter to log in as the super user
  • Enter the root password
  • Type cat /etc/resolv.conf to check what DNS server your Fedora using. Mostly this will be your Modem IP address.
  • Now we have to Find a powerful DNS server. Luckily there is a open DNS server maintain by Google.
  • Go to this page and find out what are the "Google Public DNS IP addresses"
  • Today those are 8.8.8.8 and 8.8.4.4. But in future those may change.
  • Type vi /etc/resolv.conf to edit the resolv.conf file
  • Press Esc + i for insert data to file
  • Comment all the things in the file by inserting # at the begin of the each line. Do not delete anything because can be useful in future.
  • Type below two lines in the file

    nameserver 8.8.8.8
    nameserver 8.8.4.4

    -Type Esc + : and then wq for save and exit

  • Now you are done and everything works fine (Not necessary to restart).
  • But every time when you restart the computer your /etc/resolv.conf will be replaced by default. So I'll let you find a way to avoid that.

Here is my blog post about this: http://codeketchup.blogspot.sg/2014/07/how-to-fix-curl-6-could-not-resolve.html

4
  • There are other public DNS servers-- as a long time google DNS user -- the alternatives are 1.1.1.1 and the secondary of 1.0.0.1 from Cloudflare. You can substitute them for 8.8.8.8 and 8.8.4.4 . Just depends if you want Google or Cloudflare logging where you are going on the Internet... I also believe that 1.1.1.1 has a small, nearly negligible speed advantage. Aug 28, 2019 at 22:33
  • 1
    You can tell curl to use ipv4 for name resolving explicitly by providing the --ipv4 or -4 command line argument. As an alternative to disabling ipv6 system-wide.
    – Alex Che
    Sep 19, 2019 at 9:48
  • Adding Google DNS helped. Didn't have to bother with disabling IPV6. For some reason I had nameserver 127.0.0.53 O_O in my /etc/resolv.conf. It caused a lot of pain to me. THANK YOU! Sep 3, 2021 at 9:35
  • Is there a way to add the Google DNS server permanently, not temporary? This fixed my issue, thanks for that, but I have to keep on doing it.
    – Bart
    Oct 7, 2022 at 7:46
7

Perhaps you have some very weird and restrictive SELinux rules in place?

If not, try strace -o /tmp/wtf -fF curl -v google.com and try to spot from /tmp/wtf output file what's going on.

5
  • here is the result [root@localhost run]# strace -o /tmp/wtf -fF curl -v google.com bash: strace: command not found... Jul 26, 2014 at 11:27
  • Check your /etc/nsswitch.conf. If the hosts line says something like hosts: files dns DNS is not working. But if it says something like hosts: files then the fact that DNS is working (see output of host command)
    – Sathish
    Jul 26, 2014 at 11:35
  • 1
    Here is what I can found there. hosts: files mdns4_minimal [NOTFOUND=return] dns myhostname Jul 27, 2014 at 1:23
  • @NayanaAdassuriya So from your result DNS is not working! First active that ans try curl! For more info webhostingtalk.com/showthread.php?t=1185626
    – Sathish
    Jul 27, 2014 at 7:38
  • @Sathish I have an ubuntu running in chroot with SELinux permissive mode. root user can curl files but someother gives the question's error. What is the problem here?
    – NONONONONO
    Nov 5, 2020 at 17:44
6

I have today similar problem. But weirder.

  • host - works host pl.archive.ubuntu.com
  • dig - works on default and on all other DNS's dig pl.archive.ubuntu.com, dig @127.0.1.1 pl.archive.ubuntu.com
  • curl - doesn't work! but for some addresses it does. WEIRD! Same in Ruby, APT and many more.
$ 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

Revelation

Eventually I used strace on curl and found that it was connection to nscd deamon.

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

Solution

I've restarted the nscd service (Name Service Cache Daemon) and it helped to solve this issue!

systemctl restart nscd.service
3
  • Hi, I know this is little old, but I am too facing the same issue on Centos 7 for a particular website. I am able to use dig, host for that site successfully but curl does not work (though it works for other websites). When I ran strace, I am not able to find any nscd service (taking cue from your answer). Can you please help here. Any other thing that I can check?
    – NumeroUno
    Jan 12, 2020 at 9:52
  • After checking further, I was able to curl on the IP of the problematic host/website. I added 8.8.8.8 in /etc/resolve.conf and it solved the issue
    – NumeroUno
    Jan 12, 2020 at 10:39
  • in my case systemctl restart nscd.service was the solution. thank you Dec 5, 2022 at 0:01
6

There is no need to disable IPv6 as the answer suggests. The reason curl fails is simply because the DNS resolution is missing.

There is one liner solution to this.

If you care about what is inside /etc/resolv.conf then append it:

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

I usually don't care and just replace the content of the file:

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

Try nslookup google.com to determine if there's a DNS issue. 192.168.1.254 is your local network address and it looks like your system is using it as a DNS server. Is this your gateway/modem router as well? What happens when you try ping google.com. Can you browse to it on a Internet web browser?

2
  • here is the result. I can you the web browser to load any web page without any issue. [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 Jul 27, 2014 at 1:21
  • works fine [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 Jul 27, 2014 at 1:32
1

In Our case, the command was passed through mail/skype and the person who needs to execute copied an extra space.

we found that extra space after an hour and removing that made it work.

0

I had an issue with IPV6 that suddenly showed up in wordpress admin where curl failed to connect to wordpress.org etc giving

An unexpected error occurred. Something may be wrong with WordPress.org

and also

Installation failed: Download failed. cURL error 28: Resolving timed out after 10005 milliseconds

I eventually sorted by adding the following as a php file in mu-plugins:

add_action( 'http_api_curl', function( $curl_handle ) { curl_setopt( $curl_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );});

and also when using curl directly in php by using the following option in the options array:

curl_setopt_array($curl, array(CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4));

The OS is IBM i (OS400/i5OS) v7.2 running on Power 6 and the php version is 8.1.10. Interestingly IPV6 is not active on the box.

0

My case on MacOS was simple: I closed the iTerm window I was using and opened a new one.

It seems likely that this is an issue with CURL cacheing DNS responses given that I was messing around with my DNS when I ran into the same error as the OP.

0

In my case, the problem was in the Static Route Table. Must be removed if a static route was previously added

1
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional information like what should be removed from Static Route Table, how to remove it and any other details, so developers can understand your reasoning. Would you kindly edit your answer to include additional details for the benefit of the community? May 16 at 10:39
0

Could be a firewall issue.

Try:

ufw allow out 53
reboot

Not the answer you're looking for? Browse other questions tagged or ask your own question.