如何从Windows10的Bash Shell运行图形化Linux桌面应用程序?
首先,我按照here中显示的步骤安装了Windows Subsystem for Linux (WSL),如下所示:
(1)安装了Windows 10 Pro Insider Preview Build 19619。
(2)安装了Ubuntu Linux发行版。
(3)将发行版本从WSL 1更改为WSL 2。
其次,为了在Windows10的Bash Shell中启用图形化的Linux桌面应用程序,我按照如下here所示的步骤进行了操作:
(4)我安装了一个Xming的X Server
(5)安装基于GTK的图形化vim编辑器作为测试,使用:
sudo apt-get install vim-gtk
(6)设置我的display环境变量
export DISPLAY=:0
(7)启动应用程序
gvim
然而,这并没有给应用程序提供午餐,我得到了以下错误:
E233: cannot open display
Press ENTER or type command to continue
E852: The child process failed to start the GUI
Press ENTER or type command to continue
你知道为什么会发生这个错误吗?
发布于 2021-02-27 20:02:08
WSL2中的网络子系统与WSL1中使用的不同。您必须使用consider the differences才能访问在Windows和Linux上运行的网络应用程序:
127.0.0.1
localhost
或Linux WSL2访问应用程序,Linux在轻量级虚拟机上运行,并且具有不同的IP地址。要访问在Windows主机上运行的网络应用程序,您必须使用Windows IP地址。检查Windows主机的IP地址
有许多方法可以确定Windows主机中的IP地址。您可以在WSL Linux中运行以下命令:
cat /etc/resolv.conf
显示Windowsipconfig.exe
中eth0
接口的IP地址显示Windows hostroute.exe print
中的所有IP配置显示Windows主机中的网络路由配置
设置WSL2的显示变量
根据微软文档,您可以设置DISPLAY变量来检查/etc/resolv.conf
文件中的nameserver
。(@fqquiner和@VPraharsha已经提到这一点)
export DISPLAY=$(grep nameserver /etc/resolv.conf | awk '{print $2}'):0.0
但是,我在使用此解决方案时遇到了问题,可能是因为我将笔记本电脑与WiFi连接和多个虚拟网络一起使用。与前面的解决方案不同,我使用route.exe
并检查默认网关中使用的接口来确定Windows地址。
export DISPLAY=$(route.exe print | grep 0.0.0.0 | head -1 | awk '{print $4}'):0.0
在.profile
中设置显示变量
您可以在~/.profile
文件中设置DISPLAY变量。我使用了以下代码:
# set DISPLAY to use X terminal in WSL
# in WSL2 the localhost and network interfaces are not the same than windows
if grep -q WSL2 /proc/version; then
# execute route.exe in the windows to determine its IP address
DISPLAY=$(route.exe print | grep 0.0.0.0 | head -1 | awk '{print $4}'):0.0
else
# In WSL1 the DISPLAY can be the localhost address
if grep -q icrosoft /proc/version; then
DISPLAY=127.0.0.1:0.0
fi
fi
发布于 2020-10-07 04:19:08
有相同的问题,所以我尝试了这些其他建议,但最终有效的是允许vcxsrv通过公共防火墙。我知道你没有使用vcxsrv,但也许你也有同样的问题。
安装VcXsrv,然后启用公共防火墙,如图所示。在命令提示符下使用wf.msc打开具有高级安全性的Windows Defender防火墙。然后允许连接,如图所示。
[
然后运行适用于Windows 10 WSL2的VcXsrv from this guide
通过添加程序附加参数来运行VcXsrv,或在命令提示符处键入“C:\ -ac Files\VcXsrv\vcxsrv.exe”:0 -multiwindow -clipboard -wgl -ac
然后在您的WSL2终端中键入以下内容
export DISPLAY_NUMBER="0.0"
export DISPLAY=$(grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}'):$DISPLAY_NUMBER
export LIBGL_ALWAYS_INDIRECT=1
# OPTIONAL Set the keyboard layout to US
setxkbmap -layout us
setsid emacs
exit
发布于 2020-09-20 18:45:04
除了fquinner的答案之外,
您的DISPLAY环境变量应设置为export DISPLAY=X.X.X.X:0
以使用WSL2主机的IP地址,因为WSL2和Windows主机不在同一网络设备中,其中X.X是IP地址
并且您的IP地址列在名称服务器($ cat /etc/resolv.conf
)的resolv.conf中
或者简单地export DISPLAY="`grep nameserver /etc/resolv.conf | sed 's/nameserver //'`:0"
来自动加载正确的IP地址。此外,您还可以将其添加到.bashrc
或.zshrc
(如果您使用Zsh)
https://stackoverflow.com/questions/61860208
复制相似问题