![在“sudo su - media”之后无法以用户身份使用 systemctl:无法连接到总线:未定义 $DBUS_SESSION_BUS_ADDRESS 和 $XDG_RUNTIME_DIR](https://linux22.com/image/1193757/%E5%9C%A8%E2%80%9Csudo%20su%20-%20media%E2%80%9D%E4%B9%8B%E5%90%8E%E6%97%A0%E6%B3%95%E4%BB%A5%E7%94%A8%E6%88%B7%E8%BA%AB%E4%BB%BD%E4%BD%BF%E7%94%A8%20systemctl%EF%BC%9A%E6%97%A0%E6%B3%95%E8%BF%9E%E6%8E%A5%E5%88%B0%E6%80%BB%E7%BA%BF%EF%BC%9A%E6%9C%AA%E5%AE%9A%E4%B9%89%20%24DBUS_SESSION_BUS_ADDRESS%20%E5%92%8C%20%24XDG_RUNTIME_DIR.png)
我正在尝试以用户身份创建服务,但是它不起作用:
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:看来DBUS_SESSION_BUS_ADDRESS
和XDG_RUNTIME_DIR
变量在之后被清除了sudo su - media
。
你能帮助我吗 ?
答案1
避免使用su
。而是使用来自的工具systemd
machinectl
systemd-run
(问题是关于交互式使用,但为了完整性,我还添加了非交互式使用)
交互式使用
sudo machinectl shell username@
非交互式使用
sudo systemd-run \
-M username@ \
--user \
--quiet \
--collect \
--pipe \
--wait \
mycommand
参考
苏是 ”不是用于打开全新登录会话的工具根据 systemd 的原作者 https://github.com/systemd/systemd/issues/7451#issuecomment-346787237