非root用户的X11转发不起作用

9

跨发布

环境细节

服务器的/etc/ssh/sshd_config相关部分:

X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost no

客户端的$HOME/.ssh/config相关部分:

Host *
    XAuthLocation /opt/X11/bin/xauth
    ForwardX11 yes
    ForwardX11Trusted yes

在macOS High Sierra上使用XQuartz。

问题

我正在启动一个vagrant Ubuntu 18.04虚拟机。我已经添加了一个vagrant用户之外的第二个用户。

ssh -X vagrant@ubuntu-bionic xclock

当我以vagrant用户登录时,我可以使X11Forwarding正常工作。但是当我以ops用户登录时,我无法使X11Forwarding正常工作。

ssh -X ops@ubuntu-bionic xclock

X11 forwarding request failed on channel 0
Error: Can't open display:

我希望能够让ops用户正常工作。当我使用客户端时,$DISPLAY有值。以vagrant用户登录时,$DISPLAY也有值。但是以ops用户登录时,$DISPLAY的值未设置。如果我将$DISPLAY设置为与vagrant用户相同,则会出现相同的错误信息:
Error: Can't open display: localhost:10.0

X11UseLocalhost yes

Error: Can't open display: ubuntu-bionic:10.0

X11UseLocalhost no

如果我以 ops 登录,然后执行 sudo su - vagrant$DISPLAY 仍然未设置。如果我以 vagrant 登录,然后执行 sudo su - ops$DISPLAY 就会被继承。

我错过了什么,才能让它正常工作?我已经在每个用户中运行了 xhost +(包括 sudo -s root xhost +),但它仍然不起作用。

如果我在 ssh 命令中添加 -vv,则连接为 vagrant 时会看到此消息:

X11 forwarding request accepted on channel 0

作为运维

Remote: X11 forwarding disabled in user configuration file.
X11 forwarding request failed on channel 0

在Ubuntu机器上,~ops/.ssh/config文件中有什么内容?(即在/home/ops/.ssh/config中)与vagrant用户主目录中的文件有什么区别? - ivanivan
服务器vagrant/ops的主目录当前没有~/.ssh/config文件。 - John Jelinek
现在vagrant/ops都有本帖顶部提到的~/.ssh/config的内容。但仍然无法工作。 - John Jelinek
2个回答

1

我不完全理解整个话题,但对于我的非常相似的情况,创建用户s~/.Xauthority文件有所帮助。我从vagrant用户的主目录中复制了它,然后设置了新的所有权。


如果安装了 xauth,则该文件将自动创建并填充。 - trolologuy

0

在 macOS BigSur 上使用 XQuartz 2.8.1 (xorg-server 1.20.11):

将以下内容添加到我的 Vagrantfile 中解决了问题(针对 ubuntu 20.04):

  config.vm.provision "shell",
    inline: "apt-get update && apt-get upgrade -y && apt-get install xauth -y"
  config.ssh.forward_agent = true
  config.ssh.forward_x11 = true

整个Vagrantfile将是:
Vagrant.require_version ">= 2.2.3"

Vagrant.configure("2") do |config|
  config.vm.provider "virtualbox" do |v, override|
    override.vm.box = "bento/ubuntu-20.04"
  end
  config.vm.provision "shell",
    inline: "apt-get update && apt-get upgrade -y && apt-get install xauth firefox firefox-geckodriver -y"
  config.ssh.forward_agent = true
  config.ssh.forward_x11 = true
end

允许通过X服务器运行firefox -no-remote https://stackoverflow.com/。 添加-no-remote标志似乎可以减少延迟。

受Josphat Mutai的《如何在Vagrant实例上启用和使用SSH X11转发》指南的启发。


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