How to switch systemd user/shell on remote server without logging in again via ssh?
10
votes
2
answers
4999
views
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=@.host --user to connect to bus of other user)
While [other answers suggest to just set or "fake" the requested variables](https://askubuntu.com/a/1121053/606016) 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
](https://unix.stackexchange.com/a/615964/146739) . 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](https://unix.stackexchange.com/a/616636/146739) 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.
Asked by rugk
(3496 rep)
May 30, 2021, 08:38 PM
Last activity: Jul 22, 2025, 02:13 PM
Last activity: Jul 22, 2025, 02:13 PM