rcmdnk's blog

Japanese ver.: sshでConnection reset by peerと接続が切れるのを防ぐ
SSH, The Secure Shell: The Definitive Guide

When I was working with ssh, sometime a ssh connection was broken with a message:

Read from remote host example.com: Connection reset by peer
Connection to example.com closed.

This is because that a connection is cut by a router if there is no action for a while.

To prevent such a situation, there are several ways like below.

Sponsored Links

How to keep a connection

In principle, it is cut because there is no action. Therefore, if either a server or a client sends something, the connection can be kept.

Of course it could be solved if you can change the router’s settings not to cut even in such a situation. But sometime router’s can’t be touched by you.

In such cases, you can use ssh’s function.

Solve at a server side

If you are an administrator of a server, following settings in /etc/ssh/sshd_config works:

ClientAliveInterval 60
ClientAliveCountMax 3

Add these lines in the file (or edit corresponding lines) and restart sshd:

$ sudo /etc/init.d/sshd restart

ClientAliveInterval sets an interval to send a message to the connected client to confirm the connection (in second). The default is 0, and it doesn’t send a message.

ClientAliveCountMax is a number of repeats to try to send a message when it fails with a timeout. The default value is 3.

Above settings try to send a message 3 times with an interval of 60 seconds (i.e. try for 180sec).

ClientAliveInterval must be shorter than what the setting of the router’s cut timing.

Solve at a client side

If you can edit the server, you can use a client side setting.

ssh has a function to send a message from a client side, too.

To use it, add following lines in your ~/.ssh/config:

ServerAliveInterval 60
ServerAliveCountMax 3

Here, names have ServerAlive, as they are for confirming if a server is alive or not. (and a server side settings are named as ClientAlive, to check a client.)

This is similar to the server side function.

ServerAliveInterval sets an interval to send a message (the default is 0, not to send), and ServerAliveCountMax sets a maximum number of trials (the default is 3).

With these settings, you can prevent such a disconnection for any servers.

Work at Putty

If you are working with Putty in Windows, you can use Putty’s function.

It has a function to send a harmless packet to keep a connection, called Hertbeat function.

In Connection of the setting window, you can find Sending of null packets to keep session active. There is a setting Seconds between keepalive (0 to turn off).

putty

0 doesn’t send a packet. If you set it as 60, it works similar to above ServerAliveInterval, sending a packet periodically.

TereTerm also has a similar function.

Ref:

How to disable SSH timeout

keep-alive for ssh

Sponsored Links
Sponsored Links

« Notice a copy event on your blog by email only by JavaScript Access to Mac from other machines »

}