用 systemctl --user 管理自己的服务(免 root)

systemd 为每个用户运行独立的服务管理器,放在 ~/.config/systemd/user/ 的单元可以在登录时自动启动,完全不需要 sudo。

Linux服务用户systemd中风险
运行 `systemctl --user enable --now myapp.service` 启用并启动用户级服务。

01命令

命令
$ systemctl --user enable --now myapp.service

02作用说明

用户单元放在 ~/.config/systemd/user/(系统级每用户配置在 /etc/systemd/user/),新增文件后运行 `systemctl --user daemon-reload`。默认情况下所有会话退出后用户服务会停止,执行 `sudo loginctl enable-linger $USER` 可以让它像守护进程一样常驻后台。典型用途:syncthing、podman 容器、通知守护进程。

03风险

风险中风险
使用条件systemd-based distribution; XDG_RUNTIME_DIR set (default on desktop Linux)

04撤销

撤销
$ systemctl --user disable --now myapp.service

05来源

  • systemctl(1) - Linux manual page
    Ubuntu Manpages · technical · 2026-09-17
    依据: The --user option talks to the service manager of the calling user rather than the system manager.
  • systemd/User - ArchWiki
    ArchWiki · community · 2026-09-17
    依据: 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 · 分类: 系统与设置

06相关方案