WSL踩坑记录:WSL1升级WSL2,配置静态IP,解决WSLg报错 cannot open display,实现在windows10下完美运行linux的GUI程序
一、下载并安装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
https://github.com/microsoft/wslg
三、配置WSL2为静态IP
由于WSL2静态IP比较困难,所以采用变通方法:
在windows主机安装wsl2host服务,wsl2host通过注册一个服务自动修改windows主机host域名如:ubuntu2004.wsl解析到WSL的动态IP。如果运行wsl2host.exe输入的用户名密码不对,可以到计算机管理>服务>WSL2 Host 手动修改使用的用户名和密码。
tips: 现在windows10 已经可以直接使用localhost访问wsl2了,不再需要wsl2host
四、打开了Windows X Server但运行xclock依然提示cannot open display
xclock是linux下的一个可以显示时间的GUI程序,wsl2上直接用包管理工具(如apt)安装,能正常运行xclock就意味着也能运行其他的GUI程序。vcxsrv和mobaXterm都有Windows X Server的功能随便用一个就行。
如果用vcxsrv需要配置-ac参数,wsl上配置DISPLAY变量为host的IP,然后就可以在windows10下使用linux的GUI程序了。

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

浙公网安备 33010602011771号