歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux服務器

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

  設置反空閒,如下圖所示

ssh超時斷開的解決方法

  3 Putty

  啟用putty keepalive

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

Copyright © Linux教程網 All Rights Reserved