SSH sessions seem to freeze after period of inactivity

I was having an issue with my SSH sessions where, after a period of inactivity, they would freeze. They weren’t disconnecting or timing out; they were just frozen.

To solve this I made the following change. In /etc/ssh/ssh_config (works also for ~/.ssh/config for individual users), I added two lines to the “Host *” section:

ServerAliveInterval 15
ServerAliveCountMax 5

Without going into a lot of detail, you can know that those are somewhat arbitrary – you can adjust those to your needs.
My entire “Host *” section looks like this (this is a RHEL system):

Host *

GSSAPIAuthentication yes
ForwardX11Trusted yes
SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY   LC_MESSAGES
SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE   LC_MEASUREMENT
SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
SendEnv XMODIFIERS
ServerAliveInterval 15
ServerAliveCountMax 5

Also, if you want to help out with connections TO your server, a similar directive in the main body of /etc/ssh/sshd_config will work:

ClientAliveInterval 15
ClientAliveCountMax 5

Since this applies to the SSH server, don’t forget to restart the service with
service sshd restart
or
systemctl restart sshd.service

You may also like...