由于“无法获得 D-bus 连接:权限被拒绝”,无法使用 `systemctl --user`

xen*_*oid 9 dbus systemd systemctl

我正在尝试使用类似问题的答案来设置用户级服务。我已经创建了所需的文件并重新启动。

我正在取得进展,因为我现在收到“无法获得 D 总线连接:权限被拒绝”,而“无法获得 D 总线连接:连接被拒绝”,但我很困惑,因为我不知道是什么对象它正在尝试访问(文件?套接字?),因此甚至无法检查当前权限。有任何想法吗?

到目前为止,我已经添加了:

loginctl enable-linger userservice
Run Code Online (Sandbox Code Playgroud)

/usr/lib/systemd/user/dbus.service (-rw-r--r-- root root)

[Unit]
Description=D-Bus User Message Bus
Requires=dbus.socket

[Service]
ExecStart=/usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation
ExecReload=/usr/bin/dbus-send --print-reply --session --type=method_call --dest=org.freedesktop.DBus / org.freedesktop.DBus.ReloadConfig

[Install]
Also=dbus.socket
Run Code Online (Sandbox Code Playgroud)

/usr/lib/systemd/user/dbus.socket (-rw-r--r-- root root)

[Unit]
Description=D-Bus User Message Bus Socket

[Socket]
ListenStream=%t/bus
ExecStartPost=-/bin/systemctl --user set-environment DBUS_SESSION_BUS_ADDRESS=unix:path=%t/bus

[Install]
WantedBy=sockets.target
Also=dbus.service
Run Code Online (Sandbox Code Playgroud)

/home/userservice/.config/systemd/user/userservice.service

[Unit]
Description=Test user-level service

[Service]
Type=dbus
BusName=com.wtf.service
ExecStart=/home/userservice/userservice.py
Restart=on-failure

[Install]
WantedBy=default.target
Run Code Online (Sandbox Code Playgroud)

没有在别处添加任何链接...

让它失败:

systemctl --user status
Run Code Online (Sandbox Code Playgroud)

编辑 2018-10-25:

添加export XDG_RUNTIME_DIR=/run/user/$(id -u).bashrc. 变量已设置,现在我得到:Failed to get D-us connection: no such file or directory。奇怪的是,既没有man systemctl也没有systemctl --help提及该--user选项,而两者都提到--system并指定这是默认值(那么其他选项是什么)。

在 SELinux 中使用 RHEL 7.4(systemd 219根据 报告systemctl --version)。

Mic*_*ton 7

因此,存在一个长期存在的问题,即XDG_RUNTIME_DIR当用户登录时环境变量没有正确设置,或者根本没有设置,因此无法访问用户 D-Bus。当用户通过本地图形控制台以外的其他方法登录时,就会发生这种情况。

您可以通过添加到用户的$HOME/.bashrc

export XDG_RUNTIME_DIR=/run/user/$(id -u)
Run Code Online (Sandbox Code Playgroud)

然后注销并重新登录。


小智 6

我注意到 tableau 服务器使用 --user systemd 服务 - 他们甚至在他们的文档中对此进行了说明:https : //help.tableau.com/current/server-linux/en-us/systemd_user_service_error.htm

systemd 用户服务不像普通 systemd 进程管理器那样常用。Red Hat 在 RHEL 7 中禁用了 systemd 用户服务(因此所有来自 RHEL 的发行版,如 CentOS、Oracle Linux 7、Amazon Linux 2)。但是,RedHat 已向 Tableau 保证,只要重新启用该服务,就支持运行 systemd 用户服务。

他们是怎么做的(例如,用户 ID 为 29575)

# cat /etc/systemd/system/user@29575.service
[Unit]
Description=User Manager for UID %i
After=systemd-user-sessions.service
# These are present in the RHEL8 version of this file except that the unit is Requires, not Wants.
# It's listed as Wants here so that if this file is used in a RHEL7 settings, it will not fail.
# If a user upgrades from RHEL7 to RHEL8, this unit file will continue to work until it's
# deleted the next time they upgrade Tableau Server itself.
After=user-runtime-dir@%i.service
Wants=user-runtime-dir@%i.service

[Service]
LimitNOFILE=infinity
LimitNPROC=infinity
User=%i
PAMName=systemd-user
Type=notify
# PermissionsStartOnly is deprecated and will be removed in future versions of systemd
# This is required for all systemd versions prior to version 231
PermissionsStartOnly=true
ExecStartPre=/bin/loginctl enable-linger %i
ExecStart=-/lib/systemd/systemd --user
Slice=user-%i.slice
KillMode=mixed
Delegate=yes
TasksMax=infinity
Restart=always
RestartSec=15

[Install]
WantedBy=default.target
Run Code Online (Sandbox Code Playgroud)

创建该文件后:

systemctl daemon-reload
systemctl enable user@29575.service
systemctl start user@29575.service
Run Code Online (Sandbox Code Playgroud)

您需要通过 bashrc 或类似方法在该用户的 env 中设置 XDG_RUNTIME_DIR :

[ -z "${XDG_RUNTIME_DIR}" ] && export XDG_RUNTIME_DIR=/run/user/$(id -ru)
Run Code Online (Sandbox Code Playgroud)

我已经在最近的 RHEL 7.8 上进行了测试,它按预期工作,执行此操作后,我可以以我的用户身份运行“systemctl --user status”。

  • 这也适用于 Amazon Linux 实例。 (2认同)

am7*_*m70 6

您应该了解 PAM 的工作原理。

如果您使用以下任一方式登录系统

  1. 图形会话
  2. 登录终端(用户名和密码)
  3. SSH

然后 PAM 机制将调用pam_systemd,这将设置所有需要使用的钩子systemctl;如果您使用sudo或切换用户su,则不会发生这种情况。

这是故意的,请参阅 https://github.com/systemd/systemd/issues/7451#issuecomment-346787237

奇怪的是,实际上/etc/pam.d/su包含/etc/pam.d/common-session包含调用,pam_systemd但这失败了,确实是这样/var/log/auth.log说的:

pam_systemd(su:session):无法创建会话:已在会话中运行

目前,一个好的方法是使用ssh另一个用户连接到同一台计算机,例如ssh user@localhost.