Ubuntu – systemctl edit problem “Failed to connect to bus”

systemd

When I attempt to create a new systemd unit (on Ubuntu 16.04)

$ sudo systemctl edit --user --full --force wagoOpenhabBridge.service
Failed to connect to bus: No such file or directory

Apart from this problem my systemd is running fine.
After some internet research, I checked these things:

  • I'm not using docker, Ubuntu is running directly on Intel NUC x64 hardware
  • systemd is running with PID=1
  • XDG variables in env are

    XDGSESSIONID=1790   
    XDGDATADIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop     
    XDGRUNTIMEDIR=/run/user/1000
    

Any ideas what is going wrong? What other things can I check?

Best Answer

I just came across a similar problem, it was caused by trying to run a service as a user I was not logged in with (this user has login disable, and I was using su and sg to fake it).

Why sudo ?

You have probably added sudo because the command was not working, you can safely remove it. A user systemd service is a regular file owned by the regular user.

Fixing Failed to connect to bus: No such file or directory

I found the solution on stackexchange, the DBUS_SESSION_BUS_ADDRESS seems to be missing from your environment.

Your command can be run as this:

export XDG_RUNTIME_DIR="/run/user/$UID"
export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus"
systemctl edit --user --full --force wagoOpenhabBridge.service

Running the command before login

If you want the service to be started before the user login, don't forget to run:

sudo loginctl enable-linger USERNAME
Related Question