通过 WSL 连接进行 X11 转发

Mat*_*ita 5 ssh xorg x11-forwarding windows-subsystem-for-linux

我有以下设置:

我的工作计算机运行的是 Ubuntu 18.04,具有以下内容/etc/ssh/sshd_config

#       $OpenBSD: sshd_config,v 1.101 2017/03/14 07:19:07 djm Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

Port 3022
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

PubkeyAuthentication yes

# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile     .ssh/authorized_keys .ssh/authorized_keys2

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
#PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM no

AllowAgentForwarding no
AllowTcpForwarding no
#GatewayPorts no
X11Forwarding yes
X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintLastLog yes
TCPKeepAlive no
#UseLogin no
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
Banner /etc/issue.net
PrintMotd yes

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

# override default of no subsystems
Subsystem       sftp    /usr/lib/openssh/sftp-server

# Example of overriding settings on a per-user basis
Match User me
        AllowAgentForwarding yes
        AllowTcpForwarding yes
        X11Forwarding yes
        X11UseLocalhost no
        PermitTunnel yes
Run Code Online (Sandbox Code Playgroud)

我目前无法实际访问工作计算机。所以我从家里连接到它。我的家用电脑运行的是 Windows 10,带有适用于 Linux 的 Windows Subshel​​l,运行 Ubuntu 18.04。其中~/.ssh/config包含以下内容:

# Defaults
Host *
        ForwardAgent no
        ForwardX11 no
        ForwardX11Trusted no
        User me
        Protocol 2
        ServerAliveInterval 0
        TcpKeepAlive no

# Workstation
Host workcomputer
        HostName machine.domain.com
        Port 3022
        ForwardX11 Yes
        ForwardX11Trusted Yes
Run Code Online (Sandbox Code Playgroud)

~/.profile包含行

DISPLAY=:0
Run Code Online (Sandbox Code Playgroud)

家用计算机还安装并运行了 VcXsrv。

我能够像xeyes本地一样运行应用程序,并且它们在 VcXsrv 窗口中打开得很好(尽管我需要运行它,xeyes -display :0因为xeyes似乎没有使用 $DISPLAY 变量)。但是,我无法显示任何远程 GUI 应用程序。

me@Home:~$ ssh workcomputer
me@Work:~$ echo $DISPLAY

me@Work:~$ gedit
Unable to init server: Could not connect: Connection refused

(gedit:20107): Gtk-WARNING **: 16:58:42.096: cannot open display:
Run Code Online (Sandbox Code Playgroud)

我尝试export DISPLAY=:0在远程计算机上运行,gedit​​之后确实运行了,但没有出现在本地计算机上的 VcXsrv 窗口中。我也尝试过export DISPLAY=:10,但这又导致了Connection refused. 我也尝试过连接,ssh -X但也没有成功。

我还可以尝试什么来打开本地 X 服务器上的远程 GUI 应用程序?

Mat*_*ita 2

所以我终于设法使这项工作正常进行。要做的几件事(除了原始帖子之外):

  1. 在本地和远程安装 xauth 并生成:0.

     sudo apt install xauth
     xauth generate :0 . trusted
    
    Run Code Online (Sandbox Code Playgroud)

这在远程可能需要也可能不需要,但在本地肯定需要这样做,以消除有关丢失身份验证数据的警告。在本地可能也需要,具体取决于 xauth 的安装位置。

  1. 在本地,将此确切的行(不缩短或省略任何内容)放入~/.bashrc(将其从~/.profile)中删除:

     export DISPLAY=localhost:0.0
    
    Run Code Online (Sandbox Code Playgroud)

3. 即使 和ForwardX11在您的 中ForwardX11Trusted设置为,请在连接时使用(或者如果您不信任该连接)开关:Yes~/.ssh/config-Y-X

    ssh -Y workcomputer
Run Code Online (Sandbox Code Playgroud)

我不确定为什么需要这样做,但由于某种原因,如果没有我的开关,它不会转发 X11。

更新

关于第三点,-Y我需要切换的原因是我的 ssh 配置文件顶部的默认值不正确。其中的顺序很重要(使用每个参数的第一个匹配项),因此适当的修复方法-Y是反转文件中的条目,而不是始终使用~/.ssh/config

# Workstation
Host workcomputer
        HostName machine.domain.com
        Port 3022
        ForwardX11 Yes
        ForwardX11Trusted Yes

# Defaults
Host *
        ForwardAgent no
        ForwardX11 no
        ForwardX11Trusted no
        User me
        Protocol 2
        ServerAliveInterval 0
        TcpKeepAlive no
Run Code Online (Sandbox Code Playgroud)