手头有台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反向解析的问题。

ssh连接超慢解决的更多相关文章

  1. 【linux】——FreeBSD 建立 SSH 连接慢的解决方法

    一般在编写 linux 程序的时候,会使用 SecureCRT 或者 xshell 等工具远程登录到 linux 服务器上.最近发现在建立 SSH 连接的时候,非常慢,但是建立连接成功之后可以正常使用 ...

  2. ssh连接速度慢解决

    这些天,公司新安装的几台服务器,发现SSH本地连接上去,速度非常慢,慢的难以忍受,大概30秒左右. 后来到网上查了下,尝试了一种办法,效果还可以,特发上来给大家共享. [root@data208 ~] ...

  3. SSH连接LINUX乱码解决方法

    1.vi /etc/sysconfig/i18n 将内容改为 LANG="zh_CN.GB18030" LANGUAGE="zh_CN.GB18030:zh_CN.GB2 ...

  4. ssh连接虚拟机失败解决办法

    首先,你需要知道本机IP跟虚拟机IP,然后让两者互相ping一下,看能否ping通 让主机ping虚拟机 :ping (虚拟机ip) 出现如下: 表示主机可以ping通虚拟机 然后让虚拟机ping主机 ...

  5. ssh连接服务器失败解决记录

    故障:db2inst1用户无法通过ssh连接数据库服务器. 但是root用户可以连接,连接后su – db2inst1用户报错: su: cannot set user id: Resource te ...

  6. 烂泥:【解决】Ubuntu下使用SSH连接centos系统很慢

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 这几天在Ubuntu下使用SSH连接centos系统,发现连接很慢.建议一个连接大约需要30s.很是坑爹,如下: 后来查询相关资料,发现这个是Ubunt ...

  7. 001. 使用ssh连接不上centos 6.5的解决方法及其解决中文乱码

    1. 使用ssh连接不上centos 6.5的解决方法: 错误显示: Connecting to 192.168.1.106:22... Could not connect to '192.168.1 ...

  8. SSH连接不上Linux的解决方法

    SSH连接不上Linux的解决方法: 连续弄了几次,今天早上终于把SSH连接虚拟机连接不通的问题解决了. 先简单说下概要: 主机装的是XP系统,虚拟机用的是red hat Linux. 我用的是nat ...

  9. 解决虚拟机ssh连接出错connection refused

    在安装操作系统之后,使用ssh连接,发现直接报错connection refused. 检查虚拟机的连接方式,在使用host only的方式的时候,必须接入网线,不然的网卡是不活动的,从而不能使用ss ...

随机推荐

  1. CH6802 車的放置

    原题链接 和棋盘覆盖(题解)差不多. 将行和列看成\(n+m\)个节点,且分属两个集合,如果某个节点没有被禁止,则行坐标对应节点向列坐标对应节点连边,然后就是求二分图最大匹配了. #include&l ...

  2. How To Configure SAMBA Server And Transfer Files Between Linux & Windows

    If you are reading this article it means you have a network at home or office with Windows and Linux ...

  3. vue 获取跳转上一页组件信息

    项目中有一需求,需要根据不同的页面路径url跳转进行不同的操作,首先需要获得上一页面的url,利用 beforeRouteEnter 这个钩子中的from参数获得之前url的信息,然后给 next 传 ...

  4. idea+tomcat 端口占用

    ntelliJ IDEA和Tomcat整合注意事项(转) 这两天一直在学习IDEA这个开发工具,今天再整合tomcat的时候遇到了问题,运行时总是报错,说是8080端口被占用,把我就搞的郁闷了,我就尝 ...

  5. 【机器学习】从分类问题区别机器学习类型 与 初步介绍无监督学习算法 PAC

    如果要对硬币进行分类,我们对硬币根据不同的尺寸重量来告诉机器它是多少面值的硬币 这种对应的机器学习即使监督学习,那么如果我们不告诉机器这是多少面额的硬币,只有尺寸和重量,这时候让机器进行分类,希望机器 ...

  6. spring学习 十 schema-based 前置后后置通知

    spring 提供了 2 种 AOP 实现方式:(1)Schema-based ,(2)AspectJ Schema-based:每个通知都需要实现接口或类,配置 spring 配置文件时在<a ...

  7. 762. Prime Number of Set Bits in Binary Representation

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  8. Lagrange 乘子法求最优解

    clc clear syms x y z r1 r2 w f=x^+y^+z^+w^; g1=*x-y+z-w-; g2=x+y-z+w-; h=f-r1*g1 -r2*g2; hx=diff(h,x ...

  9. C++ MFC棋牌类小游戏day1

    好用没用过C++做一个完整一点的东西了,今天开始希望靠我这点微薄的技术来完成这个小游戏. 我现在的水平应该算是菜鸟中的战斗鸡了,所以又很多东西在设计和技术方面肯定会有很大的缺陷,我做这个小游戏的目的单 ...

  10. vs2015转到定义没反应

    开始菜单 -->所有程序-->Visual Studio 文件夹 --> Visual Studio Tools -->VS2015开发人员命令提示输入DOS命令: CD Co ...