Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix DNS resolution in WSL2
More recent resolution:
1. cd ~/../../etc (go to etc folder in WSL).
2. echo "[network]" | sudo tee wsl.conf (Create wsl.conf file and add the first line).
3. echo "generateResolvConf = false" | sudo tee -a wsl.conf (Append wsl.conf the next line).
4. wsl --terminate Debian (Terminate WSL in Windows cmd, in case is Ubuntu not Debian).
5. cd ~/../../etc (go to etc folder in WSL).
6. sudo rm -Rf resolv.conf (Delete the resolv.conf file).
7. In windows cmd, ps or terminal with the vpn connected do: Get-NetIPInterface or ipconfig /all for get the dns primary and
secondary.
8. With the dns primary and secondary getted replace the numbers in the next step in the X.X.X.X
9. echo "nameserver X.X.X.X" | sudo tee resolv.conf (Create resolv.conf and append the line.)
10. echo "nameserver X.X.X.X" | sudo tee -a resolv.conf (Append the line in resolv.conf)
11. wsl --terminate Debian (Terminate WSL in Windows cmd, in case is Ubuntu not Debian).
12. sudo chattr +i resolv.conf
13. And finally in windows cmd, ps or terminal:
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000
Credit: @MartinCaccia, @yukosgiti, @machuu and @AlbesK:
https://github.com/microsoft/WSL/issues/4277
https://github.com/microsoft/WSL/issues/4246
---
Original resoltuion:
1. Create a file: /etc/wsl.conf.
2. Put the following lines in the file in order to ensure the your DNS changes do not get blown away
[network]
generateResolvConf = false
3. In a cmd window, run wsl --shutdown
4. Restart WSL2
5. Create a file: /etc/resolv.conf. If it exists, replace existing one with this new file.
6. Put the following line in the file
nameserver 8.8.8.8 # Or use your DNS server instead of 8.8.8.8 which is a Google DNS server
7. Repeat step 3 and 4. You will see git working fine now.
Credit: https://github.com/microsoft/WSL/issues/4285#issuecomment-522201021
@YangAi

This comment has been minimized.

Copy link

@YangAi YangAi commented Oct 18, 2019

Thank you so much! I have spent two useless hours before I saw this post.

@aborruso

This comment has been minimized.

Copy link

@aborruso aborruso commented Oct 24, 2019

Thank you very much!

@jonhnes

This comment has been minimized.

Copy link

@jonhnes jonhnes commented Oct 24, 2019

Thank you so much bro!

@scroobius-pip

This comment has been minimized.

Copy link

@scroobius-pip scroobius-pip commented Nov 6, 2019

thanks alot

@jorgeag68

This comment has been minimized.

Copy link

@jorgeag68 jorgeag68 commented Nov 8, 2019

Very good.Thank you.

@jaakkouu

This comment has been minimized.

Copy link

@jaakkouu jaakkouu commented Dec 20, 2019

Thank you very much! This fixed the issue for me when I tried to start Visual Studio Code Remote Extension. For some reason connection was blocked even to local server.

@bryan-hoang

This comment has been minimized.

Copy link

@bryan-hoang bryan-hoang commented Jan 19, 2020

Thank you!

@anand-kashyap

This comment has been minimized.

Copy link

@anand-kashyap anand-kashyap commented Feb 17, 2020

I was getting timeout errors with WSL2 when connecting with mongoose.connect to mongodb atlas - this fixed that issue!!
Thanks a lot!.

@daniel5151

This comment has been minimized.

Copy link

@daniel5151 daniel5151 commented Mar 1, 2020

Thanks for the fix!

@jaakkouu

This comment has been minimized.

Copy link

@jaakkouu jaakkouu commented Mar 12, 2020

After a while of use this is not needed anymore. Not super confirmed, but updating and upgrading everything just started working normally.
Currently running Ubuntu-18.04.

@arnisjuraga

This comment has been minimized.

Copy link

@arnisjuraga arnisjuraga commented Mar 24, 2020

Thanks!

@machinista

This comment has been minimized.

Copy link

@machinista machinista commented Mar 25, 2020

This process worked for me. Thank you.
WSL2 on 19041.53

@myzinsky

This comment has been minimized.

Copy link

@myzinsky myzinsky commented Mar 26, 2020

wsl --shutdown

--shutdown does not exists... but --terminate debian does exist

@armavion

This comment has been minimized.

Copy link

@armavion armavion commented May 5, 2020

Thanks for the gist!

@fuomag9

This comment has been minimized.

Copy link

@fuomag9 fuomag9 commented Jun 2, 2020

Just a note for point 5: I had a syslink in my case, so delete that as well and replace the file in the /etc folder directly

@rmuddana

This comment has been minimized.

Copy link

@rmuddana rmuddana commented Jun 2, 2020

My case it is unable to ping to any of the addresses from WSL 2 when I connect to VPN (Cisco Anyconnect VPN client). I am running Windows 10 Enterprise Version 2004 (OS Build 19041.264)

It did work after conversion from WSL1 to 2 and before reboot. After reboot it screwed up. As i could see the default route is to reach the gateway on the host, that itself failed. Hence it is the main reason for failure.

Tried multiple things and ended up uninstalling WSL2. Afterwards I faced no issues with WSL1. When i checked the difference WSL1 is using windows routes instead of bridging with a dedicated host interface.

@pavles6

This comment has been minimized.

Copy link

@pavles6 pavles6 commented Jun 3, 2020

This is awesome, thanks!

@arturolc

This comment has been minimized.

Copy link

@arturolc arturolc commented Jun 4, 2020

After restarting (step 4) /etc/resolv.conf is symlinked to /run/resolvconf/resolv.conf. However, this directory and file do not exist. I tried generating it but ping still does not work.

@hoomanali

This comment has been minimized.

Copy link

@hoomanali hoomanali commented Jun 23, 2020

My case it is unable to ping to any of the addresses from WSL 2 when I connect to VPN (Cisco Anyconnect VPN client). I am running Windows 10 Enterprise Version 2004 (OS Build 19041.264)

It did work after conversion from WSL1 to 2 and before reboot. After reboot it screwed up. As i could see the default route is to reach the gateway on the host, that itself failed. Hence it is the main reason for failure.

Tried multiple things and ended up uninstalling WSL2. Afterwards I faced no issues with WSL1. When i checked the difference WSL1 is using windows routes instead of bridging with a dedicated host interface.

Did you ever get WSL2 working with Anyconnect? I have been experiencing your exact issue and have yet to find a solution.

Changing local DNS settings does not work as WSL2 has no route to any of the DNS servers I set. Can't ping Google's DNS nor can it ping my VPN's DNS servers.

@arturolc

This comment has been minimized.

Copy link

@arturolc arturolc commented Jun 23, 2020

@Brokeos

This comment has been minimized.

Copy link

@Brokeos Brokeos commented Jun 24, 2020

At the step 5 : Error writing /etc/resolv.conf: No such file or directory
I don't understand why I'm getting this error

@raedbenz

This comment has been minimized.

Copy link

@raedbenz raedbenz commented Jun 24, 2020

after every restart it stops working again...i have to manually edit it:
sudo nano /etc/resolv.conf
and enter nameserver 8.8.8.8.

any thought why it keeps reverting?

@dimlucas

This comment has been minimized.

Copy link

@dimlucas dimlucas commented Jul 1, 2020

@Brokeos There's a symlink called resolv.conf in that directory already.
Try removing it first and create a new resolv.conf file afterwards

@Olgagr

This comment has been minimized.

Copy link

@Olgagr Olgagr commented Jul 1, 2020

It doesn't work for me :/

@begetan

This comment has been minimized.

Copy link

@begetan begetan commented Jul 7, 2020

rm /etc/resolv.conf
echo "nameserver 8.8.8.8" > /etc/resolv.conf

@inovizz

This comment has been minimized.

Copy link

@inovizz inovizz commented Jul 10, 2020

You're Awesome, Thanks a lot!

@yukosgiti

This comment has been minimized.

Copy link

@yukosgiti yukosgiti commented Jul 10, 2020

Thanks this worked for me. However, sudo stuff made it a bit hard for me to apply these steps. So, here is the steps I took to write these files in case someone also struggles with these. (Probably there is a better way of doing this)

# steps explanations
1 cd ~/../../etc go to etc folder in WSL
2 echo "[network]" | sudo tee wsl.conf Create wsl.conf file and add the first line.
3 echo "generateResolvConf = false" | sudo tee -a wsl.conf Append wsl.conf the next line.
4 wsl --terminate Debian Terminate WSL in Windows cmd.
5 cd ~/../../etc go to etc folder in WSL
6 echo "nameserver 8.8.8.8" | sudo tee resolv.conf Create or override resolv.conf and append the line.
7 wsl --terminate Debian Terminate WSL in Windows cmd.
@JohnnyQuest1983

This comment has been minimized.

Copy link

@JohnnyQuest1983 JohnnyQuest1983 commented Jul 30, 2020

I'm not sure if Microsoft ninjad in some fixes this latest update (it didn't look like it...) but I've finally got this working for me. The main issue is the symlinked etc/resolv.conf file pointing at run/resolvconf/resolv.conf

Create wsl.conf as explained in every issue you can find about this problem:
[network]
generateResolvConf=false

This will kill run/resolvconf/resolv.conf and prevent it ever being created again.
exit or in Windows cmd wsl --terminate [YourDistroName], then load it up again.

Delete the existing resolv.conf file (which is still a symlink, but now to nowhere): sudo rm resolv.conf
Create a new resolv.conf, however you see fit. sudo touch resolv.conf is as good a method as any.
sudo vim resolv.conf to edit it and add this line: nameserver 8.8.8.8
replace 8.8.8.8 with your preferred functional nameserver.
exit or in Windows cmd wsl --terminate [YourDistroName], wsl --shutdown just to be sure that you've definitely killed everything.

Confirm that your resolv.conf changes are still in effect, or just ping a domain name and cry tears of joy after struggling to get this working for far too long.

@maor-rosenfeld

This comment has been minimized.

Copy link

@maor-rosenfeld maor-rosenfeld commented Aug 14, 2020

Add this thank you to the pile 👌

@matrwall

This comment has been minimized.

Copy link

@matrwall matrwall commented Aug 17, 2020

thank you!

@newcarrotgames

This comment has been minimized.

Copy link

@newcarrotgames newcarrotgames commented Aug 17, 2020

soo.. for me this was caused by me starting my VPN before starting WSL. I fixed it by disconnecting from the VPN, restarting WSL (wsl -t ), and then reconnecting to the VPN.

@milianj

This comment has been minimized.

Copy link

@milianj milianj commented Aug 17, 2020

yep..same for me..I have to shutdown my vpn and restart WSL for dns to work.
...removing symlink for resolv.conf still recreates it with a nameserver pointing to 172.22.224.1. After shutting down the VPN, DNS resolution works fine with my distro (Ubuntu 20.04)

@alejoasotelo

This comment has been minimized.

Copy link

@alejoasotelo alejoasotelo commented Aug 25, 2020

thank you!!!

@kmin135

This comment has been minimized.

Copy link

@kmin135 kmin135 commented Aug 30, 2020

thank you. it works. (wsl2 ubuntu20.04)

@leonheess

This comment has been minimized.

Copy link

@leonheess leonheess commented Sep 4, 2020

Whenever I load it up again it will have reset resolv.conf and the nameserver

@sc0ttwad3

This comment has been minimized.

Copy link

@sc0ttwad3 sc0ttwad3 commented Sep 5, 2020

The wsl.conf and resolv.conf configuration methods are no longer working—for me—with the latest insider build 20206, but there's a workaround. So far, the word seems to be that it Looks like it's a Hyper-V issue. Windows Sandbox and other Hyper-V VMs also cannot access the internet. #5805

Here's the simplified workaround that's been working for me after any loss of WSL 2 network connectivity: ( thanks @onyxg for the tips )

Use WinKey + x to bring up the quick system menu and select Device Manager.

Enable 'Show hidden devices'

image

Open 'Network Adapters'

image

Disable, and then Enable, each Hyper-V Virtual Switch Extension Adapter in the list.

Then check if your network connectivity and DNS resolution have returned.

@pe0mot

This comment has been minimized.

Copy link

@pe0mot pe0mot commented Sep 18, 2020

Thanks, and reading your advice is important: If it exists, replace existing
So do NOT change the existing one as it is a symbolic link!

@fgbaezp

This comment has been minimized.

Copy link

@fgbaezp fgbaezp commented Sep 23, 2020

You're a genius, thanks a lot!!!

@taichoup

This comment has been minimized.

Copy link

@taichoup taichoup commented Sep 28, 2020

Thank you so much!

@coltenkrauter

This comment has been minimized.

Copy link
Owner Author

@coltenkrauter coltenkrauter commented Sep 29, 2020

Love all of the conversation and suggestions here. Much appreciated.

@whrocha

This comment has been minimized.

Copy link

@whrocha whrocha commented Sep 29, 2020

I can't run a single curl www.google.com in WSL 2

➜  rochawi cat /etc/wsl.conf
[network]
generateResolvConf = false
➜  rochawi cat /etc/resolv.conf
nameserver 8.8.8.8
➜  rochawi curl www.google.com
curl: (6) Could not resolve host: www.google.com

all day working on it and I don't how to fix it.

@pe0mot

This comment has been minimized.

Copy link

@pe0mot pe0mot commented Sep 30, 2020

@jcefoli

This comment has been minimized.

Copy link

@jcefoli jcefoli commented Oct 1, 2020

I cannot get /etc/resolv.conf to be writable, even as sudo. I've followed all the instructions and even reinstalled Ubuntu / restarted LxssManager, rebooted... Nothing is working for me.

"/etc/resolv.conf" E166: Can't open linked file for writing

@pe0mot

This comment has been minimized.

Copy link

@pe0mot pe0mot commented Oct 1, 2020

@Aldairng

This comment has been minimized.

Copy link

@Aldairng Aldairng commented Oct 5, 2020

thank you. it works. (wsl2 ubuntu20.04).

@amd989

This comment has been minimized.

Copy link

@amd989 amd989 commented Oct 5, 2020

#womm

@wozniaklukasz

This comment has been minimized.

Copy link

@wozniaklukasz wozniaklukasz commented Oct 5, 2020

After some problems with resolv.conf it finally works. Thanks.

@Aldairng

This comment has been minimized.

Copy link

@Aldairng Aldairng commented Oct 6, 2020

The solution in this post works but, I just figure out when I start Citrix VPN service, the Ethernet adapter vEthernet (WSL) goes down.
I disconect the vpn but the adapter doesn't start. Neither wsl--shutdown, neither @sc0ttwad3 workaround works after that. Just restarting my pc the adapter starts again.
I think is the same issue as @rmuddana.

image

@rmuddana

This comment has been minimized.

Copy link

@rmuddana rmuddana commented Oct 6, 2020

Initially I was excited about using WSL2 but after these basic connectivity issues, downgraded the distros to WSL1. After that internet connectivity is fine with VPN.

@Aldairng

This comment has been minimized.

Copy link

@Aldairng Aldairng commented Oct 9, 2020

@rmuddana

This comment has been minimized.

Copy link

@rmuddana rmuddana commented Oct 9, 2020

Oh! Cool. Did you get a chance to verify it? No mention about VPN in the release notes though!

@parayiv

This comment has been minimized.

Copy link

@parayiv parayiv commented Oct 20, 2020

Windows 10 19041
I tried all the solution for removing and updating /etc/resolv.conf and creating wsl.conf file.
Unfortunately nothing work for me :)
Anyone can help

@Tiberriver256

This comment has been minimized.

Copy link

@Tiberriver256 Tiberriver256 commented Oct 22, 2020

Thank you

@moonrockfamily

This comment has been minimized.

Copy link

@moonrockfamily moonrockfamily commented Oct 27, 2020

Evidence

Thought I'd share my experience/findings.

Symptoms

The issue presents itself as the inability to resolve a hostname that is known via the VPN interface.

Default DNS server

$ nslookup private-hostname.company.ca
Server:         192.168.1.1
Address:        192.168.1.1#53

** server can't find private-hostname.company.ca: NXDOMAIN

Specific DNS server

$ nslookup private-hostname.company.ca 10.2.2.10
Server:         10.2.2.10
Address:        10.2.2.10#53

Name:   private-hostname.company.ca
Address: 10.2.103.142
Name:   private-hostname.company.ca
Address: 10.2.103.143
Name:   private-hostname.company.ca
Address: 10.2.103.141

Configuration Evidence

Resolv.conf from WSL/Ubuntu

cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 192.168.1.1
nameserver 10.2.2.10
nameserver 10.2.2.11
search Home

Host Network

Ethernet adapter Ethernet 4:

   Connection-specific DNS Suffix  . : 
   Description . . . . . . . . . . . : PANGP Virtual Ethernet Adapter #2
   Physical Address. . . . . . . . . : 99999999999999
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   IPv4 Address. . . . . . . . . . . : 172.30.251.206(Preferred) 
   Subnet Mask . . . . . . . . . . . : 255.255.255.255
   Default Gateway . . . . . . . . . : 
   DHCPv6 IAID . . . . . . . . . . . : 134369345
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-27-22-3E-FA-E8-4E-06-7C-72-41
   DNS Servers . . . . . . . . . . . : 10.2.2.10
                                       10.2.2.11
   NetBIOS over Tcpip. . . . . . . . : Enabled

...
Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . : Home
   Description . . . . . . . . . . . : Realtek 8812BU Wireless LAN 802.11ac USB NIC
   Physical Address. . . . . . . . . : 9999999999999999
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::dbf:b6d3:2912:b843%10(Preferred) 
   IPv4 Address. . . . . . . . . . . : 192.168.1.24(Preferred) 
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Lease Obtained. . . . . . . . . . : Monday, October 26, 2020 3:33:56 PM
   Lease Expires . . . . . . . . . . : Wednesday, October 28, 2020 7:38:35 AM
   Default Gateway . . . . . . . . . : 192.168.1.1
   DHCP Server . . . . . . . . . . . : 192.168.1.1
   DHCPv6 IAID . . . . . . . . . . . : 703090182
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-27-22-3E-FA-E8-4E-06-7C-72-41
   DNS Servers . . . . . . . . . . . : 192.168.1.1
   NetBIOS over Tcpip. . . . . . . . : Enabled
Ethernet adapter vEthernet (Default Switch):

   Connection-specific DNS Suffix  . : 
   Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter
   Physical Address. . . . . . . . . : 00-15-5D-5A-83-B1
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::e9f9:eb19:f9e6:c74b%44(Preferred) 
   IPv4 Address. . . . . . . . . . . : 172.17.94.81(Preferred) 
   Subnet Mask . . . . . . . . . . . : 255.255.255.240
   Default Gateway . . . . . . . . . : 
   DHCPv6 IAID . . . . . . . . . . . : 738202973
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-27-22-3E-FA-E8-4E-06-7C-72-41
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled
Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . : 
   Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter #2
   Physical Address. . . . . . . . . : 00-15-5D-B3-6E-DE
   DHCP Enabled. . . . . . . . . . . : No
   Autoconfiguration Enabled . . . . : Yes
   Link-local IPv6 Address . . . . . : fe80::2802:cde6:6143:b330%57(Preferred) 
   IPv4 Address. . . . . . . . . . . : 172.17.89.97(Preferred) 
   Subnet Mask . . . . . . . . . . . : 255.255.255.240
   Default Gateway . . . . . . . . . : 
   DHCPv6 IAID . . . . . . . . . . . : 956306781
   DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-27-22-3E-FA-E8-4E-06-7C-72-41
   DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
                                       fec0:0:0:ffff::2%1
                                       fec0:0:0:ffff::3%1
   NetBIOS over Tcpip. . . . . . . . : Enabled

My interpretation

Based on the above evidence it seems WSL translates via the Hyper-V Virtual Ethernet Adapter (Default Switch) the IPv6 DNS server addresses to the origin adapter addresses (IPv4) and writes them to the linked /etc/resolv.conf file. The Hyper-V Ethernet Adapter network appears to aggregate all network interfaces; I'm assuming to maximize its ability to use the host network abilities (including VPN networks), as evident by the DNS Servers list.

My understanding of the Issue

The 'Home' network interface has DNS Servers = 192.168.1.1 and does not know about the private VPN DNS. The WSL/ubuntu etc/resolv.conf has this DNS server first in the list. This server responds with an unknown host satisfying the request, but not permitting the alternate servers to take a crack at resolving.

Solution

I'll post an update if I find a work-around aside from manually managing the etc/resolve.conf or hacking by local home network. I'd prefer to leverage the built-in HyperV representation of the host network configuration with either a one-time config change or WSL runtime/setup tweak.

@TravelingTechGuy

This comment has been minimized.

Copy link

@TravelingTechGuy TravelingTechGuy commented Nov 11, 2020

Thanks for this tip!
I had a problem updating my installed packages when connected to VPN. Could not understand what was blocking access. This tip fixed it.
Cheers, mate!

@Draccoz

This comment has been minimized.

Copy link

@Draccoz Draccoz commented Nov 13, 2020

Works like a charm!

@blindman78

This comment has been minimized.

Copy link

@blindman78 blindman78 commented Nov 22, 2020

Hello , Thank you

sudo rm -Rf /etc/resolv.conf if you have problem for save file " 5. "

@carolynzy

This comment has been minimized.

Copy link

@carolynzy carolynzy commented Nov 24, 2020

Thank you! It works for me!

@jainsamyak

This comment has been minimized.

Copy link

@jainsamyak jainsamyak commented Nov 27, 2020

Thanks so much for this fix! Saved me hours of head scratching...

@jonathanzuramski

This comment has been minimized.

Copy link

@jonathanzuramski jonathanzuramski commented Nov 27, 2020

Does seem to work to fix local DNS, any ideas on that?

@MartinCaccia

This comment has been minimized.

Copy link

@MartinCaccia MartinCaccia commented Nov 29, 2020

My case it is unable to ping to any of the addresses from WSL 2 when I connect to VPN (Cisco Anyconnect VPN client). I am running Windows 10 Enterprise Version 2004 (OS Build 19041.264)

It did work after conversion from WSL1 to 2 and before reboot. After reboot it screwed up. As i could see the default route is to reach the gateway on the host, that itself failed. Hence it is the main reason for failure.

Tried multiple things and ended up uninstalling WSL2. Afterwards I faced no issues with WSL1. When i checked the difference WSL1 is using windows routes instead of bridging with a dedicated host interface.

Exactly the same case, i have to downgrade to WSL1 (wsl --set-version Ubuntu 1), i tried a lot of solutions with WSL2 but i didnt get it.

@rmuddana

This comment has been minimized.

Copy link

@rmuddana rmuddana commented Nov 29, 2020

Below steps worked for me: Try it.

Step #1 Change the adapter priorities from admin power shell

PS> Get-NetIPInterface -InterfaceAlias "vEthernet (WSL)" | Set-NetIPInterface -InterfaceMetric 1 
PS> Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000

Step #2 Add VPN DNS servers to /etc/resolv.conf in WSL
You can get DNS server info using below command:
PS> Get-DnsClientServerAddress -AddressFamily ipv4 | Select-Object -ExpandProperty ServerAddresses

@MartinCaccia

This comment has been minimized.

Copy link

@MartinCaccia MartinCaccia commented Nov 30, 2020

Problem Resolved with windows 10 and Cisco AnyConnect vpn

Well the first thing i realised is the problem is with the WSL 2 if u downgrade to WSL 1 (wsl --set-version Ubuntu 1) u dont have any problem with connection.

Thats steps worked for me:

1 cd ~/../../etc (go to etc folder in WSL).
2 echo "[network]" | sudo tee wsl.conf (Create wsl.conf file and add the first line).
3 echo "generateResolvConf = false" | sudo tee -a wsl.conf (Append wsl.conf the next line).
4 wsl --terminate Debian (Terminate WSL in Windows cmd, in case is Ubuntu not Debian).
5 cd ~/../../etc (go to etc folder in WSL).
6 sudo rm -Rf resolv.conf (Delete the resolv.conf file).
7 In windows cmd, ps or terminal with the vpn connected do: Get-NetIPInterface or ipconfig /all for get the dns primary and
secondary.
8 With the dns primary and secondary getted replace the numbers in the next step in the X.X.X.X
9 echo "nameserver X.X.X.X" | sudo tee resolv.conf (Create resolv.conf and append the line.)
10 echo "nameserver X.X.X.X" | sudo tee -a resolv.conf (Append the line in resolv.conf)
11 wsl --terminate Debian (Terminate WSL in Windows cmd, in case is Ubuntu not Debian).
12 sudo chattr +i resolv.conf
13 And finally in windows cmd, ps or terminal:
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000

Credits to @yukosgiti, @machuu and @AlbesK:
microsoft/WSL#4277
microsoft/WSL#4246

@carolynzy

This comment has been minimized.

Copy link

@carolynzy carolynzy commented Dec 1, 2020

I just want to share some of my experience about this issue. The solution in this post solve my connection issue at first. However, I encounted another problem afterwards. I couldn't use X11 anymore, which gives me this error message:

Exception in thread "main" java.awt.AWTError: Can't connect to X11 window server using ':0' as the value of the DISPLAY variable.

Then I googled this and get solutions about setting up X11 server in WSL2. So I add the following line to my ~/.bashrc:
export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0.0
export LIBGL_ALWAYS_INDIRECT=1

I tried a couple of other solutions as well, including:

  1. install VcXsrv

  2. tick the box before "Disable access control" in Xlaunch settings.

and also changed my Windows Defend Firewall settings according to this post:
https://github.com/cascadium/wsl-windows-toolbar-launcher#firewall-rules
and this post:
https://stackoverflow.com/questions/61110603/how-to-set-up-working-x11-forwarding-on-wsl2

However, this further gives me anothe error message:
Exception in thread "main" java.awt.AWTError: Can't connect to X11 window server using '8.8.8.8:0' as the value of the DISPLAY variable.

Then I thought the changes I made previously caused the problem. So I changed it back to the default one, which is:
generateResolvConf = true

Now I could get nameserver 172.27.112.1 in /etc/resolv.conf file.

Miraculously, this solved the X11 connection and also the sudo apt-get update issues at the same time. I have tried so many ways that I hope I didn't missing anything. I guess this may have something to do with the firewall settings but I'm not sure. If you have the same problem I hope this will could be helpful.

@artemrizhov

This comment has been minimized.

Copy link

@artemrizhov artemrizhov commented Dec 3, 2020

Disabling Windows Brandmauer for public networks resolves any issues with connection to the host machine, both to DNS service and to X server.

What about grepping /etc/resolv.conf, I prefer to substitute this with the folloing line, so that connection to the X server does not depent on DNS settings.

export DISPLAY=$(netstat -rn | grep -o "^0.0.0.0 \+\([0-9]\.\?\)\+" | grep -o "[^ ]\+$"):0

Does anybody know how to make the WSL network treated at local instead of public? There should be some settings, maybe in Windows registry.

@DarkenedOrigins

This comment has been minimized.

Copy link

@DarkenedOrigins DarkenedOrigins commented Dec 13, 2020

Thank you!

@jeanmichel4121

This comment has been minimized.

Copy link

@jeanmichel4121 jeanmichel4121 commented Dec 20, 2020

Problem Resolved with windows 10 and Cisco AnyConnect vpn

Well the first thing i realised is the problem is with the WSL 2 if u downgrade to WSL 1 (wsl --set-version Ubuntu 1) u dont have any problem with connection.

Thats steps worked for me:

1 cd ~/../../etc (go to etc folder in WSL).
2 echo "[network]" | sudo tee wsl.conf (Create wsl.conf file and add the first line).
3 echo "generateResolvConf = false" | sudo tee -a wsl.conf (Append wsl.conf the next line).
4 wsl --terminate Debian (Terminate WSL in Windows cmd, in case is Ubuntu not Debian).
5 cd ~/../../etc (go to etc folder in WSL).
6 sudo rm -Rf resolv.conf (Delete the resolv.conf file).
7 In windows cmd, ps or terminal with the vpn connected do: Get-NetIPInterface or ipconfig /all for get the dns primary and
secondary.
8 With the dns primary and secondary getted replace the numbers in the next step in the X.X.X.X
9 echo "nameserver X.X.X.X" | sudo tee resolv.conf (Create resolv.conf and append the line.)
10 echo "nameserver X.X.X.X" | sudo tee -a resolv.conf (Append the line in resolv.conf)
11 wsl --terminate Debian (Terminate WSL in Windows cmd, in case is Ubuntu not Debian).
12 sudo chattr +i resolv.conf
13 And finally in windows cmd, ps or terminal:
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000

Credits to @yukosgiti, @machuu and @AlbesK:
microsoft/WSL#4277
microsoft/WSL#4246

Thank you !!

@Rshep3087

This comment has been minimized.

Copy link

@Rshep3087 Rshep3087 commented Dec 21, 2020

Thank you for this!

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Dec 31, 2020

Thanks so much! Fixed my problem! 👍

@santolucito

This comment has been minimized.

Copy link

@santolucito santolucito commented Jan 5, 2021

still the right fix more than a year later

@haxpor

This comment has been minimized.

Copy link

@haxpor haxpor commented Jan 7, 2021

MartinCaccia's solution works for me. Thanks!

@mgkmarco

This comment has been minimized.

Copy link

@mgkmarco mgkmarco commented Jan 7, 2021

Saved my soul. Thanks

@armenic

This comment has been minimized.

Copy link

@armenic armenic commented Jan 12, 2021

Version 1909 (OS Build 18363.1139), WSL 2

Nothing worked for me. I can not update Windows since this is a company laptop. I had to go back to WSL 1. I will ping back in a year or so when we get another update from our company IT :(

@jindalsyogesh

This comment has been minimized.

Copy link

@jindalsyogesh jindalsyogesh commented Jan 14, 2021

Thank you so much. Very good solution.

@mwoodpatrick

This comment has been minimized.

Copy link

@mwoodpatrick mwoodpatrick commented Jan 17, 2021

with windows 10 pro version 10.0.21292 Build 21292 if I set "generateResolvConf = false" in /etc/wsl.conf , then shutdown & restart wsl2 any /etc/resolv.conf I have created is removed! Is anyone else seeing this?

@bburket

This comment has been minimized.

Copy link

@bburket bburket commented Jan 18, 2021

with windows 10 pro version 10.0.21292 Build 21292 if I set "generateResolvConf = false" in /etc/wsl.conf , then shutdown & restart wsl2 any /etc/resolv.conf I have created is removed! Is anyone else seeing this?

I am now seeing this as well. DNS is totally busted. I can manually create the resolv.conf, but as soon as I reset its gone again

@praveenmak

This comment has been minimized.

Copy link

@praveenmak praveenmak commented Jan 19, 2021

I am using Windows 10 20H2 build 19042.746 (Enterprise version) and use Any connect VPN. I followed all the steps mentioned above and none of them worked for me.

Switching back to WSL1. Not worth spending time on this. @microsoft, please look into this. Why MS always make me so unproductive. I spend hours and hours on just using some MS software. Be it BSODs , Visual studio slowness and now WSL2 issue.

@machuu

This comment has been minimized.

Copy link

@machuu machuu commented Jan 19, 2021

@praveenmak,

I made a gist here with some alternate steps, if you want to give it another shot.

@Lod3

This comment has been minimized.

Copy link

@Lod3 Lod3 commented Jan 22, 2021

I disabled ipv6 on vEthernet (wsl) adapter en Wifi adapter in Windows.
Then I entered the cloudflare dns for both the adapters and followed these instructions and it worked: microsoft/WSL#1350

@gstewart

This comment has been minimized.

Copy link

@gstewart gstewart commented Jan 22, 2021

@mwoodpatrick @bburket - have you tried doing sudo chattr +i /etc/resolv.conf after creating the resolv.conf? This prevented it from being removed for me.

@coltenkrauter

This comment has been minimized.

Copy link
Owner Author

@coltenkrauter coltenkrauter commented Jan 23, 2021

Thanks for sharing!
@gstewart, for context, can you share what version+build of Windows you are using?

@fbergmann

This comment has been minimized.

Copy link

@fbergmann fbergmann commented Jan 27, 2021

The approach in:

https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8ca6#gistcomment-3443182

was the only one working for me (20H2, 19042.746).

@TheDucke

This comment has been minimized.

Copy link

@TheDucke TheDucke commented Jan 28, 2021

Thank you!!!

@matrwall

This comment has been minimized.

Copy link

@matrwall matrwall commented Feb 9, 2021

Amazing! Thank you!

@wanieldilson

This comment has been minimized.

Copy link

@wanieldilson wanieldilson commented Feb 12, 2021

I used my local router nameserver 192.168.1.254 which also did the trick.

@mehrdadnd

This comment has been minimized.

Copy link

@mehrdadnd mehrdadnd commented Feb 15, 2021

I found this solution to work very well. Is important to shutdown WSL2 in a correct way so it accepts the changed settings.
do above or here settings. then shutdown WSL via command from cmd or powershell .

wsl.exe --terminate NAME_OF_YOUR_LINUX

and open a WSL one more time, this time the settings should be persisted as it supposed.

@Snapperfish

This comment has been minimized.

Copy link

@Snapperfish Snapperfish commented Feb 24, 2021

Trying the above commands wiped out my existing wsl.conf file. I did not back it up, but did notice that it had the following content before it was wiped.

[user]
<my username>

After killing wsl and restarting by clicking the LTS app in the taskbar, wsl was no longer started with my user (but root instead).

Restoring the above in the file did not fix the problem.

Eventually, I found that I had to run the following from PowerShell ubuntu2004 config --default-user <my username>. Then startup was restored to my user again.

@alexandermorgan

This comment has been minimized.

Copy link

@alexandermorgan alexandermorgan commented Mar 1, 2021

@Brokeos There's a symlink called resolv.conf in that directory already.
Try removing it first and create a new resolv.conf file afterwards

How do we distinguish between the symlink resolv.conf and the actual resolv.conf? I don't see how to delete the symlink version if I have one. I only see one resolv.conf file in the etc/ directory, and then also a resolvconf/ directory. Adding namespace 8.8.8.8 to the resolv.conf file doesn't do anything for me.

@amegbor

This comment has been minimized.

Copy link

@amegbor amegbor commented Mar 12, 2021

Thanks, this helped me.

@tylercubell

This comment has been minimized.

Copy link

@tylercubell tylercubell commented Mar 19, 2021

Echoing what @JohnnyQuest1983 suggested above.

For some reason, wsl --terminate distro_here didn't do the trick for me but wsl --shutdown did.

@Vac1911

This comment has been minimized.

Copy link

@Vac1911 Vac1911 commented Mar 22, 2021

Did "Original resoltuion". Worked like a charm. Thank you!

@amitca71

This comment has been minimized.

Copy link

@amitca71 amitca71 commented Mar 26, 2021

thanks alot!!

@babuvignesh

This comment has been minimized.

Copy link

@babuvignesh babuvignesh commented Apr 1, 2021

Thanks you very much

@shikhar-saxena

This comment has been minimized.

Copy link

@shikhar-saxena shikhar-saxena commented Apr 9, 2021

Thank you so much!!

@Juan-Robledo

This comment has been minimized.

Copy link

@Juan-Robledo Juan-Robledo commented Apr 10, 2021

muchas gracias. pude volver a tener el VSC en remoto con ubuntu.

@tsaber

This comment has been minimized.

Copy link

@tsaber tsaber commented Apr 15, 2021

setting the don't generate resolv.conf option in wsl.conf and then setting nameserver to 192.168.0.1 (router ip) in the resolv.conf solved the problem for me.

@mcianc

This comment has been minimized.

Copy link

@mcianc mcianc commented Apr 16, 2021

it worked for me as well! Thanks a lot

@shyney7

This comment has been minimized.

Copy link

@shyney7 shyney7 commented May 11, 2021

after changing the nameserver to be able to update my packages I cant use my X-Server anymore. Even after setting the X-Server Ip manually in the .bashrc with export DISPLAY=127.0.0.1:0.0 I keep getting errors like "Could not connect to any X display."

@dons20

This comment has been minimized.

Copy link

@dons20 dons20 commented May 14, 2021

Thank you so much, I spent the better half of a day trying to figure out what to do about these connection issues and finally it's resolved.

@frenchLineCigar

This comment has been minimized.

Copy link

@frenchLineCigar frenchLineCigar commented May 14, 2021

Thank you for your details!

@prgeor

This comment has been minimized.

Copy link

@prgeor prgeor commented May 16, 2021

Thanks so much

@mitrajparmar93

This comment has been minimized.

Copy link

@mitrajparmar93 mitrajparmar93 commented May 19, 2021

This solution stops working after I reboot. Anyone else face the same issue? The resolv.conf is still present, the wsl.conf file is still present as well.

@chenhayat

This comment has been minimized.

Copy link

@chenhayat chenhayat commented Jun 6, 2021

if you have any permission issues with removing the file in (6) execute the following (change the file to be mutable):
sudo chattr -i resolv.conf

@powellblyth

This comment has been minimized.

Copy link

@powellblyth powellblyth commented Jun 10, 2021

Thank you. worked here

@wengm

This comment has been minimized.

Copy link

@wengm wengm commented Jun 16, 2021

Doesn't work for me. Have to switch to WSL 1.

@naguilar12

This comment has been minimized.

Copy link

@naguilar12 naguilar12 commented Jun 24, 2021

It worked, thanks for sharing!

@HiroForYou

This comment has been minimized.

Copy link

@HiroForYou HiroForYou commented Jun 28, 2021

Thanks this worked for me. However, sudo stuff made it a bit hard for me to apply these steps. So, here is the steps I took to write these files in case someone also struggles with these. (Probably there is a better way of doing this)

steps explanations

1 cd ~/../../etc go to etc folder in WSL
2 echo "[network]" | sudo tee wsl.conf Create wsl.conf file and add the first line.
3 echo "generateResolvConf = false" | sudo tee -a wsl.conf Append wsl.conf the next line.
4 wsl --terminate Debian Terminate WSL in Windows cmd.
5 cd ~/../../etc go to etc folder in WSL
6 echo "nameserver 8.8.8.8" | sudo tee resolv.conf Create or override resolv.conf and append the line.
7 wsl --terminate Debian Terminate WSL in Windows cmd.

great, this worked for me, tks

@thomasfrobieter

This comment has been minimized.

Copy link

@thomasfrobieter thomasfrobieter commented Jun 30, 2021

If you need to change it again afterwards you'll get:

rm: cannot remove 'resolv.conf': Operation not permitted

Fix it by:

  1. lsattr /etc/resolv.conf
  2. sudo chattr -i /etc/resolv.conf
  3. lsattr /etc/resolv.conf

(https://support.tools/post/fix-stuck-resolv-conf/)

@sabotagebeats

This comment has been minimized.

Copy link

@sabotagebeats sabotagebeats commented Jul 18, 2021

At the step 5 : Error writing /etc/resolv.conf: No such file or directory
I don't understand why I'm getting this error

are you using sudo?

This solution stops working after I reboot. Anyone else face the same issue? The resolv.conf is still present, the wsl.conf file is still present as well.

I also have this issue however for me the resolv.conf disappears on each reboot and the steps need to be done again.

@cameroncuster

This comment has been minimized.

Copy link

@cameroncuster cameroncuster commented Jul 19, 2021

This saved me lots of time, I appreciate your efforts.

@xXGoziXx

This comment has been minimized.

Copy link

@xXGoziXx xXGoziXx commented Jul 21, 2021

I have to enter this command in powershell/cmd everything I connect to the VPN (Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000)
Is there any workaround for this. Like maybe on connecting to VPN run above command?

@Pabosik

This comment has been minimized.

Copy link

@Pabosik Pabosik commented Jul 22, 2021

So first of all: THANK YOU SO VERY MUCH! This issue has been plaguing me for I dunno how long... I even tried the Cisco Anyconnect from Windows Store, and that works, but it keeps dropping connections, glitching, bugging out, plus it really doesn't work all that well in combination with WSL2 and vscode... basically, vscode tries to connect to it, but since the routes are changed somehow, it can't reach, so it takes 300s to timeout and only starts afterwards... horrible.

Aaanyways - I prepared a little powershell script so that you dont have to run step 13:
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000
manually every time you connect to VPN. Keep in mind that I assume you've completed the 13 steps in the original post and that the DNS is working for you. This just helps simplify the procedure a bit.

This is the script - very simple - lets call it vpn-connect.ps1:

cd 'C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client'; ./vpncli.exe connect <insert-vpn-address-here>
Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000

Now I thought that it would be nice if I could just click a shortcut and run the script as admin. You can do that by creating a shortcut with this executable:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy unrestricted -file <insert-path-to-powershell-script>\vpn-connect.ps1

Now after you've created the shortcut, the last thing you need to do is to right-click it, go to properties, click 'Shortcut' on top, and then 'Advanced' and there tick 'Run as administrator'.

That's it. I can now connect to VPN via cisco by a single double click (I still have to fill in the password though) and the DNS resolving works inside WSL2. Even my vscode now starts instantly when on VPN... pure awesomeness.

Hope this helps someone.

@hareeshghk

This comment has been minimized.

Copy link

@hareeshghk hareeshghk commented Jul 28, 2021

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment