天天看點

CentOS/Linux 解決SSH連接配接慢

    現在連接配接inux伺服器一般都是使用SSH遠端連接配接的方式。最近新裝了一台伺服器,發現telnet時速度很快,ping時一切也正常,但SSH連接配接的時候卻很慢。經過網上資料查詢,大緻是有以下幾種原因:

1、SERVER的SSHD會去DNS查找通路的CLIENT IP的HOSTNAME,如果DNS不可用或者沒有相關記錄,就會消耗一段時間。

2、在authentication gssapi-with-mic有時候也會消耗一段時間

一、測試查找具體原因:

1、使用ssh -v host進行debug

# ssh -v 192.168.100.10
           

然後就會輸出一大堆debug,通過debug資訊就可以看到連接配接到什麼地方被耽擱了

比如會顯示如下資訊:

[html] view plaincopyprint?debug1: Next authentication method: gssapi-with-mic  
debug1: Unspecified GSS failure. Minor code may provide more information  
No credentials cache found 

debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
No credentials cache found
           

 2、檢測連接配接時間

# time ssh [email protected] exit
           

二、解決方法(建議一個個設定,因為每個人連接配接慢的原因都不一樣):

1、關閉DNS反向解析

在linux中,預設就是開啟了SSH的反向DNS解析,這個會消耗大量時間,是以需要關閉。

# vi /etc/ssh/sshd_config
UseDNS=no
           

在配置檔案中,雖然UseDNS yes是被注釋的,但預設開關就是yes

2、關閉SERVER上的GSS認證

在authentication gssapi-with-mic有很大的可能出現問題,是以關閉GSS認證可以提高ssh連接配接速度。

# vi /etc/ssh/sshd_config
GSSAPIAuthentication no
           

3、修改server上nsswitch.conf檔案

# vi /etc/nsswitch.conf
           

找到

hosts: files dns

改為

hosts:files

hosts: files dns這一行含義是對于通路的主機進行域名解析的順序,是先通路file,也就是/etc/hosts檔案,如果hosts中沒有記錄域名,則通路dns,進行域名解析,如果dns也無法通路,就會等待通路逾時後傳回,是以等待時間比較長。

注意:如果SERVER需要通過域名通路其他伺服器,則需要保留此行。

4、修改SERVER上hosts檔案

在SERVER上/etc/hosts檔案中把用戶端的IP和HOSTNAME加入

5、打開SERVER上的IgnoreRhosts參數

IgnoreRhosts參數可以忽略以前登入過主機的記錄,設定為yes後可以極大的提高連接配接速度

# vi /etc/ssh/sshd_config
IgnoreRhosts yes
           

6、注意:修改之後記得重新開機sshd服務

# service sshd restart
           

重新開機systemd-logind服務:

systemctl restart systemd-logind
           

或者關閉systemd-logind服務 ,指令:

systemctl stop systemd-logind
           

繼續閱讀