11

I have a systemd [email protected] that every user on the server can start with systemd --user start [email protected].

As root I would like to stop that service for all users when we do maintenance.

I haven't found a way in systemd man, not even via Conflict. Is there a way to stop user services as root?

0

2 Answers 2

11
+50

As root, systemd supports actions on user services using a combination of the --user and --machine flags ...

e.g.

systemctl --user --machine=<user>@.host <command> <service>

or, it can be shortened to (on localhost)

systemctl --user --machine=<user>@ <command> <service>

i.e.

systemctl --user --machine=jtingiris@ stop a-user.service

That is documented in man 1 systemctl ...

-M, --machine=

Execute operation on a local container. Specify a container name to connect to, optionally prefixed by a user name to connect as and a separating "@" character. If the special string ".host" is used in place of the container name, a connection to the local system is made (which is useful to connect to a specific user's user bus: "--user [email protected]"). If the "@" syntax is not used, the connection is made as root user. If the "@" syntax is used either the left hand side or the right hand side may be omitted (but not both) in which case the local user name and ".host" are implied.

1
  • 2
    I believe, based on this comment you have to be on systemd >= 248. For older versions you can try runuser from answer by @Dave
    – Kepi
    Aug 18, 2022 at 15:15
1

@Joseph Tingiris post did not work for me. I kept getting "Cannot access user instance remotely."

Workaround was to use runuser as root:

runuser -l userNameHere -c 'systemctl --user stop user-service.service'

EDIT: I forgot to mention that this will also work with the following:

su - userNameHere -c 'systemctl --user stop user-service.service'

But all this only works if the user is ACTUALLY logged in (via direct terminal, GUI, or SSH.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .