WSL2:从Windows10BashShell运行图形Linux桌面应用程序;错误E233:无法打开显示“;

WSL2:从Windows10BashShell运行图形Linux桌面应用程序;错误E233:无法打开显示“;,windows,ubuntu,windows-subsystem-for-linux,xserver,xming,Windows,Ubuntu,Windows Subsystem For Linux,Xserver,Xming,如何从Windows10的BashShell运行图形化Linux桌面应用程序 首先,我安装了Windows Subsystem for Linux(WSL),步骤如下所示: (1) 已安装Windows 10 Pro内部预览版本19619 (2) 安装了Ubuntu Linux发行版 (3) 将分发版本从WSL 1更改为WSL 2 其次,为了从Windows 10的Bash Shell中启用图形化Linux桌面应用程序,我遵循以下步骤,如下所示: (4) 我安装了一个正在使用Xming的X服务器

如何从Windows10的BashShell运行图形化Linux桌面应用程序

首先,我安装了Windows Subsystem for Linux(WSL),步骤如下所示:

(1) 已安装Windows 10 Pro内部预览版本19619

(2) 安装了Ubuntu Linux发行版

(3) 将分发版本从WSL 1更改为WSL 2

其次,为了从Windows 10的Bash Shell中启用图形化Linux桌面应用程序,我遵循以下步骤,如下所示:

(4) 我安装了一个正在使用Xming的X服务器

(5) 安装基于GTK的vim编辑器作为测试,使用:

sudo apt get install vim gtk

(6) 设置我的显示环境变量

导出显示=: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

知道发生此错误的原因吗?

这里有一个故障排除部分,用于在wsl2上调试X11:

端口转发与WSL1不同-您的Linux服务可以通过localhost for windows访问,但反过来就不一样了


因此,您需要使用windows主机的内部IP,并调整防火墙以允许WSL网络通过

在fquinner的回答中添加

您的DISPLAYenv变量应设置为
export DISPLAY=X.X.X.X:0
,以使用Windows主机的IP地址,因为WSL2和Windows主机不在同一网络设备中,其中X.X.X.X是IP地址

您的IP地址列在名称服务器的resolv.conf中(
$cat/etc/resolv.conf


或者干脆
export DISPLAY=“`grep nameserver/etc/resolv.conf | sed's/nameserver/”`:0“
自动加载正确的IP地址。此外,您可以将其添加到
.bashrc
.zshrc
(如果您使用Zsh)

发现现在为了让GUI工作(至少在线时),您需要使用公共IP

e、 g.运行以下各项应可行:

export DISPLAY=$(dig +short myip.opendns.com @resolver1.opendns.com):0

我也遇到了同样的问题,所以我尝试了其他建议,但最终成功的是允许vcxsrv通过公共防火墙。我知道你没有使用vcxsrv,但也许对你来说也是同样的问题

安装VcXsrv,然后启用公共防火墙,如下图所示。在命令提示下使用wf.msc打开具有高级安全性的Windows Defender防火墙。然后允许这些图片中的连接

[

然后为Windows 10 WSL2运行VcXsrv

通过添加-ac addition参数运行VcXsrv,或在命令提示下键入此参数 “C:\Program 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
以下说明是从我写的一篇文章中复制和粘贴的,但它丢失了原始格式、链接和屏幕截图: 来源:


下载VcXsrv: 访问官方网站 点击“下载”


安装VcXsrv: 打开“vcxsrv-64.1.20.8.1.installer.exe” 单击“下一步” 单击“安装” 单击“关闭”


允许访问VcXsrv: 检查“专用网络” 单击“允许访问”


打开PowerShell: 按“⊞ 窗口“ 在搜索栏中输入“PowerShell” 右键单击“Windows PowerShell” 单击“以管理员身份运行”


打开WSL2: 从以下说明中复制命令 将命令粘贴到PowerShell中 按“回车”

wsl


安装Ubuntu桌面: 从以下说明中复制命令 将命令粘贴到PowerShell中 按“回车”

sudo apt——是安装ubuntu桌面


设置用户名变量: 从以下说明中复制命令 将命令粘贴到PowerShell中 按“回车”

username=$(wslvar username)


创建Ubuntu目录: 从以下说明中复制命令 将命令粘贴到PowerShell中 按“回车”

mkdir--parents/mnt/c/users/$username/.ubuntu/


打开Ubuntu目录: 从以下说明中复制命令 将命令粘贴到PowerShell中 按“回车”

cd/mnt/c/users/$username/.ubuntu


下载适用于Microsoft产品的Linux软件存储库: 从以下说明中复制命令 将命令粘贴到PowerShell中 按“回车”

Ubuntu 20.04:
wgethttps://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb --输出文档包-microsoft-prod.deb
Ubuntu 18.04:
wgethttps://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb --输出文档包-microsoft-prod.deb


为Microsoft产品安装Linux软件存储库: 从以下说明中复制命令 将命令粘贴到PowerShell中 按“回车”

sudo dpkg——安装软件包-microsoft-prod.deb


更新存储库: 从以下说明中复制命令 将命令粘贴到PowerShell中 按“回车”

sudo apt更新


安装适用于HTTPS的APT传输: 从以下说明中复制命令 将命令粘贴到PowerShell中 按“回车”

sudo apt安装——是apt传输https


更新存储库: 从以下说明中复制命令 将命令粘贴到PowerShell中 按“回车”

sudo apt更新


安装.Net: 从以下说明中复制命令 将命令粘贴到PowerShell中 按“回车”

sudo apt安装——是dotnet-sdk-5.0


将Arkane系统添加到源列表目录: 从以下位置复制命令:

# CREATE BASH SCRIPT

# Store block of text with here document
create_bash_script=$(cat << end_of_string

# Define necessary environment variables
export DISPLAY="\$(cat /etc/resolv.conf | grep nameserver | awk '{ print \$2 }'):1.0"
export DESKTOP_SESSION="ubuntu"
export GDMSESSION="ubuntu"
export XDG_SESSION_DESKTOP="ubuntu"
export XDG_CURRENT_DESKTOP="ubuntu:GNOME"
export XDG_SESSION_TYPE="x11"
export XDG_BACKEND="x11"
export XDG_SESSION_CLASS="user"
export XDG_DATA_DIRS="/usr/local/share/:/usr/share/:/var/lib/snapd/desktop"
export XDG_CONFIG_DIRS="/etc/xdg"
export XDG_RUNTIME_DIR="\$HOME/xdg"
export XDG_CONFIG_HOME="\$HOME/.config"
export XDG_DATA_HOME="\$HOME/.local/share" 
export XDG_CACHE_HOME="\$HOME/.cache"
export XDG_DESKTOP_DIR="\$HOME/Desktop"
export XDG_DOCUMENTS_DIR="\$HOME/Documents"
export XDG_DOWNLOAD_DIR="\$HOME/Downloads"
export XDG_MUSIC_DIR="\$HOME/Music"
export XDG_PICTURES_DIR="\$HOME/Pictures"
export XDG_PUBLICSHARE_DIR="\$HOME/Public"
export XDG_TEMPLATES_DIR="\$HOME/Templates"
export XDG_VIDEOS_DIR="\$HOME/Videos"

# Start desktop environment
gnome-session

end_of_string
)

# Store username environment variable in lowercase
username=$(wslvar USERNAME | awk '{ print tolower($0) }') &&

# Save block of text in bash file
echo "${create_bash_script}" > "/mnt/c/users/$username/.ubuntu/02_start_desktop.sh"


# RELOAD VCXSRV SCRIPT

# Store username environment variable in lowercase
$username = $env:username.tolower()  

# Store block of text with here-string
$reload_vcxsrv_script = @"

# Stop vcxsrv proccess that contains "1.0" in the program window title
get-process vcxsrv | where { `$_.mainwindowtitle -like "*1.0*" } | stop-process

# Start vcxsrv process in a large program window on display number one
start-process "c:\program files\vcxsrv\vcxsrv.exe" -argument ":1 -ac -nowgl -multimonitors -dpms"

"@

# Save block of text in powershell file
echo "${reload_vcxsrv_script}" > $env:userprofile/.ubuntu/reload_vcxsrv.ps1


# CREATE VISUAL BASIC SCRIPT

# Store username environment variable in lowercase
$username = $env:username.tolower()           

# Store block of text with here-string
$create_vbs_script = @"

' Run PowerShell script in background
set application = createobject("shell.application")
application.shellexecute "powershell", "-file c:\users\admin\.ubuntu\01_reload_vcxsrv.ps1", "", "", 0

' Allow PowerShell script to complete
wscript.sleep 3000

' Run Bash script in background
set shell= createobject("wscript.shell")
shell.run "wsl sudo genie -c bash /mnt/c/users/admin/.ubuntu/02_start_desktop.sh", 0

"@

# Save block of text in bash file
echo "${create_vbs_script}" > $env:userprofile/.ubuntu/03_start_ubuntu.vbs


# Store block of text with here-string
$create_shortcut_script = @"

# Define location variables
`$shortcut_location = "`$env:userprofile\.ubuntu\Ubuntu.lnk"
`$program_location = "`$env:userprofile\.ubuntu\03_start_ubuntu.vbs"

# Create shortcut
`$object = new-object -comobject wscript.shell
`$shortcut = `$object.createshortcut(`$shortcut_location)
`$shortcut.targetpath = `$program_location
`$shortcut.iconlocation = "`$env:userprofile\.ubuntu\ubuntu.ico"
`$shortcut.save()

"@

# Save block of text in powershell file
echo $create_shortcut_script > $env:userprofile/.ubuntu/04_create_shortcut.ps1

export DISPLAY=$(grep nameserver /etc/resolv.conf | awk '{print $2}'):0.0
export DISPLAY=$(route.exe print | grep 0.0.0.0 | head -1 | awk '{print $4}'):0.0
# 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