Skip to main content

A systemd User Unit Gotcha

·1 min

When you login to a Linux system that uses systemd, a systemd service manager is automatically started for you that allows you to use systemctl --user to stop, start, enable, and disable your own user units. This is usually done by pam_systemd as part of the session configuration.
But what happens if you don't login through a channel that triggers the PAM module? For example, you login as a different user and use sudo -u USERNAME -i to switch users. In that case, when you try to run systemctl --user commands, you get an error:

Failed to connect to bus: No medium found

To resolve this, the root user can configure a long-running systemd service manager for the user (this will also enable the user to keep services running when they are not logged in):

# loginctl enable-linger USERNAME

Then, after running sudo to switch users, run:

$ export XDG_RUNTIME_DIR=/run/user/${UID}

Now systemctl --user commands should work as expected.