iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🌐

Fixing WSL2 internet connectivity issues when VPN is enabled

に公開

Introduction

WSL2 is convenient, but I was troubled by an issue where the internet became unusable (DNS could not be resolved) when a VPN was enabled.
I found a way to fix it, so I will summarize the method here.

Microsoft's Official Method

https://docs.microsoft.com/en-us/windows/wsl/troubleshooting#bash-loses-network-connectivity-once-connected-to-a-vpn

To summarize, it seems to suggest rewriting /etc/resolv.conf every time you connect to or disconnect from a VPN.
This is tedious, so I don't want to do it.

An Easier Way

https://gist.github.com/coltenkrauter/608cfe02319ce60facd76373249b8ca6

First, log in to WSL2 (Ubuntu 20.04.1), create a file named /etc/wsl.conf, and write the following settings.

/etc/wsl.conf
[network]
generateResolvConf = false

Next, restart WSL2 on the Windows side.

> wsl -l
> wsl -t Ubuntu

Log in to WSL2 again and delete /etc/resolv.conf.
It should likely be a broken symbolic link.

$ rm /etc/resolv.conf

Recreate /etc/resolv.conf with the following content. Any DNS server that works both when the VPN is enabled and disabled will do.

/etc/resolv.conf
nameserver 8.8.8.8

Go back to the Windows side once more and restart WSL2.

> wsl -l
> wsl -t Ubuntu

Now, the internet should be accessible both when the VPN is enabled and disabled.

Summary

WSL2 is so convenient!

Discussion