
SSH の ServerAliveCountMax は実際に何をするのでしょうか?
SSH経由でサーバーに接続する際に、短時間の非アクティブ後に接続が切れるのではなく、接続が長時間開いたままになるようにしたい。これは例です。
Host *
ServerAliveInterval 60
ServerAliveCountMax 2
私は聞いた1つの情報源上記の設定では、サーバーが応答を受信する限り、常に 60 秒ごとにサーバーに応答が送信されます。ただし、何らかの理由で応答がサーバーに届かない場合は、別のメッセージを送信しようとします。そのメッセージも失敗した場合は、接続が閉じられます。(これは間違っていると思います)
の2番そして三番目しかし、ソースは別のことを言っています。非アクティブな期間がある場合、60 秒ごとにサーバーにメッセージが送信されると主張していますが、2 つのリクエストのみが送信され、その後接続が閉じられます。
では、ServerAliveCountMax は具体的に何をするのでしょうか?
答え1
「これは間違っている」というあなたの感覚は正しいです。マニュアルページを参照:
ServerAliveCountMax
Sets the number of server alive messages (see below) which may be
sent without ssh(1) receiving any messages back from the server.
If this threshold is reached while server alive messages are
being sent, ssh will disconnect from the server, terminating the
session. It is important to note that the use of server alive
messages is very different from TCPKeepAlive (below). The server
alive messages are sent through the encrypted channel and there‐
fore will not be spoofable. The TCP keepalive option enabled by
TCPKeepAlive is spoofable. The server alive mechanism is valu‐
able when the client or server depend on knowing when a connec‐
tion has become inactive.
The default value is 3. If, for example, ServerAliveInterval
(see below) is set to 15 and ServerAliveCountMax is left at the
default, if the server becomes unresponsive, ssh will disconnect
after approximately 45 seconds. This option applies to protocol
version 2 only.
ServerAliveInterval
Sets a timeout interval in seconds after which if no data has
been received from the server, ssh(1) will send a message through
the encrypted channel to request a response from the server. The
default is 0, indicating that these messages will not be sent to
the server. This option applies to protocol version 2 only.
答え2
サーバー アライブ メッセージは、SSH サーバーが一定時間トラフィックがないと接続を閉じるように構成されている場合に役立ちます (たとえば、SSH アクセスを提供する共有 Web ホスティング プロバイダーは、ほぼ常にこれを実行します)。これらの 2 つのオプションを設定すると、ServerAliveInterval
最大回数、1 秒ごとにパケットが送信され、ServerAliveCountMax
セッションが維持されます。
どちらかのオプションを に設定すると不確実になるというコメントに回答するために0
、実装のソース コードを読みましたがopenssh
、次のようなことがわかりました...
ServerAliveInterval
に設定する0
とパケットは送信されませんが、TCP タイムアウトにより接続が切断されず、サーバーが非アクティブなクライアントを切断するように構成されていないと仮定すると、セッションは無期限に維持されます。ServerAliveCountMax
を に設定すると、を に0
設定するのと同じ効果があります。ServerAliveInterval
0
どちらかの値を負の値またはそれより大きい値
INT_MAX
(例:2,147,483,647)に設定すると、「整数値...」エラー。(つまり 2,147,484) から(つまり 2,147,483,647) の
ServerAliveCountMax
間の 設定も、いずれかの値を に設定するのと同じです。INT_MAX/1000+1
INT_MAX
0
つまり、本質的には、(パケットを送信している間に) 取得できる最大のタイムアウトはINT_MAX/1000
(つまり 2,147,483) です。タイムアウトが で1
、セッションにトラフィックがまったくない場合は、約 25 日間になります。
明らかに、SSH の他の実装では結果が異なる場合があります。