Linux – Gracefully logout all connected ssh users on reboot

linuxshutdownsshsystemd

When I reboot my Linux server (BusyBox Linux) all ongoing ssh client connections run into a stucked state until a timeout on client side occurs. There is no message sent to the ssh client to produce a "Disconnect" error message. This basically makes the ssh client appear like something is frozen.

I'am using systemd with this service config for sshd in version OpenSSH_5.8p2

[Unit]
Description=SSH Per-Connection Server
After=syslog.target

[Service]
ExecStart=/usr/sbin/sshd -i
SuccessExitStatus=0 255
StandardInput=socket

Is there a way to configure a gracefull shutdown for the sshd?

Best Answer

sshd sessions on my (rhel) server may be identified by:

~# ps -ef |grep sshd:
root     12120     1  0 10:18 ?        00:00:00 sshd: sysope [priv]
sysope   12132 12120  0 10:18 ?        00:00:00 sshd: sysope@pts/2

killing those processes will immediately close the connections. So adapt your sshd service file for systemd like this:

[Unit]
Description=SSH Per-Connection Server
After=syslog.target

[Service]
ExecStart=/usr/sbin/sshd -i
ExecStop=/usr/sbin/killall -9 sshd
SuccessExitStatus=0 255
StandardInput=socket