Xpra

来自 ArchWiki

来自 Xpra 的 README

Xpra 被称为 “screen for X” :它的无缝模式允许你运行 X11 程序,通常在远程主机上,将其显示定向到你的本地机器,然后在不断开与这些程序的连接的情况下,从同一台或另一台机器重新连接,而不会丢失任何状态。有效地为你提供对单个图形应用程序的远程访问。它也可以用于访问现有的桌面会话和启动远程桌面会话。
Xpra 是开源的 (GPLv2+),客户端可用于许多受支持的平台,服务器包含内置的 HTML5 客户端。Xpra 可通过各种网络协议使用,并尽最大努力适应任何网络条件。

安装

安装 软件包 xpraxpra-gitAUR,在服务器和客户端上。

用法

运行应用程序 “单次”

要在远程主机上通过 SSH 运行应用程序,而无需预先在主机上启动 xpra 服务器,你可以在客户端机器上简单地使用此命令

$ xpra start ssh:user@host --exit-with-children --start-child="command"

其中 “command” 是你将在远程主机 shell 上启动的命令。这将远程启动 xpra,并在命令退出时关闭 xpra 服务器。

如果你看到 Public Key not accepted by remote side, .ssh/config not used correctly,请尝试使用你正常的系统 ssh 客户端,而不是集成的 Paramiko SSH 客户端

$ xpra start --ssh="ssh" ssh:user@host --exit-with-children --start-child="command"

在远程主机上持久的 xpra 服务器中运行应用程序

如果想要在远程机器上拥有持久的服务器,你可以在要运行应用程序的机器上启动 xpra 服务器(这里我们使用显示器编号 7

$ xpra start :7

现在你可以启动一个应用程序,例如 firefox

$ DISPLAY=:7 firefox

或者,如果你想启动一个 screen 会话并在那里执行程序,以便能够关闭控制台

$ DISPLAY=:7 screen
[screen starts]
$ firefox

请注意,如果像这样启动 screen,则在执行程序时不必指定显示器编号。它们将自动在 xpra 显示器上运行。

运行这些命令后,你还看不到任何窗口。要真正在显示器上看到应用程序,你必须连接到 xpra 服务器。如果要连接到同一台机器上的 xpra 显示器,请像这样启动 xpra 客户端

$ xpra attach :7

或者,如果要通过 ssh 连接到远程机器

$ xpra attach ssh:user@example.com:7

启动客户端后,远程服务器显示器上运行的任何程序都会显示在本地屏幕上。要分离,请键入 ctrl-c 或使用命令

$ xpra detach ssh:user@example.com:7

程序继续在服务器上运行,你可以稍后重新连接。

你可以使用以下命令停止服务器

$ xpra stop :7

在服务器运行的机器上,或远程

$ xpra stop ssh:user@example.com:7

运行完整的桌面环境

要运行完整的桌面(在服务器端)

$ xpra start-desktop :7 --start-child=xfce4-session --exit-with-children

其中

  • :7 是 xorg DISPLAY 会话的编号
  • --start-child=xfce4-session 在 xpra 服务器上将 xfce4 会话作为子进程运行
  • --exit-with-children 意味着服务器将在会话注销后关闭(子进程退出)

要附加它(在服务器端)

$ xpra attach :7

要附加它(在客户端)

$ xpra attach ssh:user@example.com:7

要分离,请在终端上按 ctrl+c 或运行

$ xpra detach :7
提示: 屏幕分辨率可以使用 xrandr 更改。

Shadow 远程桌面

要克隆

  • 显示器必须通过与 ssh 登录相同的用户进行身份验证
  • 显示器必须在远程屏幕上显示

要在客户端 shadow 桌面,请运行此命令

$ xpra shadow ssh:DISPLAY_user@example.com:DISPLAY_number

作为 xorg 沙箱

可以使用 firejail 实现沙箱化

  • 对于应用程序沙箱(将 eth0 更改为你的接口名称或 none
    $ firejail --x11=xpra --net=eth0 firefox
  • xpra attach, 等
    $ firejail --net=eth0 xpra [...]
注意: --net=eth0 为网络创建一个单独的命名空间。这对于从 xpra 隐藏所有 xorg 或系统套接字是必要的,否则沙箱很可能无效 (issue-54#153757284)。

更多

有关完整手册,请参阅 xpra(1)

技巧和窍门

开机启动

服务器

可以使用 systemd 单元在启动时启动 xpra 服务器。

创建单元文件

/etc/systemd/system/xpra@.service
[Unit]
Description=xpra display

[Service]
Type=simple
User=%i
EnvironmentFile=/etc/conf.d/xpra
ExecStart=/usr/bin/xpra --no-daemon start ${%i}

[Install]
WantedBy=multi-user.target

现在创建配置,为每个想要拥有 xpra 显示的用户添加一行

/etc/conf.d/xpra
myusername=:7

启用 每个拥有显示器的用户的服务。在此示例中,服务将是 xpra@myusername.service

客户端

注意: 如果客户端是远程计算机,首先使用 SSH 密钥,以便能够连接到服务器而无需键入密码。阅读 SSH 密钥 以了解更多详情。
方法 1: .xinitrc

将启动连接所需的行添加到你的 ~/.xinitrc 文件中,并在行尾添加 &

确保在 exec之前添加这样的行。

例如,在远程客户端上,它可能是

~/.xinitrc
xpra attach ssh:user@example.com:7 &
方法 2: systemd 用户会话

配置你的会话以使用 systemd 用户会话。阅读 Systemd/User 以了解详情。

注意: 确保你理解 systemd 用户会话 服务和常规 systemd 服务之间的区别。再次阅读 Systemd/User 以了解详情。

创建以下服务单元

~/.config/systemd/user/xpra-client@.service
[Unit]
Description=xpra client

[Service]
Type=simple
EnvironmentFile=%h/.config/conf/xpra_client
ExecStart=/usr/bin/xpra attach %i $OPTS

[Install]
WantedBy=default.target

创建配置文件,使用你想要的选项

~/.config/conf/xpra_client
OPTS=--encoding=jpeg --quality=90

服务名称的格式为 xpra-client@ssh:username@hostname:<display number>.service

示例:xpra-client@ssh:myuser@example.com:7.service

启用 该服务作为 用户单元

故障排除

错误:只有控制台用户才能运行 X 服务器

如果执行失败并在日志文件中出现此错误消息,你需要进行以下更改

创建以下内容

/etc/X11/Xwrapper.config
allowed_users=anybody

错误:无法连接到显示器 :100

有时会产生此错误

/run/user/1000/xpra/:100.log
(EE) Fatal server error:
(EE) parse_vt_settings: Cannot open /dev/tty0 (Permission denied)
(EE) Server terminated with error (1). Closing log file.

要解决此问题,需要使用 --xvfb=/usr/bin/Xorg 运行。示例

$ xpra start ssh://$USER@$SERVER/100 --start-child=/usr/bin/terminator --xvfb=/usr/bin/Xorg

参见