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
Edit 2018-10-25:
Added export XDG_RUNTIME_DIR=/run/user/$(id -u)
to .bashrc
. The variable is set and now I get: Failed to get D-us connection: no such file or directory
. Strangely, neither man systemctl
nor systemctl --help
mention the --user
option, while both mention --system
and specify that this is the default (so what are the other options).
Using RHEL 7.4 (with systemd 219
as reported by systemctl --version
) with SELinux.
I've noticed the tableau server uses --user systemd services - they even have a note about this in their docs: https://help.tableau.com/current/server-linux/en-us/systemd_user_service_error.htm
How they do it (example is with a userid 29575)
After you create that file:
And you'll need to set XDG_RUNTIME_DIR in env of that user via bashrc or similar:
I've tested in on a recent RHEL 7.8 and it works as expected, I can run "systemctl --user status" as my user after doing this.
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
:Then log out and back in.
You should be aware of how the PAM works.
If you login in the system using either of
then the PAM machinery will call
pam_systemd
, and this will setup all needed hooks to usesystemctl
; if you switch user usingsudo
orsu
, this will not happen.This is deliberate , see https://github.com/systemd/systemd/issues/7451#issuecomment-346787237
What's weird is that actually
/etc/pam.d/su
includes/etc/pam.d/common-session
that contains an invocation ofpam_systemd
but this fails, indeed the/var/log/auth.log
says:Currently, a good way is to use
ssh
to connect to the same machine but as another user, something likessh user@localhost
.