Chun-Jie Liu

Set up WSL2 on windows

Chun-Jie Liu · 2022-05-19

Prerequisites

You must be running Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11, here.

WSL install

wsl --install
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
wsl --install -d Ubuntu
wsl --set-default-version 2
# then restart computer.

Install Ubuntu WSL will take few minutes. After installation finished, it requires username and password for sudo privileges.

Chagne the default Linux distribution installed

wsl -l -v
wsl -l -o

.wslconfig

[wsl2]
  processor=8
  memory=8G
  localhostForwarding=true

Install Docker WSL2 backend

Instruction refer to here

wsl -l -v
wsl --set-default-version 2
wsl --set-default Ubuntu

Manual installation steps for older version of WSL

refer to here

  1. Enable the Windows Subsystem for Linux (WSL) feature
  2. Check requirements for running WLS 2
  3. Enable Virtual Machine feature
  4. Download the Linux kernel update package
  5. Set WSL 2 as your default version

WSL and Cisco Anyconnect VPN internet issue, here and here.

Step 1

Open powershell as Admin and run the following commands, and take nots of DNS/namesever

Get-DnsClientServerAddress -AddressFamily IPv4 | Select-Object -ExpandProperty ServerAddresses
Get-DnsClientGlobalSetting | Select-Object -ExpandProperty SuffixSearchList

Step 2

Open WSL and run the following commands with previous DNS and nameservers.

sudo unlink /etc/resolv.conf

#prevent wsl2 from overwritting the resolve.conf file everytime you start wsl2

cat <<EOF | sudo tee -a /etc/wsl.conf
[network]
generateResolvConf = false
EOF

cat <<EOF | sudo tee -a /etc/resolv.conf
nameserver *.*.*.* # The company DNS/nameserver from the command in step 1
nameserver *.*.*.* # The company DNS/nameserver from the command in step 1
nameserver *.*.*.* # The company DNS/nameserver from the command in step 1
nameserver 8.8.8.8
nameserver 8.8.4.4
search domain.com # The search domain that we got from step 2
EOF

Step 3

Change Cisco Anyconnect metric from default 1 to 6000 in powershell.

Changing the Interface Metric 1 -> 6000 for AnyConnect VPN Adapter resolves the connection issue, but this has to be done after each time the VPN connects.

By default, the Interface Metrics for AnyConnect are:

Changing the Interface Metrics for AnyConnect to:

Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "Cisco AnyConnect"} | Set-NetIPInterface -InterfaceMetric 6000

Step 4

Restart WSL2 on the same powershell.

Restart-Service LxssManager