自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

診斷并解決CentOS SSH連接慢的方法

系統(tǒng) Linux 系統(tǒng)運(yùn)維
每次PuTTY使用SSH登錄到遠(yuǎn)程的Linux進(jìn)行管理的時(shí)候,遠(yuǎn)程登錄的過程都非常慢——輸入完用戶名之后,非要等到30秒左右才會(huì)出來輸入密碼的提示。這難道是CentOS的問題?

每次PuTTY使用SSH登錄到遠(yuǎn)程的Linux進(jìn)行管理的時(shí)候,遠(yuǎn)程登錄的過程都非常慢——輸入完用戶名之后,非要等到30秒左右才會(huì)出來輸入密碼的提示。在實(shí)際處理問題的時(shí)候,特別需要快速響應(yīng)的時(shí)候,這種狀況著實(shí)讓人難以忍受。

但后來具體測(cè)試了一下,發(fā)現(xiàn)這又并非是每種系統(tǒng)的通病,出現(xiàn)問題的機(jī)器主要集中的CentOS上,同樣的Debian系統(tǒng),在遠(yuǎn)程連接的過程就是健步如飛,絲毫沒有卡頓猶豫的感覺。這難道是CentOS的問題?

[[141252]]

出于好奇,查看了下兩個(gè)系統(tǒng)在SSH時(shí)的差別

CentOS:

  1. ssh -v ssh_test@192.168.128.137

SSH遠(yuǎn)程登錄的時(shí)候顯示的信息如下:

  1. OpenSSH_6.0p1 Debian-4, OpenSSL 1.0.1e 11 Feb 2013
  2. ...Some sensitive information...
  3. debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
  4. debug1: match: OpenSSH_5.3 pat OpenSSH_5*
  5. debug1: Enabling compatibility mode for protocol 2.0
  6. debug1: Local version string SSH-2.0-OpenSSH_6.0p1 Debian-4
  7. debug1: SSH2_MSG_KEXINIT sent
  8. debug1: SSH2_MSG_KEXINIT received
  9. debug1: kex: server->client aes128-ctr hmac-md5 none
  10. debug1: kex: client->server aes128-ctr hmac-md5 none
  11. debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
  12. debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
  13. debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
  14. debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
  15. ...Some sensitive information...
  16. debug1: ssh_rsa_verify: signature correct
  17. debug1: SSH2_MSG_NEWKEYS sent
  18. debug1: expecting SSH2_MSG_NEWKEYS
  19. debug1: SSH2_MSG_NEWKEYS received
  20. debug1: Roaming not allowed by server
  21. debug1: SSH2_MSG_SERVICE_REQUEST sent
  22. debug1: SSH2_MSG_SERVICE_ACCEPT received
  23. debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
  24. debug1: Next authentication method: gssapi-keyex
  25. debug1: No valid Key exchange context
  26. debug1: Next authentication method: gssapi-with-mic
  27. debug1: Unspecified GSS failure. Minor code may provide more information
  28. Cannot determine realm for numeric host address
  29. debug1: Unspecified GSS failure. Minor code may provide more information
  30. Cannot determine realm for numeric host address
  31. debug1: Unspecified GSS failure. Minor code may provide more information
  32. debug1: Unspecified GSS failure. Minor code may provide more information
  33. Cannot determine realm for numeric host address
  34. debug1: Next authentication method: publickey
  35. debug1: Trying private key: /home/mitchellchu/.ssh/id_rsa
  36. debug1: Trying private key: /home/mitchellchu/.ssh/id_dsa
  37. debug1: Trying private key: /home/mitchellchu/.ssh/id_ecdsa
  38. debug1: Next authentication method: password

而Debian使用同樣的命令測(cè)試的結(jié)果為:

  1. OpenSSH_6.0p1 Debian-4, OpenSSL 1.0.1e 11 Feb 2013
  2. ...Some sensitive information...
  3. debug1: Remote protocol version 2.0, remote software version OpenSSH_6.0p1 Debian-4
  4. debug1: match: OpenSSH_6.0p1 Debian-4 pat OpenSSH*
  5. debug1: Enabling compatibility mode for protocol 2.0
  6. debug1: Local version string SSH-2.0-OpenSSH_6.0p1 Debian-4
  7. debug1: SSH2_MSG_KEXINIT sent
  8. debug1: SSH2_MSG_KEXINIT received
  9. debug1: kex: server->client aes128-ctr hmac-md5 none
  10. debug1: kex: client->server aes128-ctr hmac-md5 none
  11. debug1: sending SSH2_MSG_KEX_ECDH_INIT
  12. debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
  13. ...Some sensitive information...
  14. debug1: ssh_ecdsa_verify: signature correct
  15. debug1: SSH2_MSG_NEWKEYS sent
  16. debug1: expecting SSH2_MSG_NEWKEYS
  17. debug1: SSH2_MSG_NEWKEYS received
  18. debug1: Roaming not allowed by server
  19. debug1: SSH2_MSG_SERVICE_REQUEST sent
  20. debug1: SSH2_MSG_SERVICE_ACCEPT received
  21. debug1: Authentications that can continue: publickey,password
  22. debug1: Next authentication method: publickey
  23. debug1: Trying private key: /home/mitchellchu/.ssh/id_rsa
  24. debug1: Trying private key: /home/mitchellchu/.ssh/id_dsa
  25. debug1: Trying private key: /home/mitchellchu/.ssh/id_ecdsa
  26. debug1: Next authentication method: password

從上面可以看到,在CentOS中,系統(tǒng)使用了publickey,gssapi-keyex,gssapi-with-mic,和 password來進(jìn)行認(rèn)證(上面顏色標(biāo)記行,23行),而Debian此時(shí)則使用了Publickey和password兩種。在連接CentOS的時(shí) 候,在23行處花費(fèi)了相當(dāng)多的時(shí)間。我們?cè)谀抢镩_始往下看,就能非常清楚的看到下面的信息:

  1. #下面使用的是GSSAPI-KEYEX來進(jìn)行驗(yàn)證
  2. debug1: Next authentication method: gssapi-keyex
  3. #但是報(bào)錯(cuò):沒有可用的Key來交換信息
  4. debug1: No valid Key exchange context
  5. #系統(tǒng)接著又使用下一個(gè)驗(yàn)證方法:GSSAPI-WITH-MIC
  6. debug1: Next authentication method: gssapi-with-mic
  7. #但遺憾的是,GSSAPI-WITH-MIC方法也失敗。
  8. #原因:不能確定數(shù)字主機(jī)地址的域
  9. debug1: Unspecified GSS failure. Minor code may provide more information
  10. Cannot determine realm for numeric host address
  11. debug1: Unspecified GSS failure. Minor code may provide more information
  12. Cannot determine realm for numeric host address
  13. debug1: Unspecified GSS failure. Minor code may provide more information
  14. debug1: Unspecified GSS failure. Minor code may provide more information
  15. Cannot determine realm for numeric host address
  16. # 在嘗試幾次后,SSH認(rèn)證終于放棄了這種驗(yàn)證。進(jìn)入下一個(gè)驗(yàn)證:Publickey
  17. debug1: Next authentication method: publickey

除了這個(gè)方法還有其他方法么?這個(gè)自然是有的,CentOS其實(shí)就已經(jīng)提供給我們一個(gè)解決方案了——使用ssh遠(yuǎn)程登錄的時(shí)候禁用GSSAPI驗(yàn)證。當(dāng)然,還有一個(gè)問題不得不注意,如果你的機(jī)器上啟用了UseDNS的話,需要一并關(guān)閉,具體可參見最后的說明。

從錯(cuò)誤可以看出應(yīng)該是和主機(jī)域相關(guān)的問題——應(yīng)該是無法確認(rèn)IP對(duì)應(yīng)的域,因此會(huì)出現(xiàn)這個(gè)問題。GSSAPI主要是基于Kerberos的,因此要解決這個(gè)問題也就變得要系統(tǒng)配置有Kerberos,這對(duì)于沒有Kerberos的筒子們來說,配置個(gè)Kerberos就為了解決個(gè)登錄延時(shí)問題,似乎不是個(gè)明智的決定——特別是在生產(chǎn)環(huán)境中!最小化滿足需求才是王道。

#p#

下面先放出處理GSSAPI的方法

禁用GSSAPI認(rèn)證有兩個(gè)方式:客戶端和服務(wù)端

1. 客戶端禁用

比較簡(jiǎn)單,影響的只有單個(gè)客戶端用戶,可以用下面的方法實(shí)現(xiàn):

  1. ssh -o GSSAPIAuthentication=no your-server-username@serverIP

用上面的方法登錄遠(yuǎn)程,即可實(shí)現(xiàn)禁用GSSAPIAuthentication。

如果你嫌麻煩,直接配置你ssh客戶端的文件/etc/ssh/ssh_config來達(dá)到永久解決這個(gè)問題:

  1. vi /etc/ssh/ssh_config
  2. ### 找到ssh_config文件里面的GSSAPIAuthentication yes這行
  3. ### 修改為GSSAPIAuthentication no
  4. ### 保存ssh_config文件并退出

這個(gè)修改方法是將所有這個(gè)機(jī)器上的用戶都影響到了,如果你影響面不要那么的廣泛,只要在指定的用戶上實(shí)施禁用GSSAPIAuthentication的話,那么你可以在該用戶的目錄下,找到.ssh目錄,在其下面添加config文件,并在文件內(nèi)添加上面這句,如果沒有這個(gè)文件,你也可以直接這么做:

  1. cat >>~/.ssh/config<<EOF
  2. GSSAPIAuthentication no
  3. EOF

使用cat,直接將輸入導(dǎo)出到文件中,這時(shí)候,你在使用ssh連接遠(yuǎn)程的目標(biāo)主機(jī)時(shí),就不會(huì)再使用GSSAPI認(rèn)證了。

上面這些文件是在客戶端,不是服務(wù)端的。也就是說,要修改這個(gè)文件,你的客戶端也要是Linux才行。

如果你是在Windows下使用PuTTY這樣的客戶端工具,就不使用上面這個(gè)方法了,PuTTY下可以嘗試在連接之前進(jìn)行設(shè)置:

PuTTY Configuration -> Connection -> SSH -> Auth -> GSSAPI -> (取消勾選)Attempt GSSAPI authentication(SSH-2 only)

如果沒有關(guān)閉PuTTY的GSSAPIAuthentication,你可以在連接的窗口右鍵(或:Ctrl + 右鍵)查看日志,可以發(fā)現(xiàn)PuTTY會(huì)自動(dòng)嘗試GSSAPI連接的日志:

  1. 2014-05-18 23:46:54 Using SSPI from SECUR32.DLL
  2. 2014-05-18 23:46:54 Attempting GSSAPI authentication
  3. 2014-05-18 23:46:54 GSSAPI authentication request refused

嗯,上面基本上將客戶端禁止GSSAPIAuthentication的方法羅列了一下。

注意:上面這些方法是比較通用的。

2. 如果你已經(jīng)配置了Kerberos的情況下

那么你也可以嘗試下如下的客戶端解決這個(gè)問題的方法:

添加遠(yuǎn)程主機(jī)的主機(jī)名到你本機(jī)的host文件中(Linux是/etc/hosts,Windows是系統(tǒng)盤:\Windows\System32\drivers\etc\hosts)。Linux和Windows下都可以添加下面這行。

  1. ### 注意:下面這樣的IP-Addr要替換成你的遠(yuǎn)程機(jī)器的IP地址,HostName,自然是主機(jī)名
    IP-Addr HostName

添加完畢之后,保存退出。

如果你沒有配置Kerberos的話,僅配置這個(gè)hosts文件一樣是不能解決問題的,在使用ssh登錄的時(shí)候,你可以看到報(bào)錯(cuò)日志會(huì)類似下面這樣:

  1. debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mi
  2. debug1: Next authentication method: gssapi-keyex
  3. debug1: No valid Key exchange context
  4. debug1: Next authentication method: gssapi-with-mic
  5. debug1: Unspecified GSS failure. Minor code may provide more information
  6. Credentials cache file '/tmp/krb5cc_0' not found
  7. debug1: Unspecified GSS failure. Minor code may provide more information
  8. Credentials cache file '/tmp/krb5cc_0' not found
  9. debug1: Unspecified GSS failure. Minor code may provide more information
  10. debug1: Unspecified GSS failure. Minor code may provide more information
  11. Credentials cache file '/tmp/krb5cc_0' not found
  12. debug1: Next authentication method: publickey

這個(gè)錯(cuò)誤我在剛開始的時(shí)候也犯了的,需要注意。

3. 服務(wù)端禁用GSSAPIAuthentication

直接到/etc/ssh/sshd_config里面,將GSSAPIAuthentication yes改為no即可了,同時(shí)也請(qǐng)注意,你可能也需要將UseDNS這個(gè)也修改成UseDNS no(這個(gè)要注意,每個(gè)系統(tǒng)的默認(rèn)值不同,此處以CentOS 6為例):

  1. sudo vi /etc/ssh/sshd_config
  2. ### 普通用戶權(quán)限不夠,需要root權(quán)限
  3. ### 找到GSSAPIAuthentication yes,修改為
  4. ### GSSAPIAuthentication no
  5. ### 注意,這里你也需要將UseDNS修改為no,CentOS默認(rèn)是yes,即使這行已被注釋,你也需要加上
  6. ### UseDNS no
  7. ### 有看到人說UseDNS yes不需要修改為UseDNS no,Mitchell測(cè)試下來是需要的。
  8. ### 保存文件,退出

當(dāng)禁用之后,我們需要重啟SSH服務(wù)來保證新的配置文件被正確應(yīng)用:

  1. service sshd restart

這個(gè)時(shí)候,再次使用SSH登錄這個(gè)主機(jī)時(shí),是不是感覺飛快了?

呼~ 終于完成了這篇長(zhǎng)文,要一邊搗騰一邊弄出這些個(gè)文字,還是真是有點(diǎn)困難。不過,這樣也就將問題搗騰的差不多了,希望看文章的你能夠看的明白,歡迎討論。  

說明:

1. GSSAPI:Generic Security Services Application Program Interface,GSSAPI本身是一套API,由IETF標(biāo)準(zhǔn)化。其最主要也是著名的實(shí)現(xiàn)是基于Kerberos的。一般說到GSSAPI都暗指Kerberos實(shí)現(xiàn)。

2. UseDNS:是OpenSSH服務(wù)器上的一個(gè)DNS查找選項(xiàng),而且默認(rèn)還是打開的,在打開的狀態(tài)下,每當(dāng)客戶端嘗試連接OpenSSH服務(wù)器的時(shí)候,服務(wù)端就自動(dòng)根據(jù)用戶客戶端的IP進(jìn)行DNS PTR反向查詢(IP反向解析才會(huì)有記錄),查詢出IP對(duì)應(yīng)的Hostname,之后在根據(jù)客戶端的Hostname進(jìn)行DNS正向A記錄查詢。通過這個(gè)查詢,驗(yàn)證IP是否和連接的客戶端IP一致。但絕大部分我們的機(jī)器是動(dòng)態(tài)獲取IP的,也就是說,這個(gè)選項(xiàng)對(duì)于這種情況根本就沒用——即使是普通靜態(tài)IP服務(wù)器,只要沒有做IP反向解析,也難以適用。如果你符合這些情況,建議關(guān)閉UseDNS以提高SSH遠(yuǎn)程登錄時(shí)候的認(rèn)證速度。

責(zé)任編輯:火鳳凰 來源: Mitchell Chu博客
相關(guān)推薦

2010-02-04 11:38:27

ibmdwLotus

2010-05-21 18:01:13

2009-12-17 10:09:02

ssh超時(shí)斷開

2014-06-19 10:19:00

Winlogon組件GPO

2025-05-07 08:05:00

SSH網(wǎng)絡(luò)

2011-02-21 15:45:26

Sendmail啟動(dòng)慢原因

2009-12-08 15:31:45

2010-08-24 11:34:37

DIV+CSS

2021-07-15 17:35:28

MaxCompute logview 阿里云

2025-04-15 01:00:00

SSH程序連接安全

2010-04-01 11:30:40

CentOS系統(tǒng)

2010-03-25 10:06:57

CentOS配置

2010-02-23 13:07:07

CentOS gcc

2010-11-16 16:07:32

oracle用戶連接

2010-10-19 10:25:29

SQL Server連

2010-04-01 13:19:53

CentOS系統(tǒng)

2023-05-04 18:45:11

2010-10-19 12:22:02

SQL Server遠(yuǎn)

2010-11-08 16:16:57

SQL Server遠(yuǎn)

2010-03-26 18:36:30

CentOS系統(tǒng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)