If you're a Windows user using WSL2, then you will run into the need of setting up WSL2 with a VPN. This guide will walk you through the steps needed to connect WSL2 to the internet through the VPN so you can access these resources.
Steps we will take:
Connect to your VPN on Windows
Find the VPN DNS Addresses
Edit the WSL2 Configuration
Edit the WSL2 DNS Configuration
Shutdown and Relaunch WSL2
(Optional) Setup Proxy
Step 1: Connect to your VPN on Windows
On Windows, connect to whichever VPN you wish to use.
Step 2: Find the VPN DNS Addresses
Open the Command Prompt (not WSL2) and type ipconfig /all
. This will list all your network settings. Scroll and look for the VPN you connected to. This will have a property called DNS Servers with 1 or more IP addresses. These IPs are the IP addresses of the VPN. Copy all the IP addresses listed.
Step 3: Edit the WSL2 Configuration
Open WSL2 and edit the file /etc/wsl.conf
by replacing all its contents with the following 2 lines. This will disable automatically generating the /etc/resolv.conf
to prevent overwriting our settings performed in step 4.
# /etc/wsl.conf
[network]
generateResolvConf = false
Step 4: Edit the WSL2 DNS Configuration
On WSL2, edit the file /etc/resolv.conf
by replacing all its contents with the following:
# /etc/resolv.conf
nameserver <VPN DNS address here. one per line>
# example:
# nameserver 147.152.22.68
# nameserver 147.152.22.4
You may encounter issues editing /etc/resolv.conf
due to it being a symlink (fake file, pointing to a real file). If this is the case, you may simply delete /etc/resolv.conf
and create it manually with the same contents in step 4.
Step 5: Shutdown and Relaunch WSL2
Shutdown WSL2 by opening Command Prompt on Windows and typing wsl --shutdown
. Then, relaunch WSL2 and run cat /etc/wsl.conf
and cat /etc/resolv.conf
. Both files should contain the contents we saved. If not, then WSL2 will have overwritten them, which will be an issue.
Step 6: Setup Proxy
To connect through a proxy, add the following line to your environment file (such as .bashrc
or .zshrc
):
# ~/.bashrc
export HTTPS_PROXY="<proxy_url>:<port>"
🙌🎉Done! You should now be able to access the internet through your chosen VPN.