手头有台Linux服务器ssh登录时超级慢,需要几十秒。其它服务器均没有这个问题。平时登录操作都默默忍了。今天终于忍不住想搞清楚到底什么原因。搜索了一下发现了很多关于ssh登录慢的资料,于是自己也学着来分析、印证一下ssh登录慢的原因。

出现ssh登录慢一般有两个原因:DNS反向解析的问题和ssh的gssapi认证

1:ssh的gssapi认证问题

GSSAPI ( Generic Security Services Application Programming Interface) 是一套类似Kerberos 5 的通用网络安全系统接口。该接口是对各种不同的客户端服务器安全机制的封装,以消除安全接口的不同,降低编程难度。但该接口在目标机器无域名解析时会有问题

默认情况下,GSSAPIAuthentication在服务器端和客户端都激活的。如果DNS服务出现问题,那么登录过程要等到DNS查询超时后才能继续,这就是为什么SSH登录提示符要等很久才出现的原因。 为什么ssh登录过程中要用到DNS解析服务呢?这个是GSSAPI认证方式需要的缘故。

所以在配置文件/etc/ssh/sshd_config(服务器)或/etc/ssh/ssh_config(客户端)将参数GSSAPIAuthentication设置为no可以解决ssh登录慢的问题。

2:DNS反向解析的问题

OpenSSH在用户登录的时候会验证IP,它根据用户的IP使用反向DNS找到主机名,再使用DNS找到IP地址,最后匹配一下登录的IP是否合法。如果客户机的IP没有域名,或者DNS服务器很慢或不通,那么登录就会很花时间。

问题分析:

首先可以在ssh命令后面加上“-v“ 参数,输出debug信息定位问题。 具体操作为ssh -v root@serverip

[root@localhost ~]# ssh -v root@192.168.xxx.xxx

OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008

debug1: Reading configuration data /etc/ssh/ssh_config

debug1: Applying options for *

debug1: Connecting to 192.168.xxx.xxx [192.168.xxx.xxx] port 22.

debug1: Connection established.

debug1: permanently_set_uid: 0/0

debug1: identity file /root/.ssh/identity type -1

debug1: identity file /root/.ssh/id_rsa type -1

debug1: identity file /root/.ssh/id_dsa type -1

debug1: loaded 3 keys

debug1: Remote protocol version 2.0, remote software version OpenSSH_4.3

debug1: match: OpenSSH_4.3 pat OpenSSH*

debug1: Enabling compatibility mode for protocol 2.0

debug1: Local version string SSH-2.0-OpenSSH_4.3

debug1: SSH2_MSG_KEXINIT sent

debug1: SSH2_MSG_KEXINIT received

debug1: kex: server->client aes128-ctr hmac-md5 none

debug1: kex: client->server aes128-ctr hmac-md5 none

debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent

debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP

debug1: SSH2_MSG_KEX_DH_GEX_INIT sent

debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY

The authenticity of host '192.168.xxx.xxx (192.168.xxx.xxx)' can't be established.

RSA key fingerprint is 04:08:57:22:7e:8d:dc:d3:8e:91:20:d0:ba:d9:ed:78.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.xxx.xxx' (RSA) to the list of known hosts.

debug1: ssh_rsa_verify: signature correct

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,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

No credentials cache found

 

debug1: Next authentication method: publickey

debug1: Trying private key: /root/.ssh/identity

debug1: Trying private key: /root/.ssh/id_rsa

debug1: Trying private key: /root/.ssh/id_dsa

debug1: Next authentication method: password

root@192.168.xxx.xxx's password: 

debug1: Authentication succeeded (password).

debug1: channel 0: new [client-session]

debug1: Entering interactive session.

debug1: Sending environment.

debug1: Sending env LANG = en_US.UTF-8

Last login: Sun Sep  6 08:30:47 2015 from 192.168.7.222

[root@ceglnx01 ~]# 

 

 

从上面输出信息看到有关于Unspecified GSS failure,于是我将/etc/ssh/sshd_config(服务器)或/etc/ssh/ssh_config(客户端)将参数GSSAPIAuthentication设置为no,重启了sshd服务,测试发现ssh登录还是很慢。

[root@localhost ~]# service sshd status

 

openssh-daemon (pid 3594) is running...

 

[root@localhost ~]# service sshd restart

 

Stopping sshd: [ OK ]

 

Starting sshd: [ OK ]

那么原因应该是DNS反向解析的问题,关于DNS反向解析的问题有几个解决方法:

1:在server上/etc/hosts文件中把常用的ip和hostname加入,然后在/etc/nsswitch.conf看看程序是否先查询hosts文件

2:在server上/etc/ssh/sshd_config文件中修改或加入UseDNS=no。然后重启sshd服务

我在/etc/ssh/sshd_config上将UseDNS设置为no,重启sshd服务后,然后测试ssh连接速度。果然飞快连接上。看来主要还是DNS反向解析的问题。

参考资料:

http://www.linuxidc.com/Linux/2012-12/77144.htm

http://blog.chinaunix.net/uid-16728139-id-3435980.html

Linux SSH登录慢案例分析的更多相关文章

  1. Linux ssh登录和软件安装详解

    阿哲Style   Linux第一天 ssh登录和软件安装详解 Linux学习第一天 操作环境: Ubuntu 16.04 Win10系统,使用putty_V0.63 本身学习Linux就是想在服务器 ...

  2. Linux SSH登录服务器报ECDSA host key "ip地址" for has changed and you have requested strict checking.错误

    Linux SSH命令用了那么久,第一次遇到这样的错误:ECDSA host key "ip地址" for  has changed and you have requested ...

  3. Linux ssh登录命令

    常用格式:ssh [-l login_name] [-p port] [user@]hostname举例不指定用户:ssh 192.168.0.11指定用户:ssh -l root 192.168.0 ...

  4. 解决Linux SSH登录慢

    出现ssh登录慢一般有两个原因:DNS反向解析的问题和ssh的gssapi认证 :ssh的gssapi认证问题 GSSAPI ( Generic Security Services Applicati ...

  5. linux SSH 登录

    1:  远程连接linux server:       ssh -p22 root@192.168.XXX.XXX 然后输入密码. 2: 免密码的方式: A: sudo -i B: ssh-keyge ...

  6. linux ssh 登录同时执行其他指令

    目的:懒的敲一些重复的指令,比如登录后cd到某个目录. 咋办: ssh -t user@xxx.xxx.xxx.xxx "cd /directory_wanted ; bash" ...

  7. Linux服务器挂死案例分析

    问题现象: 在linux服务器上运行一个指定的脚本时,就会出现无数个相同进程的,而且不停的产生,杀也杀不掉,最后系统就陷入死循环,无法登陆,只能人工去按机器的电源键才可以.这够崩溃的吧? 问题分析过程 ...

  8. linux ssh登录的小知识

    查看服务器的各个端口: # netstat -tulnp 或者 #netstat -tnip 筛选在后面添加 |grep *** 准许root登录 #vi /etc/ssh/sshd_config 找 ...

  9. Linux SSH登录服务器报ECDSA host key "ip地址" for has changed and you have requested strict checking错误

    错误:ECDSA host key "ip地址" for  has changed and you have requested strict checking. 解决方案:在终端 ...

随机推荐

  1. OO中,先有对象还是先有类?

    就是问,在面向对象思想里,先有对象还是先有类,乍一看和先有鸡蛋还是先有鸡是一类问题,其实不然!这个问题,在lz考研复试的时候被面试官问过,一模一样,如今又在一个笔试题里看到了类似的题目,眨一下,有人会 ...

  2. struts2学习笔记--线程安全问题小结

    在说struts2的线程安全之前,先说一下,什么是线程安全?这是一个网友讲的, 如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会同时运行这段代码.如果每次运行结果和单线程运行的结果是一样 ...

  3. STL模板中的map的使用与例题

    最近的计分赛,记得自己的都只是过了两题.遇到了两次map,自己在寒假看了一点的map,只知道在字符串匹配的时候可以用的到.但是自己对map的使用还是不够熟练使用,这回在第一次和第二次的计分赛中都遇到可 ...

  4. SDN/NFV若干问题

    1.首先谈一谈网络技术和组网技术的关系 网络可分为两层:业务网.承载网.业务网主要是组织业务系统,而承载网主要是用来传输信息流:包括传送网(点到点数据专线).数据网(端到端连接).内容分发网(点到多点 ...

  5. PHP中curl_init函数用法

    使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所抓取的网 页,然后就可以以程序的方式得到你想要的数据了.无论是你想从从一个链接上取部分数据,或是取一个XML文件并 ...

  6. MongoDB - basic

    mongoDB basic from:http://www.tutorialspoint.com/mongodb prject:https://github.com/chenxing12/l4mong ...

  7. [Asp.net 5] Localization-Asp.net运行时多语言

    本节介绍的是Microsoft.AspNet.Localization工程.该工程是运行在Asp.net 5环境中的运行时多语言设置. ASP.net 5中间件技术 在新的Asp.net 5中,可以将 ...

  8. Java程序,基本数据类型、、数据类型转换、变量和常量、常用运算符

    一.基本数据类型 整数类型:byte.short. int(常用).long(较常用)     定义某个变量          int  a=10: 浮点类型(小数):float.double(常用) ...

  9. Javascript中addEventListener和attachEvent的区别

    在利用javascript为DOM Element添加事件处理程序时,如果要想下兼容IE6,7时,就不得不考虑addEventListener与attachEvent的异同. 1.首先说下addEve ...

  10. DrawSVG - SVG 路径动画 jQuery 插件

    jQuery DrawSVG 使用了 jQuery 内置的动画引擎实现 SVG 路径动画,用到了 stroke-dasharray 和 stroke-dashoffset 属性.DrawSVG 是完全 ...