Score:1

Cannot use systemctl as a user after a "sudo su - media" : Failed to connect to bus: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined

de flag

I'm trying to create a service as a user but it does not work :

admin $ sudo su - media
$ systemctl --user edit fmgr-copro.service --full --force
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)
$ exit
admin $ systemctl status dbus -n 2
● dbus.service - D-Bus System Message Bus
     Loaded: loaded (/lib/systemd/system/dbus.service; static)
     Active: active (running) since Wed 2023-03-22 15:07:24 CET; 1 week 0 days ago
TriggeredBy: ● dbus.socket
       Docs: man:dbus-daemon(1)
   Main PID: 869 (dbus-daemon)
      Tasks: 1 (limit: 38289)
     Memory: 1.7M
        CPU: 4.125s
     CGroup: /system.slice/dbus.service
             └─869 @dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only

Mar 28 10:09:57 myServer dbus-daemon[869]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service' r>
Mar 28 10:09:57 myServer dbus-daemon[869]: [system] Successfully activated service 'org.freedesktop.hostname1'
admin $ sudo su - media
$ echo $XDG_RUNTIME_DIR

$ echo $DBUS_SESSION_BUS_ADDRESS

$

EDIT0 : It seems the DBUS_SESSION_BUS_ADDRESS and XDG_RUNTIME_DIR variabes are cleared after a sudo su - media.

Can you help me ?

SebMa avatar
de flag
Found a solution [here](https://unix.stackexchange.com/a/245866/135038)
Score:0
cn flag

Avoid using su. Instead use the tools that comes from systemd

  • machinectl
  • systemd-run

(The question is about interactive use but for completeness I also add noninteractive use)

Interactive use

sudo machinectl shell username@

Noninteractive use

sudo systemd-run \
              -M username@ \
              --user \
              --quiet \
              --collect \
              --pipe \
              --wait \
             mycommand

Reference

su is "not a tool for opening a completely new login session" according to the original author of systemd https://github.com/systemd/systemd/issues/7451#issuecomment-346787237

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.