手头有台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. Oracle在线重定义DBMS_REDEFINITION 普通表—>分区表

    实验环境:RHEL 6.4 + Oracle 11.2.0.3实验:在线重定义 普通表 为 分区表,包括主键对应的索引都改造为分区索引. 1,构造普通表t_objects conn test1/tes ...

  2. ssh整合问题总结--在添加商品模块实现图片(文件)的上传

    今天在做毕设(基于SSH的网上商城项目)中碰到了一个文件上传的需求,就是在后台管理员的商品模块中,有一个添加商品,需要将磁盘上的图片上传到tomcat保存图片的指定目录中: 完成这个功能需要两个步,第 ...

  3. JS实现返回对象的详细信息

    使用JS有时会需要打印出对象的详细信息,下面方法可以实现: function ShowObjProperty(Obj) { var PropertyList=''; var PropertyCount ...

  4. 在ASP.NET Core中怎么使用HttpContext.Current

    一.前言 我们都知道,ASP.NET Core作为最新的框架,在MVC5和ASP.NET WebForm的基础上做了大量的重构.如果我们想使用以前版本中的HttpContext.Current的话,目 ...

  5. 分离与继承的思想实现图片上传后的预览功能:ImageUploadView

    本文要介绍的是网页中常见的图片上传后直接在页面生成小图预览的实现思路,考虑到该功能有一定的适用性,于是把相关的逻辑封装成了一个ImageUploadView组件,实际使用效果可查看下一段的git效果图 ...

  6. 使用toggle()方法进行显示隐藏

    这是一个示例: <html> <head> <script type="text/javascript" src="http://keley ...

  7. mssql 跨库获取mysql

    exec sp_addlinkedserver @server='MYSQL', --这是链接服务器的名称 @srvproduct='mysql', --这个你自己随便吧 @provider='MSD ...

  8. java条件语句练习题

    输入三个数字显示最大的: System.out.println("请输入三个数字:"); int a,b,c; Scanner d = new Scanner(System.in) ...

  9. listener监听器的相关知识

    从别人的博客上我学习了listener的相关知识现在分享给大家 1.概念: 监听器就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上 ...

  10. js promise chain

    新的标准里增加了原生的Promise. 这里只讨论链式使用的情况,思考一下其中的细节部分. 一,关于 then() 和 catch() 的复习 then() 和 catch() 的参数里可以放置 ca ...