Installing and Configuring WSL v2

Installing and Configuring WSL v2

2020-11-29. Category & Tags: WSL2, Windows Sub System, Docker

Tested on Windows 10.

Pre-requirements #

  • Windows 10 May 2020 (2004) // or older Windows 10 May 2019 (1903) / or Windows 10 November 2019 (1909)
  • A computer with Hyper-V Virtualization support

Steps Overview #

  • Enable WSL 2
  • Enable ‘Virtual Machine Platform’
  • Set WSL 2 as default
  • Install a Linux distro
  • Download the lateest kernel to update [ref]

Powershell CML for Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11 #

wsl --install

Powershell CML for Win before Win-2004 (Build 19041) #

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart  # Enable WSL
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart  # Enable ‘Virtual Machine Platform’
# Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -NoRestart  # for even older win10
wsl --list --verbose
# wsl --set-version <ubuntu-dist-NAME> 2    # for even older win10 # convert current if installed
wsl --set-default-version 2  # use WSL2 for future installations

Then install from Microsoft Store. Do NOT use VPN! Do NOT install the preview version, instead, install the version that matches “ubuntu-dist-NAME” given by wsl --list --verbose.

Other versions for advanced users: wsl --list --online; wsl --install -d <dist-NAME>, e.g. wsl.exe --install --d Ubuntu-22.04

ref: OMG Ubuntu.co.uk

Change Distro / Set Default Distro for wsl & bash #

First, install a new distro (parallel to any old ones), e.g. 2204: wsl --install --d Ubuntu-22.04 Note: This may take 1~2 hours to install while showing 0.0%

Later: wsl --set-default Ubuntu-22.04 which takes effect immediately.

Install GUI #

NOT recommended !!! NOT for advanced users! sudo apt update && sudo apt install x11-apps

Debug #

cannot resovle domains #

Solution: disable auto-generation of resolv.conf by editing /etc/wsl.conf inside the WSL sys:

[network]
generateResolvConf = false

then shutdown WSL to restart WSL so this config could take effect by wsl.exe --shutdown. Later, the /etc/resolv.conf could be modified as needed.

no network #

::=============================================================================
::FIX WSL2 NETWORKING IN WINDOWS 10
::=============================================================================
cmd as admin:
wsl --shutdown
netsh winsock reset
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns
:: and restart windows

If does not work, do the following inside WSL before the above commands in Windows.

# Fix network issues
# Delete auto-generated files
[root@PC-NAME user]# rm /etc/resolv.conf || true
[root@PC-NAME user]# rm /etc/wsl.conf || true

# Enable changing /etc/resolv.conf
# Enable extended attributes on Windows drives
[root@PC-NAME user]# cat <<EOF > /etc/wsl.conf
[network]
generateResolvConf = false

[automount]
enabled = true
options = "metadata"
mountFsTab = false
EOF

# Use cn & google nameservers for DNS resolution
[root@PC-NAME user]# cat <<EOF > /etc/resolv.conf
nameserver 114.114.114.114
nameserver 8.8.4.4
EOF

ref: github

配置 WSL2 走 Windows 系统级的 TUN 网卡 #

in win:

curl https://ip.sb
notepad %userprofile%\.wslconfig
[wsl2]
networkingMode=mirrored
dnsTunneling=true
autoProxy=true
wsl --shutdown
# wait 10 sec
wsl

in wsl bash:

curl https://ip.sb
ip addr
ip route

如果成功,WSL 与 Windows 二者的出口 IP curl https://ip.sb 相同。

PS:使用 TUN,注意关闭 Windows 的 Smart Multi-Homed Name Resolution (并发 DNS)

reg add "HKLM\Software\Policies\Microsoft\Windows NT\DNSClient" /v DisableSmartNameResolution /t REG_DWORD /d 1 /f
gpupdate /force
ipconfig /flushdns

# 确认是否生效:看策略值, 看 DNS 配置:
reg query "HKLM\Software\Policies\Microsoft\Windows NT\DNSClient" 
Get-DnsClientServerAddress

ps: 这是关闭“多网卡并行/智能解析”,不是关闭 A/AAAA 并发。某些客户端自己接管 DNS,可能绕过这套系统行为;这时系统策略生效有限。在 WSL mirrored 模式下,WSL 会更接近 Windows 网络栈,所以这个设置也更有意义。

建议顺手关闭 LLMNR:

reg add "HKLM\Software\Policies\Microsoft\Windows NT\DNSClient" /v EnableMulticast /t REG_DWORD /d 0 /f
gpupdate /force

提示:检查 TUN 网卡的接口跃点数(应为最小):

Get-NetIPInterface | Sort-Object InterfaceMetric | Format-Table ifIndex,InterfaceAlias,AddressFamily,InterfaceMetric

如果跃点数(网卡优先级)有问题,可以手动调整:

Set-NetIPInterface -InterfaceAlias "TUN网卡名" -AddressFamily IPv4 -InterfaceMetric 5