user3132457
user3132457

Reputation: 1019

curl: (6) Could not resolve host: on WSL

I have installed Ubuntu 20.04 LTS on WSL.

I'm trying to run the following command:

curl https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch

It fails with error:

curl: (6) Could not resolve host: developer.mozilla.org

I have tried something else, and it worked, e.g.

$ curl https://google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>

Here's some details:

$ nslookup developer.mozilla.org
Server:         127.0.0.1
Address:        127.0.0.1#53

Non-authoritative answer:
developer.mozilla.org   canonical name = d1avawhiqh9uo1.cloudfront.net.
Name:   d1avawhiqh9uo1.cloudfront.net
Address: 52.84.112.124
Name:   d1avawhiqh9uo1.cloudfront.net
Address: 52.84.112.61
Name:   d1avawhiqh9uo1.cloudfront.net
Address: 52.84.112.82
Name:   d1avawhiqh9uo1.cloudfront.net
Address: 52.84.112.105
Name:   d1avawhiqh9uo1.cloudfront.net
Address: 2600:9000:203c:8e00:13:e62f:2040:93a1
Name:   d1avawhiqh9uo1.cloudfront.net
Address: 2600:9000:203c:9c00:13:e62f:2040:93a1
Name:   d1avawhiqh9uo1.cloudfront.net
Address: 2600:9000:203c:a000:13:e62f:2040:93a1
Name:   d1avawhiqh9uo1.cloudfront.net
Address: 2600:9000:203c:d800:13:e62f:2040:93a1
Name:   d1avawhiqh9uo1.cloudfront.net
Address: 2600:9000:203c:f600:13:e62f:2040:93a1
Name:   d1avawhiqh9uo1.cloudfront.net
Address: 2600:9000:203c:200:13:e62f:2040:93a1
Name:   d1avawhiqh9uo1.cloudfront.net
Address: 2600:9000:203c:1c00:13:e62f:2040:93a1
Name:   d1avawhiqh9uo1.cloudfront.net
Address: 2600:9000:203c:4e00:13:e62f:2040:93a1

Please help to understand the problem.

Upvotes: 19

Views: 44950

Answers (7)

Dario Defilippi
Dario Defilippi

Reputation: 595

The accepted solution didn't work for me.

Searching a little bit more made me land on WSL throws (6) Could not resolve host: raw.githubusercontent.com when trying to install NVM and cant ping google.com

and it worked for me. See the answer quoted below

you can try running this

sudo rm /etc/resolv.conf
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
sudo bash -c 'echo "[network]" >> /etc/wsl.conf'
sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'
sudo chattr +i /etc/resolv.conf

it deletes your resolve.config file thats automatically generated when u run WSL then creates a new file and puts "nameserver 8.8.8.8", then creates a wsl.conf file and adds [network] and generateResolveConf = false to prevent auto generation of that file

then

ping google.com

update 2024-04-15-1333 Made the script append to wsl.conf as it can contain other settings that should be discarded

Upvotes: 26

gurpartap
gurpartap

Reputation: 455

Problem: While my pc was connected to the VPN, WSL Network manager kept overwriting /etc/resolv.conf. This prevented me from downloading packages that can make resolv.conf immutable.

My Solution:
Run a script in bash shell that constantly updates resovl.conf. Concurrently, in a separate shell, download packages that can make resolv.conf immutable. See steps 4 & 5.

STEPS:

  1. Get DNS server addresses
    In powershell run:
    ipconfig /all
  2. Rank Interface Metric every time you reconnect to VPN
    My VPN is: Cisco AnyConnect
    In powershell run:
    Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000
    To Automate this see: Create Windows Scheduled Task for Auto update
  3. Update wsl.conf
    In WSL shell as root user sudo su - run:
echo "[network]
generateResolvConf=false" >> /etc/wsl.conf;
  1. Update resolv.conf with correct DNS IPs
    Script loops 300 times, pausing for 1 second in between.
    Replace xx.xx... in script below with actual IPs from step 1.
    *Run step 5 concurrently!*
    In WSL shell as root user sudo su - run:
for i in {1..300}; do
  echo '# non-vpn DNS IP
nameserver 8.8.8.8
# cisco any connect vpn DNS IPs
nameserver xx.xx.xx.xx
nameserver xx.xx.xx.xx' > /etc/resolv.conf;
  sleep 1;
done
  1. Download Packages and Make resolv.conf immutable
    NOTE: My WSL Distro is Fedora you may need to use a different package manager.
    In separate WSL shell as root user sudo su - run:
dnf -y install e2fsprogs
chattr +i /etc/resolv.conf

Troubleshooting:

cat /etc/resolv.conf                    # check content
chattr -i /etc/resolv.conf              # make resolve.conf editable

Upvotes: 4

Ashish Sharma
Ashish Sharma

Reputation: 672

Find DNS entry from window command and add it to /etc/resolv.conf file of ubuntu using its terminal. Restart then ping google.com to validate.

Upvotes: 0

Dmitry
Dmitry

Reputation: 706

Had the same on VM, was different reason though. Add

nameserver 127.0.0.53
options edns0 trust-ad

to

/etc/resolv.conf

Upvotes: 1

cealex
cealex

Reputation: 237

for posterity ... ( lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.4 LTS Release: 20.04 Codename: focal)

the work around for wsl2 in windows 10 and trying to reach localhost is :

"cmd.exe /C curl http://127.0.0.1 ..."

Upvotes: 0

Ties
Ties

Reputation: 5846

For me it happened after the Windows 11 upgrade.

I had to update i /etc/wsl.conf and restart my WSL instance

[network]
generateResolvConf = true

Upvotes: 9

Adrian
Adrian

Reputation: 1989

This worked for me: https://github.com/microsoft/WSL/issues/4731#issuecomment-562935992

This is happening to me after my pc goes to sleep for long hours. Shutting down wsl2 (wsl.exe --shutdown) and open it again solves the problem until the next time the pc goes to sleep again for long hours.

Upvotes: 4

Related Questions