用 systemctl --user 管理自己的服务(免 root)
systemd 为每个用户运行独立的服务管理器,放在 ~/.config/systemd/user/ 的单元可以在登录时自动启动,完全不需要 sudo。
运行 `systemctl --user enable --now myapp.service` 启用并启动用户级服务。
01命令
命令
$ systemctl --user enable --now myapp.service02作用说明
用户单元放在 ~/.config/systemd/user/(系统级每用户配置在 /etc/systemd/user/),新增文件后运行 `systemctl --user daemon-reload`。默认情况下所有会话退出后用户服务会停止,执行 `sudo loginctl enable-linger $USER` 可以让它像守护进程一样常驻后台。典型用途:syncthing、podman 容器、通知守护进程。
03风险
04撤销
撤销
$ systemctl --user disable --now myapp.service05来源
- systemctl(1) - Linux manual page依据: The --user option talks to the service manager of the calling user rather than the system manager.
- systemd/User - ArchWiki依据: Documents the systemd user instance, unit placement in ~/.config/systemd/user/, and loginctl enable-linger for running services without an active session.
最后验证: 2026-09-17 · 分类: 系统与设置