SSH自動斷開連接的原因-20200323


SSH自動斷開連接的原因

 

方法一:

 

用putty/SecureCRT連續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
 
方法二:
 
  SSH的英文全稱是Secure Shell。通過使用SSH,你可以把所有傳輸的數據進行加密,這樣“中間人”這種攻擊方式就不可能實現了, 而且也能夠防止DNS和IP欺騙。還有一個額外的好處就是傳輸的數據是經過壓縮的,所以可以加快傳輸的速度。 SSH有很多功能,它既可以代替telnet,又可以為ftp、pop、甚至ppp提供一個安全的“通道”。
    所謂 “中間人”的攻擊方式,就是“中間人”冒充真正的服務器接收你的傳給服務器的數據,然后再冒充你把數據傳給真正的服務器。服務器和你之間的數據傳送被“中間人”一轉手做了手腳之后,就會出現很嚴重的問題。
中國E動網(www.edong.com)憑借優質的服務經驗和精湛的專業技術受到了很多企業應用客戶的青睞。IDC部特提出兩種解決SSH超時斷連解決方法供大家參考:
1、 配置服務器

#vi /etc/ssh/sshd_config

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

數值是秒,比如你設置為120 ,則是2分鍾
ClientAliveInterval 120

2)ClientAliveCountMax
指如果發現客戶端沒有相應,則判斷一次超時,這個參數設置允許超時的次數。如3 、5等自定義。

修改兩項參數后如下:

ClientAliveInterval 120
ClientAliveCountMax 0   ###在不允許超時次數
重新加載sshd服務。退出客戶端,再次登陸即可驗證。

2、 配置客戶端

#vi /etc/ssh/ssh_config

然后找到里面的
ServerAliveInterval

參數,如果沒有,你同樣自己加一個就好了,參數意義相同,都是秒數,比如5分鍾等。

ServerAliveInterval 300

 
分類:  【1】Linux/shell


免責聲明!

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



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