使用systemctl --user启动用户服务失败:无法连接到总线
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_ADDRESSorXDG_RUNTIME_DIRaren’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):
- Use sudo or switch to root to run:
sudo loginctl enable-linger your-regular-username - Verify it worked with:
You should seeloginctl show-user your-regular-username | grep LingerLinger=yesin 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:
Thesu - your-regular-username-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:
- As your regular user, run:
export XDG_RUNTIME_DIR=/run/user/$(id -u) export DBUS_SESSION_BUS_ADDRESS=unix:path=${XDG_RUNTIME_DIR}/bus - Test if the connection works with:
If this outputs your user’s services without errors, you’re ready to start your target service.systemctl --user list-units
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




