Setting Up WSL2 With Ubuntu 20.04

Tags: linux, ubuntu, wsl

Contents


I eagerly decided to try out WSL2 under the promise of massive I/O performance improvements (e.g. running git commands) and a more native Linux feel. This post goes through how I got set up with WSL2, and in particular how I solved network issues when using a VPN.

See this article by Microsoft for a comparison of WSL1 and WSL2.

There are various posts online with instructions for the initial setup of WSL2, e.g. https://www.omgubuntu.co.uk/how-to-install-wsl2-on-windows-10 - this part all went smoothly for me.

General Linux/Ubuntu Setup

I’m using Ubuntu 20.04 (‘focal’). Note this comes with python3.8 and no python2. Below are the basic setup steps I went through to install packages I regularly use - these are personal to me, and this is mainly intended to serve as an example!

Network Connectivity Problems

No Connectivity At All

Everything worked fine for me at first, but once I connected to my work VPN (Cisco Anyconnect), WSL lost any network connectivity (unable to ping IP addresses).

After lots of searching online, I’ve managed to arrive at a somewhat-workable solution. It feels a bit hacky and unfortunately I haven’t managed to automate it fully, but at least I’m able to get things working. My solution is effectively the one given in this GitHub issue comment.

It seems the main issue here is that the VPN is setting itself as the highest priority in terms of getting network packets, meaning packets never reach WSL. The following commands can be run in Windows PowerShell (in admin mode) to correct this:

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

No DNS

After running the above, I found I was able to ping IP addresses from WSL, but DNS still wasn’t working. There’s a load more discussion online about this issue (and I’ve previously hit it in WSL1 too), but my solution is given below.

In WSL, edit /etc/resolve.conf to point to the correct DNS server. By default it will point to an IP address it has for the Windows host, but when the VPN is running it should point to the VPN’s DNS server.

The correct DNS server IP address can be obtained by running ‘ipconfig /all’ in Windows PowerShell (look for entry with “Cisco AnyConnect” in the description).

I managed to boil this down to an alias in WSL (after installing dos2unix through apt):

alias powershell='/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe'
alias fixdns='powershell -Command "Get-DnsClientServerAddress -AddressFamily IPv4 | Select-Object -ExpandProperty ServerAddresses" | tac | sed "s/^/nameserver /" | dos2unix | sudo tee /etc/resolv.conf'

With this in place (e.g. in your .bashrc), DNS can be fixed by running ‘fixdns’ whenever things break (due to VPN going on/off).

Other Issues

Conclusion

I’m a bit disappointed with this transition to WSL2 not being more of a smooth ride. I think part of the problem is that I’ve got used to being able to treat Windows and WSL as running on the same machine, whereas WSL2 is more noticeably its own (virtual) machine.

The biggest issues for me are:

I may end up switching back to WSL1 because of these problems, but hopefully they will be addressed in WSL2 at some point!


Comments are currently closed