I have a service that I want to start with system startup. I have built a ap@.service definition for it as a template, because there could be many instances.
Defined in the root systemd, this works well, and starts and stops the service with the system. The service instance is installed with systemctl enable ap@inst1
as would be expected. Root is also able to start and stop the service without problems. The service runs in its own account (myuser), not root, controlled by User=myuser in the ap@.service template.
But I want user 'myuser' to be able to start and stop their own service, without compromising system security.
I switched to using a user systemd, and enabled lingering with loginctl enable-linger myuser
. I then enable the service defined in the ~myuser/.config/systemd/user directory. The service now starts and stops cleanly with the system, as designed. If I log in to a terminal as 'myuser', systemctl --user start ap@inst1
, and systemctl --user stop ap@inst1
both work perfectly.
However, if I log in as a different user (user2) and perform sudo su - myuser
in a terminal, then systemctl --user
commands now fail with error message "Failed to get D-Bus connection: no such file or directory".
How do I enable systemctl --user
to work after a sudo su - myuser
command to switch the user?