[服務架設][系統][sshd] ssh_exchange_identification: Connection closed by remote host 解決

ssh_exchange_identification: Connection closed by remote host

以下討論 ssh client 連到 sshd 伺服器時遇到「ssh_exchange_identification: Connection closed by remote host」訊息的可能解決方法。

問題:

使用 ssh client 連到某台目標主機時可能會遇到以下錯誤訊息:

[aa@example ~]$ ssh -v aabbcc@1.2.3.4
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 1.2.3.4 [1.2.3.4] port 22.
debug1: Connection established.
debug1: identity file /home/aa/.ssh/identity type -1
debug1: identity file /home/aa/.ssh/identity-cert type -1
debug1: identity file /home/aa/.ssh/id_rsa type -1
debug1: identity file /home/aa/.ssh/id_rsa-cert type -1
debug1: identity file /home/aa/.ssh/id_dsa type -1
debug1: identity file /home/aa/.ssh/id_dsa-cert type -1
debug1: identity file /home/aa/.ssh/id_ecdsa type -1
debug1: identity file /home/aa/.ssh/id_ecdsa-cert type -1
ssh_exchange_identification: Connection closed by remote host
[aa@example ~]$

可能解法:

1. 可能被 hosts.allow / hosts.deny 擋了

檢查一下 /etc/hosts.deny 和 /etc/hosts.allow,用 root 權限的帳號去在 hosts.allow 中寫上「sshd: ALL」如下 :

root@example # grep sshd /etc/hosts.allow
sshd: ALL

2. 可能 sshd 所在的伺服器最近有更新 glibc 或 openssl libs

去嘗試重新跑一下 sshd,然後重試看看。

3. ~/.ssh/known_hosts

刪除 ~/.ssh/known_hosts 目標伺服器對應的那一行,然後重試看看。

4. 伺服器過於忙碌

在 sshd 伺服器端的 /etc/ssh/sshd_config 內將 MaxStartups 調大,如下:

# 舊樣式
MaxStartups 20
# 新樣式
MaxStartups 10:20:30

然後重跑 sshd。

5. 用 Snology NAS 的話,確認沒有被「自動封鎖」機制擋了

可在 Snology DSM 的 web GUI 登入後,開啟控制台應用程式,在 安全性 >> 帳號 >> 自動封鎖,找到「允許/封鎖 清單」按鈕,然後去看看有沒有被封鎖了。有的話則將想要解除封鎖的 IP 刪除即可。

6. 確認周邊的網路裝置,例如 router, switch, WiFi AP  等等的組態設定沒有去擋 ssh 連線。

除錯:

可在伺服器端用「sshd -d -p 2222」在 port 2222 跑起 debug 用的 sshd 。(不會和已經跑起的 port 22 的 sshd 衝突)

然後再用 ssh client 連連看「ssh -v -p 2222 aabbcc@1.2.3.4」,觀察看看錯誤訊息,以知道是上述的哪一種問題。