Can one user, or maybe root, control another user's user level systemd services?

I've tried sudo -u <some user> systemctl --user restart <some service>, but it complains about dbus: Failed to get D-Bus connection: Connection refused.


I had the same problem when I remotely logged into my gentoo box via ssh. In my case this was because the XDG_RUNTIME_DIR and DBUS_SESSION_BUS_ADDRESS environment variables were missing. Run the following commands and try again:

export XDG_RUNTIME_DIR="/run/user/$UID"

If this helps, you can put those commands into your .bashrc. I guess there must be a more elegant solution than .bashrc but that depends on your distro.

Here is where I found that solution.


logged in as root, I managed to successfully run systemctl --user as another user using su as follows:

su -c 'XDG_RUNTIME_DIR="/run/user/$UID" DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus" systemctl --user status' username

or using sudo (note, I had to explicitely add the respective users UID (1000) to the path '/run/user/', but if you are running it from a bash script you can use $SUDO_UID instead):

sudo -u username XDG_RUNTIME_DIR="/run/user/1000" DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus" systemctl --user status
  • 1
    I have exactly that in /etc/profile.d/dbus.sh. I guess sudo needs those whitelisted, because it clears the environment by default. I'm not sure if it will just transfer the current user's runtime dir, though. – mkaito Nov 27 '15 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.