5

This is a remote machine with SSH access and systemd.

I basically have this error when I try to run systemd user commands:

$ systemctl --user status
Failed to connect to bus: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=<user>@.host --user to connect to bus of other user)

While other answers suggest to just set or "fake" the requested variables IMHO, this is not a proper solution.

The problem is basically that I logged in as a one user (I use as a system admin) and I did switch the user via su. As such, it's very much understandable systemd does not have a clue what user I'm running here… So I understand it is confused.

Note while for systemd services maybe using the system bus is maybe the better idea altogether.

However, for experimenting with podman and especially podman generate systemd also wants a user-level session, maybe just having a lingering user (loginctl enable-linger) is a useful way.


Now the actual question: How can I switch to the other user, so that systemd really knows that this user is now logged in? I just want to "operate" as a different user (in a new shell). Just as if I would use su.

Note that I do not want to SSH into the server as another user. That requires additional SSH keys and other stuff that is just not necessary. I want to switch my user while being logged in (as a user that can run sudo, i.e. system admin).

Tries

I've sudo machinectl login, but that is:

  • inconvenient, as I need to type the username again in there (I cannot do sudo machinectl login my-user.
  • a problem, as my user is a system user just for podman and I don't have a password set – so I cannot login with a password

I've found machinectl shell, which sounds exactly what I want to do, but I cannot get it to work. I've tried machinectl shell my-user and machinectl shell my-user@localhost, but it always just tells me:

Failed to get shell PTY: No machine 'localhost' known

Well… note that it also shows me I have no "machines" (whatever these are? Is not localhost one?):

$ machinectl list
No machines.

1 Answer 1

6

Ahh, I nearly got it:

Note that for whatever reason the local "machine" (i.e. "localhost") for machinectl is called :host.

Also for whatever reason it hides it in machinectl list. You need to run machinectl list --all to actually see it.

Now, given we know that, it's actually quite easy: Just run this:

$ sudo machinectl shell [email protected]

And there you are… in your new shell of my-user. You can (somewhat) verify this if you check the environmental variables $XDG_RUNTIME_DIR and $DBUS_SESSION_BUS_ADDRESS that were there in my initial error.

Also note there is apparently another alternative syntax that does not require the knowledge of that "local" machine:

$ sudo machinectl shell --uid my-user

Actually, that's also mentioned at the end of the machinectl man page in the examples section.

1
  • Still couldn't make machinectl login works. I now use machinectl shell instead of su every time. No DBus error anymore.
    – Alexis
    Nov 27, 2021 at 13:04

You must log in to answer this question.

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