一、下載並安裝Linux內核更新包
參考微軟官方文檔
二、將分發版版本設置為 WSL 1 或 WSL 2
可打開 PowerShell 命令行並輸入以下命令(僅在 Windows 內部版本 18362 或更高版本中可用),查看已安裝的Linux發行版的WSL版本:
wsl --list --verbose //可簡寫為:wsl -l -v
設置某發行版的WSL版本:
wsl --set-version <distribution name> <versionNumber>
把distribution name替換為上面wsl -l -v
顯示的分發版的實際名稱,並將versionNumber替換為數字“1”或“2”即代表需要設置為WSL1還是WSL2。
將安裝的任何新分發版的版本默認設置為 WSL2:
wsl --set-default-version 2
三、配置WSL2為靜態IP
由於WSL2靜態IP比較困難,所以采用變通方法:
在windows主機安裝wsl2host服務,wsl2host通過注冊一個服務自動修改windows主機host域名如:ubuntu2004.wsl解析到WSL的動態IP。如果運行wsl2host.exe輸入的用戶名密碼不對,可以到計算機管理>服務>WSL2 Host 手動修改使用的用戶名和密碼。
tips: 現在windows10 已經可以直接使用localhost訪問wsl2了。
四、打開了mobaXterm的X-Server但運行xclock依然提示cannot open display
root@BENHILL-DELL:/mnt/c/Users/benhill# 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 192.168.110.177
Then you'll run:
export DISPLAY=192.168.110.117:0
You may also need to launch vcxsrv with the -ac argument.
This is an area that we are working on improving in an update soon.
https://github.com/microsoft/WSL/issues/4106#issuecomment-501532834