SSH 連線建立太慢

SSH 連線建立太慢

我在連接到具有 500 MB 記憶體的本地 Centos 6.3 虛擬機器時遇到問題。

下面是連接的輸出ssh -vvv localhost

OpenSSH_6.3, OpenSSL 1.0.1e 11 Feb 2013
debug1: Reading configuration data /usr/local/etc/ssh_config
debug2: ssh_connect: needpriv 0
debug1: Connecting to localhost [127.0.0.1] port 22.
^^^^^^^^^^ Loading this statement takes more than a minute

debug1: Connection established.
...

debug1: Next authentication method: password 
root@localhost's password:
^^^^^^^^^^ This step takes a minute too

debug3: packet_send2: adding 64 (len 50 padlen 14 extra_pad 64)
debug2: we sent a password packet, wait for reply
debug1: Authentication succeeded (password).
Authenticated to localhost ([127.0.0.1]:22).
debug1: channel 0: new [client-session]
debug3: ssh_session2_open: channel_new: 0
debug2: channel 0: send open
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug2: callback start
debug2: fd 3 setting TCP_NODELAY
debug3: packet_set_tos: set IP_TOS 0x10
debug2: client_session2_setup: id 0
debug2: channel 0: request pty-req confirm 1
debug2: channel 0: request shell confirm 1
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel_input_status_confirm: type 99 id 0
debug2: PTY allocation request accepted on channel 0
debug2: channel 0: rcvd adjust 2097152
debug2: channel_input_status_confirm: type 99 id 0
debug2: shell request accepted on channel 0
Last login: Wed Oct  9 13:27:02 2013 from 10.0.0.2

請建議我如何擺脫這種延遲。

答案1

我的猜測是,問題是 DNS 請求逾時。嘗試透過 IP 位址而不是主機名稱進行連接,並關閉UseDNS您要連接的伺服器上的選項。除此之外:

  1. 假設您從虛擬機器主機連接到那裡,我希望ssh localhost連接到主機,而不是來賓虛擬機器。當然,除非您有一些有趣的網路設定。

  2. 對於第一次延遲,您可能需要執行它並檢查客戶端掛起的strace系統呼叫。ssh

  3. 對於第二次延遲,請top在虛擬機器中執行以查看連接到虛擬機器時發生了什麼情況。同時,我建議在備用連接埠上以調試模式運行 SSH 守護程序並連接到該實例 - 您將看到它在等待的位置 - 只需運行(以 root 身份):

    sshd -ddd -p 2222 -o UsePrivilegeSeparation=no
    

    這將在連接埠 2222 上啟動 SSH 守護進程,記錄大量資訊 ( -ddd) 並且在登入期間不會使用權限分離(停用權限分離將更容易查看 strace 中發生的情況,因為只會使用一個進程) 。您也可以新增-o UseDNS=no以停用上述選項。

答案2

如果是虛擬機,則可能是由於熵不足。使用此行在連線嘗試之前/期間監視可用熵:

while true; do sleep 5; cat /proc/sys/kernel/random/entropy_avail; done

如果在連接嘗試期間它已經相當低或顯著下降,則很可能是罪魁禍首。

編輯:這樣做會影響可用熵本身,因為啟動新進程會消耗熵。也可以看看https://blog.flameeyes.eu/2011/03/entropy-broken

rng-tools您可以透過安裝嘗試收集更多熵的守護程序(或)來增加可用熵clrngd,或確保您的虛擬機器分配了適當的熵設備,這取決於您的虛擬化解決方案。

相關內容