Ubuntu (WSL2) 实例无法运行 GUI 应用程序

Ubuntu (WSL2) 实例无法运行 GUI 应用程序

我在MS-Windows 10中启用了“Windows Subsystem for Linux”功能,并安装了Ubuntu-20.04。一份使用 WSL1,一份使用 WSL2。另外,安装了 VcXsrv 工具,并且它在 MS-windows 计算机上运行。

我可以在启用了 WSL1 的实例上运行 GUI 应用程序,但是在启用了 WSL2 的实例上,我无法运行任何 GUI 应用程序。它说 ”错误:无法打开显示::0.0

请在下面找到主机和 Linux 实例的一些输出:

从 Windows 10 开始:

❯ ipconfig

Windows IP Configuration
Ethernet adapter Ethernet 4:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter Ethernet:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::102:a949:2437:16e9%55
   IPv4 Address. . . . . . . . . . . : 172.23.224.1
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . :

Wireless LAN adapter Local Area Connection* 10:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Local Area Connection* 11:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::a5b7:d11b:52b0:23d6%9
   IPv4 Address. . . . . . . . . . . : 192.168.1.4
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : fe80::1%9
                                       192.168.1.1

Ethernet adapter Bluetooth Network Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

❯ wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-20.04    Running         2
  Ubuntu          Running         1

从 Ubuntu (WSL1)- UI 应用程序工作正常(即 xclock、xeyes)

$ ifconfig -a
eth0: flags=64<RUNNING>  mtu 1500
        inet 169.254.226.136  netmask 255.255.0.0
        inet6 fe80::b0cd:dad8:df85:e288  prefixlen 64  scopeid 0xfd<compat,link,site,host>
        ether 80:19:34:d2:45:55  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=64<RUNNING>  mtu 1400
        inet 10.83.242.31  netmask 255.255.255.255
        inet6 fe80::d8f9:f80:42f7:4681  prefixlen 64  scopeid 0xfd<compat,link,site,host>
        ether 02:50:41:00:00:01  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=64<RUNNING>  mtu 1500
        inet 169.254.8.40  netmask 255.255.0.0
        inet6 fe80::41af:dac3:4f41:828  prefixlen 64  scopeid 0xfd<compat,link,site,host>
        ether 34:e6:d7:1f:97:fc  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.23.224.1  netmask 255.255.240.0  broadcast 172.23.239.255
        inet6 fe80::102:a949:2437:16e9  prefixlen 64  scopeid 0xfd<compat,link,site,host>
        ether 00:15:5d:f7:dc:5c  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth4: flags=64<RUNNING>  mtu 1406
        unspec [NONE SET]  netmask 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
        ether 00:05:9a:3c:7a:00  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth5: flags=64<RUNNING>  mtu 1300
        unspec [NONE SET]  netmask 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
        ether 02:05:85:7f:eb:80  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 1500
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0xfe<compat,link,site,host>
        loop  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wifi0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.4  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::a5b7:d11b:52b0:23d6  prefixlen 64  scopeid 0xfd<compat,link,site,host>
        ether 80:19:34:d2:45:51  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wifi1: flags=64<RUNNING>  mtu 1500
        inet 169.254.12.30  netmask 255.255.0.0
        inet6 fe80::551a:4119:9646:c1e  prefixlen 64  scopeid 0xfd<compat,link,site,host>
        ether 80:19:34:d2:45:52  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wifi2: flags=64<RUNNING>  mtu 1500
        inet 169.254.80.242  netmask 255.255.0.0
        inet6 fe80::7548:77de:4bf6:50f2  prefixlen 64  scopeid 0xfd<compat,link,site,host>
        ether 82:19:34:d2:45:51  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

$ echo $DISPLAY
:0.0

$ 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.1.1
nameserver fec0:0:0:ffff::1
nameserver fec0:0:0:ffff::2

从 Ubuntu-20.04(WSL2)开始: UI 应用程序不工作(即 xclock、xeyes)

❯ ifconfig -a
bond0: flags=5122<BROADCAST,MASTER,MULTICAST>  mtu 1500
        ether ba:e3:e3:6e:20:3d  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

dummy0: flags=130<BROADCAST,NOARP>  mtu 1500
        ether be:be:bb:8a:8f:a0  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.94.115  netmask 255.255.240.0  broadcast 172.17.95.255
        inet6 fe80::215:5dff:fe8e:25e5  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:8e:25:e5  txqueuelen 1000  (Ethernet)
        RX packets 34  bytes 4988 (4.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 936 (936.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 2  bytes 100 (100.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2  bytes 100 (100.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

sit0: flags=128<NOARP>  mtu 1480
        sit  txqueuelen 1000  (IPv6-in-IPv4)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

❯ 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.1.1
nameserver 172.17.80.1

尝试访问 GUI 应用程序时 WSL2 出错:

❯ xeyes
Error: Can't open display: :0.0

❯ echo $LIBGL_ALWAYS_INDIRECT
1

❯ echo $DISPLAY
:0.0

笔记:还尝试将不同的 IP(192.168.1.1、192.168.1.4)导出到 WSL2 实例中的 DISPLAY 变量,但没有任何帮助。

附加信息:

  • 从 WSL1 (Ubuntu) 实例中,我能够成功 ping 主机的 IP 地址 (192.168.1.4)。此外,远程登录到端口 6000 (vcxsrv) 端口也已连接。
  • 但是,从 WSL2 (Ubuntu) 实例中,我无法 ping 通主机的 IP 地址 (192.168.1.4)。另外telnet到端口6000它说没有路由。
  • 已经遵循了所有步骤在这个线程中提到其中包括添加端口 6000 的防火墙规则,但似乎并没有解决问题。

来自 WSL1:

$ telnet 192.168.1.4 6000
Trying 192.168.1.4...
Connected to 192.168.1.4.
Escape character is '^]'.

来自 WSL2:

❯ telnet 192.168.1.4 6000
Trying 192.168.1.4...
telnet: Unable to connect to remote host: No route to host

查询/帮助:

  • 网络有区别吗?关于 WSL1 和 WSL2 设置?如果是,我们如何解决我当前面临的问题以及如何在 WSL2 实例中运行 GUI 应用程序?

  • 请注意,我无法从 WSL2 实例 ping 主机。同样,从主机也无法 ping WSL2 实例。

答案1

我的.bashrc文件包含以下几行。也许看看这些export DISPLAY行并根据需要修改它们以反映您的显示。

# Added code for Windows Xwinrc
export DISPLAY=:0;
#export DISPLAY=localhost:0.0
#export LIBGL_ALWAYS_INDIRECT=1
#export GDK_DPI_SCALE=1

相关内容