我正在尝试在 Ubuntu 18.04.2 LTS 中为另一个用户运行 gsettings。具体来说,我试图防止用户的屏幕锁定。这将作为 bash 脚本的一部分运行。我使用的命令是:
su someuser
dbus-launch gsettings set org.gnome.desktop.screensaver lock-enabled false
因为这是通过 ssh 运行的,所以我使用 dbus-launch 来启动 dbus,然后尝试简单调用 gsettings。但是,我收到错误:
dbus[22652]: Unable to set up transient service directory: XDG_RUNTIME_DIR "/run/user/1000" is owned by uid 1000, not our uid 1001
(process:22650): dconf-CRITICAL **: 11:11:27.830: unable to create directory '/run/user/1000/dconf': Permission denied. dconf will not work properly.
(process:22650): dconf-CRITICAL **: 11:11:27.830: unable to create directory '/run/user/1000/dconf': Permission denied. dconf will not work properly.
(process:22650): dconf-CRITICAL **: 11:11:27.836: unable to create directory '/run/user/1000/dconf': Permission denied. dconf will not work properly.
为了确认 UID 不匹配,我检查了 /run/user 目录:
ls -lah /run/user
输出:
total 0
drwxr-xr-x 4 root root 80 Apr 16 14:25 .
drwxr-xr-x 31 root root 900 Apr 16 14:25 ..
drwx------ 4 adminuser adminuser 100 Apr 16 14:25 1000
drwx------ 11 someuser someuser 260 Apr 16 12:26 1001
我也尝试使用 sudo:
sudo -u "someuser" dbus-launch gsettings set org.gnome.desktop.screensaver lock-enabled false
这会导致错误:
(process:22264): dconf-CRITICAL **: 14:33:41.124: unable to create directory '/home/adminuser/.cache/dconf': Permission denied. dconf will not work properly.
(process:22264): dconf-CRITICAL **: 14:33:41.124: unable to create directory '/home/adminuser/.cache/dconf': Permission denied. dconf will not work properly.
(process:22264): dconf-CRITICAL **: 14:33:41.135: unable to create directory '/home/adminuser/.cache/dconf': Permission denied. dconf will not work properly.
(process:22264): dconf-WARNING **: 14:33:41.152: failed to commit changes to dconf: GDBus.Error:org.gtk.GDBus.UnmappedGError.Quark._g_2dfile_2derror_2dquark.Code2: Cannot open dconf database: Failed to open file “/home/adminuser/.config/dconf/user”: Permission denied
这就引出了一个问题,为什么 gsettings 尝试为 adminuser 而不是 someuser 运行,以及如何通过 SSH 由 adminuser 指示为 someuser 运行?
答案1
这里的问题与为什么用户永远不应该使用普通的 sudo 来启动图形应用程序?即Ubuntu 19.10 之前,默认sudo
配置不会更改$HOME
为目标用户的配置。
-H
您可以使用( ) 选项进行更改--set-home
:
-H, --set-home Request that the security policy set the HOME environment variable to the home directory specified by the target user's password database entry. Depending on the policy, this may be the default behavior.
所以
sudo -Hu someuser dbus-launch gsettings set org.gnome.desktop.screensaver lock-enabled false
-H
在较新版本中不需要添加该选项:请参阅自 19.10 以来,sudo 如何以不同的方式处理 $HOME?