首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WSL2:从Windows10Bash Shell运行图形Linux桌面应用程序"Error E233: cannot open display“

WSL2:从Windows10Bash Shell运行图形Linux桌面应用程序"Error E233: cannot open display“
EN

Stack Overflow用户
提问于 2020-05-18 07:38:21
回答 9查看 94.6K关注 0票数 27

如何从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

然而,这并没有给应用程序提供午餐,我得到了以下错误:

代码语言:javascript
运行
复制
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

你知道为什么会发生这个错误吗?

EN

回答 9

Stack Overflow用户

发布于 2021-02-27 20:02:08

WSL2中的网络子系统与WSL1中使用的不同。您必须使用consider the differences才能访问在Windows和Linux上运行的网络应用程序:

127.0.0.1

  • In

  • 在WSL1中,Linux使用与Windows主机相同的IP地址,然后,您可以使用localhost或Linux WSL2访问应用程序,Linux在轻量级虚拟机上运行,并且具有不同的IP地址。要访问在Windows主机上运行的网络应用程序,您必须使用Windows IP地址。

检查Windows主机的IP地址

有许多方法可以确定Windows主机中的IP地址。您可以在WSL Linux中运行以下命令:

  • cat /etc/resolv.conf显示Windows
  • ipconfig.exeeth0接口的IP地址显示Windows host
  • route.exe print中的所有IP配置显示Windows主机

中的网络路由配置

设置WSL2的显示变量

根据微软文档,您可以设置DISPLAY变量来检查/etc/resolv.conf文件中的nameserver。(@fqquiner@VPraharsha已经提到这一点)

代码语言:javascript
运行
复制
export DISPLAY=$(grep nameserver /etc/resolv.conf | awk '{print $2}'):0.0

但是,我在使用此解决方案时遇到了问题,可能是因为我将笔记本电脑与WiFi连接和多个虚拟网络一起使用。与前面的解决方案不同,我使用route.exe并检查默认网关中使用的接口来确定Windows地址。

代码语言:javascript
运行
复制
export DISPLAY=$(route.exe print | grep 0.0.0.0 | head -1 | awk '{print $4}'):0.0

.profile中设置显示变量

您可以在~/.profile文件中设置DISPLAY变量。我使用了以下代码:

代码语言:javascript
运行
复制
# 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
票数 59
EN

Stack Overflow用户

发布于 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终端中键入以下内容

代码语言:javascript
运行
复制
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
票数 22
EN

Stack Overflow用户

发布于 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)

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61860208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档