如何在 Wayland 下以 root 身份运行图形应用程序

如何在 Wayland 下以 root 身份运行图形应用程序

在新的 Wayland 显示服务器下,多个需要 root 权限的应用程序无法运行。我实际上有一个答案(解决该问题的一种方法)。我欢迎任何更好的解决方案或批评。

此问题的许多其他答案建议将 root 添加到 xhost,这虽然解决了问题,但稍微破坏了 Wayland 安全模型。最好只在程序运行期间将 root 添加到 xhost,无论是 synaptic 还是 gparted 或任何一个。

古夫:

编辑 gufw.desktop 文件(Debian 10 中的 /usr/share/applications/gufw.desktop)并更改以下行:

Exec=gufw

Exec=sh -c "xhost +si:localuser:root && gufw && xhost -si:localuser:root"

突触:

受到以下帖子的启发:https://discourse.ubuntu.com/t/adding-applications-to-start-up/9288 编辑 /usr/bin/synaptic-pkexec 以注释掉 zenity 警告消息(假设是 Debian 10)并更改行:

exec "/usr/sbin/synaptic" "$@"

xhost +si:localuser:root
pkexec "/usr/sbin/synaptic" "$@"
xhost -si:localuser:root

我想其他程序大多可以像 gufw 一样进行修改。这并不能解决旧程序以 root 身份运行所有内容(包括 GUI)的根本问题,但至少它们可以像以前一样运行。

答案1

在 Wayland 中,其他用户无法启动应用程序,因为他们无法访问位于以下位置的 Wayland 套接字文件:$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY。但是,root 会忽略所有文件权限,因此当在 WAYLAND_DISPLAY 中指定完整路径时,可以完美访问套接字文件。由于 Wayland 客户端库中的实现细节,还需要将 XDG_RUNTIME_DIR 定义为某些内容(即使实际值并未真正使用)。

因此,实际上可以将 Synaptic 作为纯 Wayland 应用程序运行,例如

sudo /bin/env WAYLAND_DISPLAY="$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY"  XDG_RUNTIME_DIR=/user/run/0  /usr/sbin/synaptic

到目前为止,我仅使用 Sway 测试了该技巧。

答案2

如果 root 启用了 ssh,那么也可以使用 waypipe:https://gitlab.freedesktop.org/mstoeckl/waypipe

waypipe ssh root@localhost synaptic

答案3

这是一个 shell 脚本,可使用 sudo 和 waypipe 以 root 身份运行任何 Wayland 程序(无需 ssh)。这是受到 waypipe 手册页中示例的启发。

sudo 无法启动正确的用户会话。这对于某些应用程序来说可能会出现问题。请参阅评论了解更多详情。

#!/bin/bash

# There is still a small risk of collision when using -u but this is unlikely.
SOCKET=`mktemp -u /tmp/sudo-waypipe.XXXXXXX.sock`

waypipe --socket "$SOCKET" client &

# waypipe --server requires a proper XDG_RUNTIME_DIR directory for the root user.
#
# That should typically be /run/user/0 or /var/run/0
#
# Unfortunalely, sudo does not create a user session so this variable is not set.
# Even worse, systemd destroys the directory when the last session terminates.
#
# In practice, that means that we cannot assume that /run/user/0 or /var/run/0
# exists and, even if it does exist, it cannot be safely used because it may
# be removed by systemd at any time. 
#
# Ideally, a user session should be created for user 0 (via systemctl?) 
# but this is not that easy. The alternative is to use a custom directory in /root
#
# The disadvantage is that the application may not be able to access services
# such as dbus.
#

sudo sh -c "XDG_RUNTIME_DIR=\$HOME/xdg-run/ ; mkdir -m 700 -p \$XDG_RUNTIME_DIR ; export XDG_RUNTIME_DIR ; waypipe --socket $SOCKET server -- $(printf "%q " "$@")"
kill %1
rm -f "$SOCKET" 

相关内容