You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用systemctl --user启动用户服务失败:无法连接到总线

Fixing "Failed to connect to bus: No such file or directory" for systemctl --user

Hey there, I’ve helped tons of users work through this exact error when trying to run user-level systemd services, so let’s break down the most reliable fixes to get your service up and running as a regular user.

First: What’s Causing This?

This error almost always traces back to one of two core issues:

  • The systemd user instance for your regular account isn’t running (or shuts down when you log out)
  • Critical environment variables like DBUS_SESSION_BUS_ADDRESS or XDG_RUNTIME_DIR aren’t properly initialized for your user session

Step 1: Enable User Linger (The Most Critical Fix)

By default, systemd stops a user’s service instance as soon as they log out. To keep it running even when you’re not logged in (and ensure the D-Bus connection stays intact):

  1. Use sudo or switch to root to run:
    sudo loginctl enable-linger your-regular-username
    
  2. Verify it worked with:
    loginctl show-user your-regular-username | grep Linger
    
    You should see Linger=yes in the output.

Step 2: Use a Proper User Session

If you’re switching users or logging in via SSH, you need to load the full user environment:

  • Instead of su your-regular-username, use:
    su - your-regular-username
    
    The - flag ensures all your user’s shell profiles and required environment variables (including those for D-Bus) are loaded.
  • For SSH, avoid running single commands directly (like ssh user@host systemctl --user start service). Instead, log into an interactive shell first, or pass the command with a login shell:
    ssh user@host -t "bash -l -c 'systemctl --user start your-service'"
    

Step 3: Manually Set Environment Variables (If Needed)

If the above steps don’t resolve the issue, you can manually define the D-Bus variables for your session:

  1. As your regular user, run:
    export XDG_RUNTIME_DIR=/run/user/$(id -u)
    export DBUS_SESSION_BUS_ADDRESS=unix:path=${XDG_RUNTIME_DIR}/bus
    
  2. Test if the connection works with:
    systemctl --user list-units
    
    If this outputs your user’s services without errors, you’re ready to start your target service.

Step 4: Install Missing D-Bus Packages (For Minimal Systems)

On stripped-down systems (like containers or minimal OS installs), you might be missing the user-level D-Bus package:

  • For Debian/Ubuntu-based systems:
    sudo apt install dbus-user-session
    
  • For RHEL/CentOS/Fedora:
    sudo dnf install dbus
    

Reboot your system after installing to ensure the D-Bus session initializes properly.

Final Check

Once you’ve applied the fixes, test starting your service again:

systemctl --user start your-service-name

You can confirm it’s running with:

systemctl --user status your-service-name

内容的提问来源于stack exchange,提问作者Joachim

火山引擎 最新活动