当从WSL1迁移到WSL2时,许多事情都会发生变化;显然,这也适用于X11转发。
我需要采取哪些步骤才能像在WSL1上一样在Windows 10上使用WSL2进行X11转发?
当从WSL1迁移到WSL2时,许多事情都会发生变化;显然,这也适用于X11转发。
我需要采取哪些步骤才能像在WSL1上一样在Windows 10上使用WSL2进行X11转发?
2021年Windows 10的答案
如果从resolv.conf获取IP地址无法正常工作,请查看此答案。
在WSL2中使用以下命令查找您的Windows IP地址(是的,在Linux内部有.exe文件):
ipconfig.exe
export DISPLAY=YOUR_IP_ADDRESS:0
检查您的GUI应用程序是否正常工作。
自动化对于每种情况可能会有所不同,但我会举个例子:
ipconfig.exe | grep 'IPv4 Address' | grep '10\.' | cut -d ":" -f 2 | cut -d " " -f 2
说明:我找到了所有的IPv4地址(在我的情况下是3个IP)。我知道我的IP只从“10.”开始,所以我使用第二个grep选择了这一行。接下来,我处理整行内容,只获取IP地址。
来自https://github.com/microsoft/WSL/issues/4793#issuecomment-588321333的解决方案使用VcXsrv作为X服务器,这就是我得到这个答案的地方(稍作编辑以提高可读性)。请注意,原始文档正在由其作者更新,因此不要忘记重新检查。
To make it work:
- On Windows, with the following, change
E:\VcXsrv
to where your installation is, and save it as xxx.bat in your Windows startup folder, e.g.,C:\Users\Me\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
, and you can make it run when boot if you like:@ECHO OFF REM Start WSL once to create WSL network interface wsl exit REM Find IP for WSL network interface SET WSL_IF_IP= CALL :GetIp "vEthernet (WSL)" WSL_IF_IP ECHO WSL_IF_IP=%WSL_IF_IP% setx "WSL_IF_IP" "%WSL_IF_IP%" setx "WSLENV" "WSL_IF_IP/u" REM Change E:\VcXsrv to your VcXsrv installation folder START /D "E:\VcXsrv" /B vcxsrv.exe -multiwindow -clipboard -nowgl -ac -displayfd 720 GOTO :EOF :GetIp ( aInterface , aIp ) ( SETLOCAL EnableExtensions EnableDelayedExpansion FOR /f "tokens=3 delims=: " %%i IN ('netsh interface ip show address "%~1" ^| findstr IP') DO ( SET RET=%%i ) ) ( ENDLOCAL SET "%~2=%RET%" EXIT /B )
- In WSL, edit ~/.bashrc file to add following lines:
export DISPLAY=$WSL_IF_IP:0 unset LIBGL_ALWAYS_INDIRECT
That's all to make WSL2 work automatically. The idea is to get the private LAN IP of WSL interface on Windows, and use Environment variable to pass it to WSL. WSL then updates this LAN IP to DISPLAY for X-Server connection.
剪贴板也可以很好地使用这个设置。我用Ubuntu 20.04 LTS的WSL2安装进行了测试。
export DISPLAY=$(powershell.exe -c ipconfig | grep -A4 WSL | tail -1 | awk '{ print $NF }' | tr -d '\r'):0
hostname
| grep -oP'(\s)\d+(.\d+){3}' | tail -1 | awk' {print $ NF} '| tr-d'\ r') - D. Charles Pyle我更倾向于在虚拟机中设置一个ssh服务器, 在主机上安装像Xming这样的X11服务器, 然后通过带有X11转发功能的putty连接到本地主机。 不需要调整防火墙规则,也不需要主机IP。
export DISPLAY=$HOSTNAME:0.0
export LIBGL_ALWAYS_INDIRECT=
-nowgl
。我也曾经在WSL中打开X11 GUI时遇到过困难。
我有一个问题,无法检测到正确的IP地址,有时X11服务器会出现奇怪的偏移量,有时会出现0-17的随机数。
我编写了以下脚本来自动化解决此问题,但它有一些依赖项:
source ./find_display_ip.sh
执行脚本。注意source!您将希望在运行的shell上具有DISPLAY环境变量。没有更多的话,这是脚本源代码:
#!/bin/bash
start_index=$1
start=${start_index:-0}
# check current settings
declare -i stop=0
if [ ! -z "$DISPLAY" ]; then
timeout 1s xset -display $DISPLAY q &> /dev/null;
[[ "$?" -eq 0 ]] && echo "Already Set to $DISPLAY" && stop=1;
fi
# scan displays 0-17
for port in $(seq $start 17);
do
[[ 1 -eq $stop ]] && break;
grp="ipconfig.exe | grep IPv4 | tr -d '\015' | sed 's#.*: \(.*\)\$#\1:${port}.0#;'"
for ipd in $(eval $grp)
do
echo Trying $ipd;
timeout 1s xset -display $ipd q &> /dev/null;
# command was sucessful
[[ "$?" -eq 0 ]] && export DISPLAY=$ipd && echo $ipd was set && stop=1;
##echo "Trying next IP...";
done
done
这里有两篇我写的文章,介绍了如何设置不同类型使用情况下的x11:
我发现Ubuntu有一份官方文件非常全面,供您参考。我们知道,这个提示在Debian/WSL2上同样适用。 https://wiki.ubuntu.com/WSL
感谢Kennyhyun和其他人的分享。他们所有的方法都或多或少地在我的电脑上启用了WSL2上托管的X11服务器。由于WSL2作为一个虚拟机已经不再是与WSL1相同的基础架构了。我花了一些时间去了解它。
让我简要地介绍一下如何使WSL2上的应用程序显示出来。
在 WLS2 终端上运行 'ip route'。
ip route default via a.b.c.1 dev eth0 a.b.c.0/20 dev eth0 proto kernel scope link src x.x.x.x
将 "dev eth0" 的 IP 地址添加到 "export $DISPLAY=" 中。
export $DISPLAY=a.b.c.1:0.0
运行 xming 服务器。 然后您可以运行在 WSL2 Linux 上运行的应用程序。但对于 X11,您可能需要遵循 Ubuntu 文档。
1. Start ssh service
1.1. Open WSL
1.2. Type: sudo service ssh start
2. Get Windows (WSL net) IP
2.1. Open Powershell
2.2. Type: (ipconfig | Select-String -Pattern 'WSL' -Context 1, 5).Context.PostContext | Select-String -Pattern 'IPv4'
2.3. Get the received IP
3. Set environment variable
3.1. In WSL2 terminal type: export DISPLAY=172.23.64.1:0.0 with the IP of the windows entity (2.3) instead of the place holder
4. Launch Xming
4.1. Open Xlaunch and go with the defaults In Specify parameter settings: Check No Access Control
5. Good luck!