| It would be very unusual for an ISP to drop idle connections. This implies all your connections are going through a layer 4 router. More likely you have a statefull firewall in the path somewhere. home router, server ISP firewall, etc... [Edit: ] Or in this case, an ISP in Denmark that is trying to minimize ipv4 cost by using LSN (carrier grade nat) which also has many other drawbacks. A SSH session does not generate any traffic This does not have to be true. You can enable TCP keepalive in the server and client configuration. Client via ~/.ssh/config: TCPKeepAlive yes
ServerAliveInterval 60
ServerAliveCountMax 2
Server via /etc/ssh/sshd_config: TCPKeepAlive yes
ClientAliveInterval 60
ClientAliveCountMax 2
Why are the TCP keepalives only sent after 2 hours?Each OS has a default time set for keepalives. If you do not specify it in the ssh config, it will use the OS default. In Linux, you can set this in /etc/sysctl.conf: net.ipv4.tcp_keepalive_time = 60
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 2
After adding this, run sysctl -e -pYou can see the timers on your established connections with: ss -emoian | grep tim
Note: TCP timers are not the same as ssh client and sshd server tcp keepalive packets. These are two distinctly different mechanisms that can accomplish the same thing. Not all applications support TCP socket keepalive. You can wrap applications with a library called libkeepalived to add support without code changes by using LD_PRELOAD.In Windows this is set in the registry [1] On mac this is set via sysctl similar to Linux. After you have adjusted your client and server config, restarted sshd on the server, then ssh to your server using the flag -vv and you will see the keepalive packets. [1] - https://serverfault.com/questions/735515/tcp-timeout-for-est... |
I usually just do client keepalives as they are easiest to set up. Server keepalives are good if you are worried about “forgotten” clients. TCP keepalives are usually not worth it IMHO.