#########
修改后的:
##
# tail -f -n 20 sshd_config
#MaxStartups 10:30:60
#Banner /etc/issue.net
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
ClientAliveInterval 30
ClientAliveCountMax 3860000
########
SSH連接自動斷開的解決方法
使用SSH連接遠程服務器時,如果長時間不操作,SSH連接上就沒有數據傳輸,此時連接會自動斷開,常見的錯誤提示是:
1 |
Write failed: Broken pipe |
這種超時斷開機制估計是出於安全考慮設計的,不過這也會對正常使用造成一定影響,需要進行一些設置來避免這一問題。
核心思路就是定時發送心跳包,這樣就可以保證連接上始終有數據傳輸,就不會觸發超時斷開了。客戶端ssh
和服務器端sshd
均支持此功能,只需要配置一下就可以了,以下方法選擇其一即可。
服務器端配置
如果在服務器端進行配置的話,所有連接到此服務器的會話都會產生效果。
修改/etc/ssh/sshd_config
文件,在其中添加一行:
1 |
ClientAliveInterval 30 |
這樣服務器端每隔30s就會向客戶端發送一個keep-alive
包,以此保持連接不會斷開。還可以指定發送keep-alive
包的最大次數:
1 |
ClientAliveCountMax 60 |
若發送了60個keep-alive
包后客戶端依然沒有響應,則斷開SSH連接,如果不指定此參數的話會一直發送下去,也就是永遠不斷開連接。
客戶端配置
如果沒有服務器端的權限,也可在客戶端進行配置,這樣這個客戶端所發起的所有會話都會產生效果。
修改/etc/ssh/ssh_config
文件,與服務器端配置類似,添加以下兩個參數即可:
1 |
ServerAliveInterval |
此時就是客戶端定時向服務器端發送keep-alive
包。
會話配置
如果不希望或不能修改配置文件,也可以在每次建立SSH連接時通過-o
參數指定當前會話配置:
1 |
ssh -o ServerAliveInterval=30 root@192.168.1.1 |
本文標題:SSH連接自動斷開的解決方法
文章作者:碼農半畝地
發布時間:2017-01-10, 16:53:56
最后更新:2019-09-17, 19:46:27
原始鏈接:https://gaomf.cn/2017/01/10/SSH_Broken_Pipe/
許可協議: "署名-非商用-相同方式共享 4.0" 轉載請保留原文鏈接及作者。