经常通过ssh远程主机,同样是 Linux 主机,其中一些创建 ssh 连接速度特别慢,连接建立之后执行操作速度却很正常,看来应该不是网络原因。解决的方法是通过ssh的-vv参数来查看调试信息的:
经常遇到情况有两种:
一、GSSAPIAuthentication认证失败问题。
用 ssh -vv 来查看并分析连接过程中的问题,发现连接过程中会停留在这里好久:
debug1:Authentications that can continue: publickey,gssapi-with-mic,password
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
No credentials cache found
debug1:Unspecified GSS failure. Minor code may provide more information
No credentials cache found
debug1:Unspecified GSS failure. Minor code may provide more information
debug1:Next authentication method: publickey
原来是因为尝试了个gssapi-with-mic认证方式失败浪费了时间,打开(本机的或者说是客户端的)/etc/ssh/ssh_config把里面的GSSAPIAuthentication yes改成no 关掉它并重启sshd服务,即可让 ssh直接尝试password认证方式。
禁用 GSSAPIAuthentication前后建立 ssh连接时间的对比:
view plaincopy to clipboardprint?
[root@knight95 ~]# time ssh root@192.168.1.96
root@192.168.1.96's password:
Last login: Thu Jan 5 05:44:30 2017 from 192.168.1.95
[root@knight96 ~]# exit
logout
Connection to 192.168.1.96 closed.
real 0m3.303s
user 0m0.015s
sys 0m0.016s
[root@knight95 ~]#
二、DNS问题
SSH 登录太慢可能是 DNS 解析的问题,默认配置下 sshd 初次接受 ssh 客户端连接的时候会自动反向解析客户端 IP 以得到 ssh 客户端的域名或主机名。
如果这个时候 DNS 的反向解析不正确,sshd 就会等到 DNS 解析超时后才提供 ssh 连接,这样就造成连接时间过长、ssh 客户端等待的情况,一般为10-30秒左右。有个简单的解决办法就是在 sshd 的配置文件(sshd_config)里取消 sshd 的反向 DNS 解析。
# GSSAPIDelegateCredentials no
# GSSAPIKeyExchange no
# GSSAPITrustDNS no
# BatchMode no
# CheckHostIP yes
综上:在连接过程中可以用ssh -vv root@DestIP 来Debug连接过程,最终找到问题。