歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> SSH 連接超時(ssh timeout) 解決辦法

SSH 連接超時(ssh timeout) 解決辦法

日期:2017/2/28 14:52:12   编辑:Linux教程

原來的ssh是不會超時的, 但是自從上次給Ubuntu Server 系統升級後, 出現了ssh 超時現象:只要putty連續3分鐘左右沒有輸入, 就自動斷開, 然後必須重新登陸, 很麻煩.

在網上查了很多資料, 發現原因有多種, 環境變量TMOUT引起,ClientAliveCountMax和ClientAliveInterval設置問題或者甚至是防火牆的設置問題. 所以可以這麼嘗試:

1, echo $TMOUT
如果顯示空白,表示沒有設置, 等於使用默認值0, 一般情況下應該是不超時. 如果大於0, 可以在如/etc/profile之類文件中設置它為0.
Definition: TMOUT: If set to a value greater than zero, the value is interpreted as the number of seconds to wait for input after issuing the primary prompt. Bash terminates after waiting for that number of seconds if input does not arrive.

2. ClientAliveInterval 60
在/etc/ssh/sshd_config中增加ClientAliveInterval 60, ClientAliveInterval指定了服務器端向客戶端請求消息的時間間隔, 默認是0, 不發送.而ClientAliveInterval 60表示每分鐘發送一次, 然後客戶端響應, 這樣就保持長連接了.這裡比較怪的地方是:不是客戶端主動發起保持連接的請求(如FTerm, CTerm等),而是需要服務器先主動.

另外,至於ClientAliveCountMax, 使用默認值3即可.ClientAliveCountMax表示服務器發出請求後客戶端沒有響應的次數達到一定值, 就自動斷開. 正常情況下, 客戶端不會不響應.

ClientAliveCountMax
Sets the number of client alive messages (see below) which may be
sent without sshd(8) receiving any messages back from the client.
If this threshold is reached while client alive messages are
being sent, sshd will disconnect the client, terminating the ses-
sion. It is important to note that the use of client alive mes-
sages is very different from TCPKeepAlive (below). The client
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 client 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 ClientAliveInterval (see below) is
set to 15, and ClientAliveCountMax is left at the default, unre-
sponsive SSH clients will be disconnected after approximately 45
seconds. This option applies to protocol version 2 only.

ClientAliveInterval
Sets a timeout interval in seconds after which if no data has
been received from the client, sshd(8) will send a message
through the encrypted channel to request a response from the
client. The default is 0, indicating that these messages will
not be sent to the client. This option applies to protocol ver-
sion 2 only.

3. 啟用putty keepalive
putty -> Connection -> Seconds between keepalives ( 0 to turn off ), 默認為0, 改為60.

4. 上述3個方法應該能解決大部分問題, 如果不行, 請 man sshd_config, 然後嘗試更改其他設置吧.

Copyright © Linux教程網 All Rights Reserved