Linux / 全栈之路 / 运维 · 2026/03/29

Linux 通过用户启动服务的注意事项

1. 安装目录

$HOME/.config/systemd/user/

2. 相关命令

允许服务自动运行

systemctl –user enable service.name

禁止服务自动运行

systemctl –user disable service.name

启动服务

systemctl –user start service.name

停止服务

systemctl –user stop service.name

重启服务

systemctl –user restart service.name

查看日志

journalctl –user -u service.name -n 100

3. 让服务脱离用户登录状态运行

用户登出后服务不会自动停止, 解决方案 启用 lingering

loginctl enable-linger $USER

验证 loginctl 生效

ls /var/lib/systemd/linger

如果可以看到你的用户名就是生效了