Running services by unprivileged users

Hi folks,
I want to start a custom service by an unprivileged user and I don’t get it to work.
I’m logging in to a server via ssh to an user account, switch to root and use su to switch to a technical user account, lets say “techuser”.

[root@server:~]# su - techuser
[techuser@server:~]$ systemctl --user status custom.service
Failed to connect to user scope bus via local transport: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=<user>@.host --user to connect to bus of other user)

So I don’t get the custom.service under the techuser to work.
Maybe I need to start the dbus daemon or somehow connect to it.
What am I missing?

Thanks!

The trick is to enable linger for the user using the loginctl command.
Once linger is enabled that users’ services will run from boot.
And your command will work.

run0 -u user_name

Note that user services are mostly useful for real user sessions.
They usually start when you log in and stop when you log out.

If you want to avoid session scope limitations, use system services.
They can run under a dedicated user and custom working directory.

2 Likes

For this to work the texhuser must be logged in properly.

To runa service as a certain user, create the .service file in /usr/lib/systemd/system, and add a User=techuser line to the [Service] section. See for example the .service file for systemd-resolved of how this service does it.

2 Likes

Enabling lingering wasn’t enough to solve the issue, but it’s still important for running long-lasting tasks without needing to stay logged in.

Thanks! That’s awesome, exactly what I was missing.

This seems like a nice solution as well. However, the service and all its components were supposed to reside in the tech user’s directory. Still, thanks for the input, it’s much appreciated!

1 Like