Cannot use `systemctl –user` due to "Failed to get D-bus connection: permission denied"

xenoid asked:

I’m trying to set up user-level services, using this answer to a similar question. I have create the required files and rebooted.

I’m making progress because I now get “Failed to get D-bus connection: permission denied” when it was “Failed to get D-bus connection: connection refused”, but I’m stumped because I don’t know what object it is trying to access (file? socket?) and so cannot even check current permissions. Any ideas?

So far I have added:

loginctl enable-linger userservice

/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

/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

/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

Not added any links elsewhere…

To make it fail:

systemctl --user status

Using RHEL 7.4 (systemd 219) with SELinux.

My answer:


So there’s a long standing issue where the XDG_RUNTIME_DIR environment variable doesn’t get set properly, or at all, when users log in, and therefore can’t access the user D-Bus. This happens when the user logs in via some other method than the local graphical console.

You can work around this by adding to the user’s $HOME/.bashrc:

export XDG_RUNTIME_DIR=/run/user/$(id -u)

Then log out and back in.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.