Systemd 驻留
Systemd Linger

原始链接: https://etbe.coker.com.au/2026/07/24/systemd-linger/

Systemd 的 `KillUserProcesses` 设置是一项强大但存在争议的功能。在 `/etc/systemd/logind.conf` 中启用该设置后,用户注销时系统会终止其所有进程。虽然常有人批评它会意外杀掉 `screen` 或 `tmux` 等后台任务,但现代 Debian 默认配置已允许这些进程保留。 该功能对于管理桌面用户特别有用:通过自动清理残留的服务(例如运行异常的 KDE 进程),管理员无需重启系统即可解决问题。 相反,“linger”(驻留)功能允许用户进程在登录会话之外独立运行或启动。尽管有些人误以为 `nohup` 或 `tmux` 的正常运行必须开启该功能,但其实际主要用途是让网络管理器、蓝牙或 Pipewire 等服务在系统启动时自动运行。这对于无头(headless)访问,或者在图形登录环境可能失效的系统中确保连接性非常方便。用户可以通过 `loginctl enable-linger` 命令或查看 `/var/lib/systemd/linger` 来管理此状态。归根结底,权衡这些设置既能为桌面用户保持系统整洁,又能为后台服务和远程管理提供必要的持久性。

抱歉。
相关文章

原文

Killing Processes

One of the features of systemd that is most controversial is the option to kill user processes when the user logs out. That initially killed screen/tmux/nohup processes too. In recent Debian releases the default configuration of systemd-logind (the login manager for systemd) is to allow processes to keep running, the configuration file /etc/systemd/logind.conf has an option KillUserProcesses that can be enabled to have user processes killed. If you do that then there are options to only kill processes for certain users and to exclude some users (default to excluding root). If using that option you can apparently use a systemd unit to start screen which prevents it being killed on logout.

This is a very handy feature for some particular user cases. One situation was that I was supporting some people who weren’t very good at computers on a system running KDE and some KDE processes would linger. So the option of logout and login again to deal with an issue of akonadi or some other KDE service misbehaving didn’t work. On that system I enabled the option to kill user processes which reduced the number of problems they had while not requiring rebooting.

It is widely believed that the “linger” feature is required to allow screen/tmux/nohup to work, in Debian (and probably most distributions) that is not the case. It might be that some combinations of configuration requires “linger” to allow screen/tmux to work but I am not interested in trying to discover them. Of all the people I have directly supported for Linux desktop use (which numbers in the hundreds) none of them have had the ability to use screen/tmux and also the cluelessnes that makes me want to automatically kill their processes when the logout.

Controlling Linger

You can enable and disable “linger” for your own account with the following commands if polkit is installed and in a typical configuration:

loginctl enable-linger
loginctl disable-linger

If running as root you can enable and disable it for another user with the following commands:

loginctl enable-linger $ACCOUNT
loginctl disable-linger $ACCOUNT

There doesn’t seem to be any documented way of discovering if an account has linger enabled or for listing accounts that have it, it seems that “ls /var/lib/systemd/linger” is the only option.

Linger on Debian

On a Debian system with close to default settings the processes won’t be killed on logout and the only difference “linger” makes is to start programs in the user’s context BEFORE they login. A friend was recently testing out a bunch of LLM programs on one of my servers and the account he used for that ended up with “linger” enabled, presumably one of the install scripts he ran was written on the assumption that enabling linger was necessary for nohup to work and it did so automatically without being asked.

One benefit I’ve found from this behaviour is on my laptop. I’m currently testing out new SE Linux policy on my laptop and rebooting it a lot. When I enabled linger on my account it caused the laptop to connect to wifi on boot without needing to login which is convenient. I can then ssh to it even when the X11/Wayland login configuration is broken.

I will leave it enabled after finishing these tests. Having background processes like Pipewire and Bluetooth start before I login will presumably make things slightly faster when I do login.

联系我们 contact @ memedata.com