No internet on WSL2 while on VPN (Wireguard)

No internet on WSL2 while on VPN (Wireguard)

vim /etc/resolv.conf

 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 8.8.8.8
#nameserver 172.22.112.1

The Problem

When the host Windows 10 machine is connected to a VPN (Wireguard in my case) the WSL2 (Windows Subsystem For Linux Version 2) loses internet access.

The Solution

In my case it was pretty simple. First thing I checked was the /etc/resolv.conf and that didn't look right at all. All I had to do was fix resolv.conf and make sure that WSL does not overwrite it

Open /etc/resolv.conf in a text editor, make sure to use sudo

sudo vim /etc/resolv.conf

Comment out the existing nameserver entry and add a new DNS server of choice, I chose 8.8.8.8

nameserver 8.8.8.8

My /etc/resolv.conf looks like this

➜  ~ 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 8.8.8.8
#nameserver 192.168.192.1

To make sure that WSL does not overwrite our resolv.conf again, add the following to /etc/wsl.conf

[network]
generateResolvConf = false

If the file does not exist, create it.

Hopefully, this should resolve the issue. If not, good luck, keep Googling.