ssh超時斷開的解決方法


當用SSH Secure Shell連接Linux時,如果幾分鍾沒有任何操作,連接就會斷開,必須重新登陸才行,每次都重復相同的操作,很是煩人,本文總結了兩種解決的方法。

方法1:更改ssh服務器的配置文件/etc/ssh/sshd_config

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 messages is very different from TCPKeepAlive (below).
The client alive messages are sent through the encrypted channel and therefore will not be spoofable.
The TCP keepalive option enabled by TCPKeepAlive is spoofable.
The client alive mechanism is valuable when the client or server depend on knowing when a connection has become inactive.The default value is 3.
If ClientAliveInterval (see below) is set to 15, and ClientAliveCountMax is left at the default,
unresponsive 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 version 2 only.

vim /etc/ssh/sshd_config

找到ClientAliveInterval 參數,如果沒有就自己加一行。

ClientAliveInterval 參數的數值是秒,比如你設置為540,就是9分鍾.

ClientAliveInterval 540

對於ClientAliveCountMax

指如果發現客戶端沒有相應,則判斷一次超時,這個參數設置允許超時的次數,比如10。

ClientAliveInterval 540

ClientAliveCountMax 10;

則代表允許超時 5400秒 = 90分鍾。

方法2:配置客戶端

1. linux下的ssh命令

vim /etc/ssh/ssh_config

然后找到里面的ServerAliveInterval 參數,如果沒有你同樣自己加一個就好了。參數意義相同,都是秒數,比如9分鍾:

ServerAliveInterval 540

2. SecureCRT

設置反空閑,如下圖所示

securecrt_to

 

3 Putty

 

啟用putty keepalive

 

putty -> Connection -> Seconds between keepalives ( 0 to turn off ),默認為0,改為60。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM