在ssh上设置X11转发

19

我试图设置X11转发以监视嵌入式机器人平台上的视频,但似乎无法使板生成图形输出。我在Beagleboard xM上运行Arch Linux,并通过以太网直接连接(尽管我计划将来使用WiFi)。当我尝试设置DISPLAY变量时,它会接受它,但当我尝试运行X11应用程序时,它会显示:

(Object:287): Gtk-WARNING **: cannot open display: [displaynamehere]

显然[displaynamehere]是我尝试设置为显示器的任何内容,但是似乎没有显示位置起作用。奇怪的是,每当我通过ssh -X连接时,它不会给出任何错误,并且不保存我的DISPLAY变量。

(编辑) 我还检查了调试日志并得到了这个输出:

debug2: load_server_config: filename /etc/ssh/sshd_config
debug2: load_server_config: done config len = 315
debug2: parse_server_config: config /etc/ssh/sshd_config len 315
debug3: /etc/ssh/sshd_config:53 setting AuthorizedKeysFile .ssh/authorized_keys
debug3: /etc/ssh/sshd_config:75 setting ChallengeResponseAuthentication no
debug3: /etc/ssh/sshd_config:96 setting UsePAM yes
debug3: /etc/ssh/sshd_config:101 setting X11Forwarding yes
debug3: /etc/ssh/sshd_config:104 setting PrintMotd no 
debug3: /etc/ssh/sshd_config:108 setting UsePrivilegeSeparation sandbox     
debug3: /etc/ssh/sshd_config:124 setting Subsystem sftp /usr/lib/ssh/sftp-server
debug1: sshd version OpenSSH_6.3, OpenSSL 1.0.1e 11 Feb 2013
debug3: Incorrect RSA1 identifier
debug1: read PEM private key done: type RSA
debug3: Incorrect RSA1 identifier
debug3: Could not load "/etc/ssh/ssh_host_rsa_key" as a RSA1 public key
debug1: private host key: #0 type 1 RSA
debug3: Incorrect RSA1 identifier
debug1: read PEM private key done: type DSA
debug3: Incorrect RSA1 identifier
debug3: Could not load "/etc/ssh/ssh_host_dsa_key" as a RSA1 public key
debug1: private host key: #1 type 2 DSA
debug3: Incorrect RSA1 identifier
debug1: read PEM private key done: type ECDSA
debug3: Incorrect RSA1 identifier
debug3: Could not load "/etc/ssh/ssh_host_ecdsa_key" as a RSA1 public key
debug1: private host key: #2 type 3 ECDSA
debug1: rexec_argv[0]='/usr/bin/sshd'
debug1: rexec_argv[1]='-ddd'
debug3: oom_adjust_setup
Set /proc/self/oom_score_adj from 0 to -1000
debug2: fd 3 setting O_NONBLOCK
debug1: Bind to port 22 on 0.0.0.0.
Bind to port 22 on 0.0.0.0 failed: Address already in use.
debug2: fd 3 setting O_NONBLOCK
debug3: sock_set_v6only: set socket 3 IPV6_V6ONLY
debug1: Bind to port 22 on ::.
Server listening on :: port 22.

非常感谢您的帮助和建议,我已经尝试了将近一周的时间使用谷歌搜索,但仍未得到答案。

非常感谢!


2
在远程系统的sshd_config中可能已禁用X11转发,这就是为什么连接后您不会看到设置DISPLAY变量。您需要启用X11转发:X11Forwarding yes并重新启动远程系统上的ssh服务。不要使用xhost - 大多数现代X11服务器默认不侦听tcp,并启用它会带来安全风险。 - Anya Shenanigans
我知道我忘了什么。我忽略了提到我在我的sshd_config文件中启用了它。我需要启用其他X11选项吗?我已经尝试设置X11DisplayOffset和X11UseLocalhost,以查看是否可以使其工作。无论我怎么做,我的DISPLAY仍然为空。 - SuperUser320
1
你不应该需要 X11DisplayOffset 或 X11UseLocalhost,但是使用 ssh -vvv -X remotehost 进行检查,并确保它指定了发起远程 X 连接。如果这些信息表示它正在工作,则有可能是你登录的 shell 在登录时清除了 DISPLAY 环境变量。 - Anya Shenanigans
我刚刚运行了它。它似乎根本没有初始化与X服务器有关的任何内容,只是密码、密钥和连接协商。我是否可能没有所有必需的X服务器启动连接所需的软件包?非常感谢您的时间和帮助。 - SuperUser320
这是一个好问题。我运行了 top 命令,只看到一个 sshd 实例,但没有看到其他相关任务。或者说,也没有重复的任务。然而,我也没有看到任何与 X11 相关的进程在运行。 - SuperUser320
显示剩余3条评论
3个回答

37

在服务器上

编辑 /etc/ssh/sshd_config 文件:

AllowAgentForwarding yes
AllowTcpForwarding yes
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost no

重启sshd守护进程:
sudo service sshd restart
# or
sudo /etc/init.d/ssh restart
# or whatever way of restarting your box services works on your distro...

安装软件包(Ubuntu/Debian):

apt-get -y update
apt-get -y install xauth

安装软件包(适用于RHEL/CentOS):

yum -y update
yum -y install xauth

现在退出服务器:
exit

客户端

在本地设置DISPLAY环境变量:

export DISPLAY=:0.0

并启动到服务器的可信SSH连接:

ssh -Y $ssh_user@$ssh_server

使用图形应用程序来验证成功。如果需要,请安装支持X11转发的应用程序。例如:

yum -y install xclock

并执行操作:

for i in {1..3} ; do bash -c "xclock &" ; done ;

1
非常感谢。我尝试了很多答案,但是你的最终让它工作了。 - user568109
service”是每个Linux服务器都有的东西,还是你知道OP的服务器正在运行Arch Linux,因此他有这个程序?因为我在GCloud Compute Instance上尝试了sudo apt-get install service,但什么也没有得到。然后我尝试了sudo apt-cache search service,也没有找到任何内容。如果它意味着其他东西,我会感到非常愚蠢(例如,我可能会说cat [filename],但如果新手在终端中键入cat [filename],显然他们会感到困惑)。 - Nathan majicvr.com
对于我来说,/etc/init.d/ssh restart可以工作。我认为这是因为我的机器是Debian/Ubuntu(更多内容请参见此处)。 - Nathan majicvr.com
其他未来的用户可能想在前面加上 "sudo",使其变成 sudo /etc/init.d/ssh restart - Nathan majicvr.com

3
每当我遇到这个问题时,几乎总是涉及以下两个选项,我认为在进行任何配置更改(如设置DISPLAY等)之前,您应该将它们设置为以下内容。
X11Forwarding yes
X11UseLocalhost no

1

SSH应该自动设置DISPLAY。 常见问题:在/etc/ssh/sshd_config中缺少“X11Forwarding yes”

要进行调试,您可以在客户端和服务器上运行详细模式,您可能会注意到一些内容: 在“服务器”端尝试(调试模式,无守护进程)

$ /usr/sbin/sshd -d -p 222

在“客户端”:
$ ssh -v -Y phil@192.168.0.14 -p 222

偶尔我会遇到一些奇怪的事情,比如“缺少xauth”...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接