在腾讯云 ubuntu24 24.04 root用户环境下安装OpenClaw后,执行openclaw gateway install命令时遇到以下错误:
Gateway install failed: Error: systemctl --user unavailable: Failed to connect to bus: No medium foundopenclaw gateway install/restart等命令内部使用systemctl用户服务通过启用lingering模式和设置环境变量,让root用户的systemd用户服务在没有登录会话的情况下持续运行。
loginctl enable-linger root验证lingering状态:
loginctl show-user root | grep -E 'Linger|State'
# 输出:
# State=lingering
# Linger=yesps aux | grep 'systemd --user' | grep -v grep
# 应该看到类似输出:
# root 20561 /usr/lib/systemd/systemd --usermkdir -p /root/.config/systemd/user/cat > /root/.config/systemd/user/openclaw-gateway.service << 'EOF'
[Unit]
Description=OpenClaw Gateway
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/openclaw gateway run --allow-unconfigured
Restart=on-failure
RestartSec=10
[Install]
WantedBy=default.target
EOF# 设置运行时目录环境变量
export XDG_RUNTIME_DIR=/run/user/0
# 重新加载systemd配置
systemctl --user daemon-reload
# 启用并启动服务
systemctl --user enable openclaw-gateway
systemctl --user start openclaw-gatewaysystemctl --user status openclaw-gateway预期输出:
● openclaw-gateway.service - OpenClaw Gateway
Loaded: loaded (/root/.config/systemd/user/openclaw-gateway.service; enabled; preset: enabled)
Active: active (running) since Sun 2026-03-15 02:02:59 CST; 16s ago
Main PID: 22148 (openclaw)
Tasks: 18 (limit: 2267)
Memory: 439.6M (peak: 442.5M)
CPU: 8.647s为了避免每次执行命令前都要设置环境变量,将其添加到shell配置文件:
echo 'export XDG_RUNTIME_DIR=/run/user/0' >> ~/.bashrc
source ~/.bashrc# 测试重启命令
openclaw gateway restart
# 预期输出:
# 🦞 OpenClaw 2026.3.13 (61d171a) — Automation with claws: minimal fuss, maximal pinch.
# Restarted systemd service: openclaw-gateway.servicenetstat -napl | grep 18789
# 应该看到Gateway正在监听 ws://127.0.0.1:18789openclaw gateway restart # 重启网关
openclaw gateway stop # 停止网关
openclaw gateway start # 启动网关
openclaw gateway status # 查看状态
openclaw --version # 查看版本
openclaw plugins list # 查看插件systemctl --user restart openclaw-gateway # 重启服务
systemctl --user stop openclaw-gateway # 停止服务
systemctl --user start openclaw-gateway # 启动服务
systemctl --user status openclaw-gateway # 查看状态
journalctl --user -u openclaw-gateway -f # 查看日志loginctl enable-linger root启用/run/user/0(root用户的UID为0)gateway run --allow-unconfigured直接运行Gateway/root/.config/systemd/user/openclaw-gateway.service/etc/systemd/system/openclaw-gateway.service(不推荐)错误信息:
Failed to connect to bus: No medium found解决方案:
loginctl show-user root | grep Lingerps aux | grep 'systemd --user'export XDG_RUNTIME_DIR=/run/user/0检查步骤:
# 查看服务状态
systemctl --user status openclaw-gateway
# 查看详细日志
journalctl --user -u openclaw-gateway -n 50
# 检查端口占用
lsof -i :18789解决方案:
# 重新加载PATH
export PNPM_HOME="${PNPM_HOME:-$HOME/.local/share/pnpm}"
export PATH="$PNPM_HOME:$PATH"
# 永久设置
echo 'export PNPM_HOME="$HOME/.local/share/pnpm"' >> ~/.bashrc
echo 'export PATH="$PNPM_HOME:$PATH"' >> ~/.bashrc通过正确配置lingering模式和环境变量,root用户环境下完全可以使用用户级systemd服务来管理OpenClaw Gateway。
关键要点:
loginctl enable-linger rootexport XDG_RUNTIME_DIR=/run/user/0/root/.config/systemd/user/openclaw-gateway.service配置完成后,所有OpenClaw命令都可以正常使用,无需每次手动设置环境变量。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。