If the network between your client and server goes down and your client then tries to send some data, Windows will make several attempts to send the data and will then give up and kill the connection. In particular, this can occur even if you didn’t type anything, if you are using SSH-2 and PuTTY attempts a key re-exchange.
To solve this issue, you can configure PuTTY to send null packets and TCP keepalives every few seconds.
In the left-hand menu pane, select Connection
In the field beside Seconds between keepalives, enter 5.
Check Enable TCP Keepalives (SO_KEEPALIVE option)
Now click Open and leave the SSH connection idle for a while to see if it stays up. If you are using a PuTTY session profile, don’t forget to Save your profile with these new settings.
What Can Cause the Connection to Drop?
Some network routers and firewalls need to keep track of all connections through them. Usually, these firewalls will assume a connection is dead if no data is transferred in either direction after a certain time interval. This can cause PuTTY sessions to be unexpectedly closed by the firewall if no traffic is seen in the session for some time.
The keepalive option (‘Seconds between keepalives’) allows you to configure PuTTY to send data through the session at regular intervals, in a way that does not disrupt the actual terminal session. If you find your firewall is cutting idle connections off, you can try entering a non-zero value in this field. The value is measured in seconds; so, for example, if your firewall cuts connections off after ten minutes then you might want to enter 300 seconds (5 minutes) in the box.
Note that keepalives are not always helpful. They help if you have a firewall which drops your connection after an idle period; but if the network between you and the server suffers from breaks in connectivity then keepalives can actually make things worse. If a session is idle, and connectivity is temporarily lost between the endpoints, but the connectivity is restored before either side tries to send anything, then there will be no problem – neither endpoint will notice that anything was wrong.
However, if one side does send something during the break, it will repeatedly try to re-send, and eventually give up and abandon the connection. Then when connectivity is restored, the other side will find that the first side doesn’t believe there is an open connection any more. Keepalives can make this sort of problem worse, because they increase the probability that PuTTY will attempt to send data during a break in connectivity. (Other types of periodic network activity can cause this behaviour; in particular, SSH-2 re-keys can have this effect.
Therefore, you might find that keepalives help connection loss, or you might find they make it worse, depending on what kind of network problems you have between you and the server.
I’m Still Getting Disconnected!
If keepalives fail to keep the connection up, this would indicate some other intermittent network error between you and your host, such as with WiFi, your ISP, router firewall settings, etc, but this is beyond the scope of this article. If you do manage to solve the problem, please let us know in the comments.