Debian. Systemd. Подвисает терминал при перезагрузке удаленной машины по SSH

Ситуация: вы, подключившись по SSH к удаленному серверу, производите его перезагрузку. Вас не выбрасывает из подключения, а лишь выбрасывает по tcp таймауту. Очень не приятно ждать столько времени, и даже ctrl+c не работает. Лечится очень просто, используем Systemd-logind  :

apt-get install libpam-systemd dbus

Systemd-logind отслеживает активные пользовательские сессии и назначает все процессы, порожденные в них, так называемым «слайсам». Таким образом, когда система выключается, systemd может просто выполнить SIGTERM внутри пользовательских "слайсов" (включая разветвленный SSH-процесс, который передает определенный сеанс), а затем продолжать закрывать службы и сеть.


Система межпроцессного взаимодействия, которая позволяет приложениям в операционной системе сообщаться друг с другом.

О systemd-logind:

systemd-logind is a system service that manages user logins. It is responsible for:

  • Keeping track of users and sessions, their processes and their idle state. This is implemented by allocating a systemd slice unit for each user below user.slice, and a scope unit below it for each concurrent session of a user. Also, a per-user service manager is started as system service instance of user@.service for each user logged in.
  • Generating and managing session IDs. If auditing is available and an audit session ID is set for a session already, the session ID is initialized from it. Otherwise, an independent session counter is used.
  • Providing PolicyKit-based access for users to operations such as system shutdown or sleep
  • Implementing a shutdown/sleep inhibition logic for applications
  • Handling of power/sleep hardware keys
  • Multi-seat management
  • Session switch management
  • Device access management for users
  • Automatic spawning of text logins (gettys) on virtual console activation and user runtime directory management

User sessions are registered with logind via the pam_systemd(8) PAM module.


Оригинальный текст:

When you shutdown or reboot your system, systemd tries to stop all services as fast as it can. That involves bringing down the network and terminating all processes that are still alive -- usually in that order. So when systemd kills the forked SSH processes that are handling your SSH sessions, the network connection is already disabled and they have no way of closing the client connection gracefully.

Your first thought might be to just kill all SSH processes as the first step during shutdown, and there are quite a few systemd service files out there that do just that.

But there is of course a neater solution (how it's "supposed" to be done): systemd-logind.
systemd-logind keeps track of active user sessions (local and SSH ones) and assigns all processes spawned within them to so-called "slices". That way, when the system is shut down, systemd can just SIGTERM everything inside the user slices (which includes the forked SSH process that's handing a particular session) and then continue shutting down services and the network.

systemd-logind requires a PAM module to get notified of new user sessions and you'll need dbus to use loginctl to check its status, so install both of those:

apt-get install libpam-systemd dbus