Skip to content

Instantly share code, notes, and snippets.

  • Star 63 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mandeepsmagh/f1d062fc59e4e6115385c2609b5f0448 to your computer and use it in GitHub Desktop.
Assuming you have followed all the steps to install / setup WSL2 -> https://docs.microsoft.com/en-us/windows/wsl/install-win10
**Tested on Ubuntu 20.04**
Step 1 - Find out default gateway and DNS servers
- Navigate to `Control Panel\Network and Internet\Network Connections`
- Right click on relevant connection type WiFi or Ethernet and select `Status`
- Status screen will be displayed, click on `Details` button
- Network Connection details screen will be displayed
- Note down `IPv4 default gateway` and `IPv4 DNS Servers` if available
**OR**
- Run following command from powershell or cmd
`ipconfig /all`
- Note down `Default Gateway` and `DNS Servers` for relevant connection type WiFi or Ethernet
Step 2 - Create `wsl.conf` file
- Login to WSL2 and create sudo vi `/etc/wsl.conf` with following content
``
[network]
generateResolvConf = false
``
- Save file.
Step 3 - Delete current `resolv.conf` file
`sudo rm /etc/resolv.conf`
Step 4 - Create new custom `resolv.conf` file
- Use below command to create new file
`sudo vi /etc/resolv.conf`
- enter default gateway and any `IPv4 DNS servers` like below example
``
nameserver 192.168.1.254
nameserver 192.165.1.251
``
- Save file
Step 5 - Restart WSL2
- Open powershell and run following command
`wsl --shutdown`
Step 6 - Test internet connection in WSL2
- ping google.com, it should work now.
@ronnyknight45
Copy link

This fixes the issue! Thanks.

@perOeberg
Copy link

If you are unable to edit and save /etc/resolv.conf

  • check if the flag immutable is set >
    'lsattr /etc/resolv.conf'
    ----i--------------- /etc/resolv.conf

Remove the flag by
'sudo chattr -i /etc/resolv.conf'

Should work now

@Joeboyc2
Copy link

Joeboyc2 commented May 9, 2022

Worked for me too, seems like such a simple fix, tried so many over the last few months and nothing worked.

Thank you

@Joeboyc2
Copy link

Unfortunatly, this seems to have stopped working since restarting my laptop :(

I have tried running the same steps a few times, but no luck, I even converted the install back to wsl v1 and then back to v2, but still no luck

Swapping back to wsl v1 and all works fine, other than it being very slow to run stuff :(

@mandeepsmagh
Copy link
Author

Unfortunatly, this seems to have stopped working since restarting my laptop :(

I have tried running the same steps a few times, but no luck, I even converted the install back to wsl v1 and then back to v2, but still no luck

Swapping back to wsl v1 and all works fine, other than it being very slow to run stuff :(

Most likely settings are being overwritten after reboot.

microsoft/WSL#5420

Have a look at this troubleshooting guide as well in case it helps.

https://docs.microsoft.com/en-us/windows/wsl/troubleshooting

@mereldawu
Copy link

This is a life saver. Took me the whole night and this fixed it!

@bkoprivica
Copy link

Doesn't work

@bkoprivica
Copy link

This works:

  1. Download wsl-vpnkit.tar.gz from GitHub.
  2. Open a PowerShell prompt and run the following commands:
    wsl --import wsl-vpnkit $env:USERPROFILE\wsl-vpnkit wsl-vpnkit.tar.gz --version 2
    wsl -d wsl-vpnkit
  3. Now run the following command anytime you lose network connectivity when connecting to VPN:
    wsl.exe -d wsl-vpnkit service wsl-vpnkit start

Alternatively, you can also add this command to your .profile, .bashrc or /.zshrc so that is executed automatically when WSL starts.

@Elihaza
Copy link

Elihaza commented Aug 10, 2022

Works for me after rebooting. Also had a pending windows update that's been made on the same reboot. Dunno if it has anything to do. Thanks for the solution anyway !

@jordkris
Copy link

jordkris commented Nov 1, 2022

This works:

  1. Download wsl-vpnkit.tar.gz from GitHub.
  2. Open a PowerShell prompt and run the following commands:
    wsl --import wsl-vpnkit $env:USERPROFILE\wsl-vpnkit wsl-vpnkit.tar.gz --version 2
    wsl -d wsl-vpnkit
  3. Now run the following command anytime you lose network connectivity when connecting to VPN:
    wsl.exe -d wsl-vpnkit service wsl-vpnkit start

Alternatively, you can also add this command to your .profile, .bashrc or /.zshrc so that is executed automatically when WSL starts.

Thanks, you're life saver 👍

@samuelguesnier
Copy link

  1. wsl-vpnkit.tar.gz

thanks

@costyn
Copy link

costyn commented Jul 26, 2023

Just a note, the original instructions say to remove the original file resolv.conf, but it's not a file, it's a symbolic link. And yes, it's crucial to remove it, otherwise just editing the original will make the symbolic link invalid after wsl restart.

@Aegon95
Copy link

Aegon95 commented Jan 15, 2024

This works:

  1. Download wsl-vpnkit.tar.gz from GitHub.
  2. Open a PowerShell prompt and run the following commands:
    wsl --import wsl-vpnkit $env:USERPROFILE\wsl-vpnkit wsl-vpnkit.tar.gz --version 2
    wsl -d wsl-vpnkit
  3. Now run the following command anytime you lose network connectivity when connecting to VPN:
    wsl.exe -d wsl-vpnkit service wsl-vpnkit start

Alternatively, you can also add this command to your .profile, .bashrc or /.zshrc so that is executed automatically when WSL starts.

Thanks, this works for me

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