1. 程式人生 > >linux下ssh連接慢的原因調查及解決方案

linux下ssh連接慢的原因調查及解決方案

acc 設置 發現 lin opened 左右 config 連接 war


項目中的一臺阿裏雲,最近一段時間出現ssh的時候,連接非常慢,大概輸入密碼後要10-20秒左右才能連上,以下記錄調查過程及解決辦法

通過網上的一些查詢,發現大都是因為設置dns,hosts或者通過關閉UseDNS=no GSSAPIAuthentication no的方式來解決,但我的問題和這些不同,首先我是通過ip連接的,不存在dns域名解析的問題,並且sshd_config文件中的這些設置項也都是no,因此自己通過調查,發現是以下原因引起的:


1.通過[-v]參數,查看ssh連接的具體過程

deMacBook-Pro:~ yyq$ ssh -v [email protected] -p xx
OpenSSH_6.9p1, LibreSSL 2.1.8
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to x.x.x.x [x.x.x.x] port xx.
debug1: Connection established.
debug1: identity file /Users/yyq/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yyq/.ssh/id_rsa-cert type -1
debug1: identity file /Users/yyq/.ssh/id_dsa type 2
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yyq/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yyq/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yyq/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yyq/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yyq/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.9
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.1
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to x.x.x.x:xx as ‘root‘
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client [email protected] <implicit> none
debug1: kex: client->server [email protected] <implicit> none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:HictrRpAs7Yv495dDLNqHsFCNKXVACpX9FWUnNVenZU
debug1: Host ‘[x.x.x.x]:xx‘ is known and matches the ECDSA host key.
debug1: Found key in /Users/yyq/.ssh/known_hosts:38
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/yyq/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Offering DSA public key: /Users/yyq/.ssh/id_dsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /Users/yyq/.ssh/id_ecdsa
debug1: Trying private key: /Users/yyq/.ssh/id_ed25519
debug1: Next authentication method: password
[email protected]‘s password:

首先觀察在要求輸入密碼前有沒有出現耗時的操作?
我的問題並沒有,因此,繼續輸出密碼:

[email protected]‘s password: 
debug1: Authentication succeeded (password).
Authenticated to x.x.x.x ([x.x.x.x]:xx).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.

輸入密碼後,發現停在這裏了,大概10-20秒後,就可以連接上;

通過日誌可以發現,密碼的驗證沒有損耗時間,已經正確驗證通過Authentication succeeded (password)
哪麽到底是什麽原因導致的速度這麽慢?

2.打開服務器的系統日誌,查看

tail -f /var/log/auth.log
sshd[12642]: pam_systemd(sshd:session): Failed to create session: Connection timed out
dbus[617]: [system] Failed to activate service ‘org.freedesktop.login1‘: timed out
sshd[12642]: Received disconnect from x.x.x.x port 52856:11: disconnected by user
sshd[12642]: Disconnected from x.x.x.x port 52856
sshd[12642]: pam_unix(sshd:session): session closed for user root
sshd[12689]: userauth_pubkey: key type ssh-dss not in PubkeyAcceptedKeyTypes [preauth]
sshd[12689]: Accepted password for root from x.x.x.x port 52866 ssh2
sshd[12689]: pam_unix(sshd:session): session opened for user root by (uid=0)
sshd[12689]: pam_systemd(sshd:session): Failed to create session: Connection timed out
dbus[617]: [system] Failed to activate service ‘org.freedesktop.login1‘: timed out

從日誌中可以看到[system] Failed to activate service ‘org.freedesktop.login1‘: timed out

的錯誤,查了下資料,大致意思如下:
dbus的服務重啟後,systemd-logind服務沒有重啟導致,可以查看systemctl status systemd-logind的狀態,解決方法就是重啟該服務 systemctl restart systemd-logind
重啟systemd-logind服務後,發現ssh可以秒連接了

linux下ssh連接慢的原因調查及解決方案