ssh terminal always freezes after ~ 2minutes of inactivity

I have a remote server on ubuntu 20 and locally I'm using ubuntu 18. Bit of an open ended question since I'm unsure what specifics to post to diagnose my problem...

I am able to ssh in to my remote server no problem. But, if the window is idle for ~2 minutes it freezes and I cannot do anything. I have to keep opening new connections and closing the old windows.

Does this sound familiar? How can I diagnose or rectify?


Solution 1:

Your situation may require use of the "keep alive" functionality, optionally configurable in the server ssh daemon. By default it is disabled. Keep a copy of your original /etc/ssh/sshd_config file on your 20.04 server, then edit it, finding these lines and changing them as shown:

Change this:

#TCPKeepAlive yes

To this:

TCPKeepAlive yes

and change this:

#ClientAliveInterval 0

To this (you mentioned ~120 seconds as your problem point, so doing 100):

ClientAliveInterval 100

Restart the sshd service or reboot to have the changes take effect.

You can test using tcpdump, watching for the keep "alive packets". However, you will have to run tcpdump locally on your 20.04 server, or you will get a flood of packets from your own SSH connection. Example (at 10 seconds per keep alive packet):

sudo tcpdump -n -tttt
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp3s0, link-type EN10MB (Ethernet), capture size 262144 bytes
2021-06-04 15:04:57.551414 IP 192.168.111.136.22 > 192.168.111.122.53176: Flags [P.], seq 1949276260:1949276320, ack 1484187791, win 501, length 60
2021-06-04 15:04:57.551947 IP 192.168.111.122.53176 > 192.168.111.136.22: Flags [P.], seq 1:37, ack 60, win 8208, length 36
2021-06-04 15:04:57.551986 IP 192.168.111.136.22 > 192.168.111.122.53176: Flags [.], ack 37, win 501, length 0
2021-06-04 15:05:07.562203 IP 192.168.111.136.22 > 192.168.111.122.53176: Flags [P.], seq 60:120, ack 37, win 501, length 60
2021-06-04 15:05:07.562681 IP 192.168.111.122.53176 > 192.168.111.136.22: Flags [P.], seq 37:73, ack 120, win 8208, length 36
2021-06-04 15:05:07.562716 IP 192.168.111.136.22 > 192.168.111.122.53176: Flags [.], ack 73, win 501, length 0
2021-06-04 15:05:17.572944 IP 192.168.111.136.22 > 192.168.111.122.53176: Flags [P.], seq 120:180, ack 73, win 501, length 60
2021-06-04 15:05:17.573550 IP 192.168.111.122.53176 > 192.168.111.136.22: Flags [P.], seq 73:109, ack 180, win 8208, length 36
2021-06-04 15:05:17.573581 IP 192.168.111.136.22 > 192.168.111.122.53176: Flags [.], ack 109, win 501, length 0
2021-06-04 15:05:27.583815 IP 192.168.111.136.22 > 192.168.111.122.53176: Flags [P.], seq 180:240, ack 109, win 501, length 60
2021-06-04 15:05:27.584345 IP 192.168.111.122.53176 > 192.168.111.136.22: Flags [P.], seq 109:145, ack 240, win 8208, length 36
2021-06-04 15:05:27.584382 IP 192.168.111.136.22 > 192.168.111.122.53176: Flags [.], ack 145, win 501, length 0
2021-06-04 15:05:37.594578 IP 192.168.111.136.22 > 192.168.111.122.53176: Flags [P.], seq 240:300, ack 145, win 501, length 60
2021-06-04 15:05:37.595213 IP 192.168.111.122.53176 > 192.168.111.136.22: Flags [P.], seq 145:181, ack 300, win 8207, length 36
2021-06-04 15:05:37.595260 IP 192.168.111.136.22 > 192.168.111.122.53176: Flags [.], ack 181, win 501, length 0
2021-06-04 15:05:47.605476 IP 192.168.111.136.22 > 192.168.111.122.53176: Flags [P.], seq 300:360, ack 181, win 501, length 60
2021-06-04 15:05:47.606003 IP 192.168.111.122.53176 > 192.168.111.136.22: Flags [P.], seq 181:217, ack 360, win 8207, length 36
2021-06-04 15:05:47.606039 IP 192.168.111.136.22 > 192.168.111.122.53176: Flags [.], ack 217, win 501, length 0

You can also test without configuring for "keep alive" using ping with varying intervals, if for no other reason than to figure out exactly the time limit. Example:

doug@s19:~/freq-scalers$ ping -i 100 192.168.111.1
PING 192.168.111.1 (192.168.111.1) 56(84) bytes of data.
64 bytes from 192.168.111.1: icmp_seq=1 ttl=64 time=0.210 ms
64 bytes from 192.168.111.1: icmp_seq=2 ttl=64 time=0.269 ms
^C
--- 192.168.111.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 103912ms
rtt min/avg/max/mdev = 0.210/0.239/0.269/0.029 ms

Vary the interval to find the point of failure.