天天看点

CentOS/Linux 解决SSH连接慢

    现在连接inux服务器一般都是使用SSH远程连接的方式。最近新装了一台服务器,发现telnet时速度很快,ping时一切也正常,但SSH连接的时候却很慢。经过网上资料查询,大致是有以下几种原因:

1、SERVER的SSHD会去DNS查找访问的CLIENT IP的HOSTNAME,如果DNS不可用或者没有相关记录,就会消耗一段时间。

2、在authentication gssapi-with-mic有时候也会消耗一段时间

一、测试查找具体原因:

1、使用ssh -v host进行debug

# ssh -v 192.168.100.10
           

然后就会输出一大堆debug,通过debug信息就可以看到连接到什么地方被耽搁了

比如会显示如下信息:

[html] view plaincopyprint?debug1: Next authentication method: gssapi-with-mic  
debug1: Unspecified GSS failure. Minor code may provide more information  
No credentials cache found 

debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
No credentials cache found
           

 2、检测连接时间

# time ssh [email protected] exit
           

二、解决方法(建议一个个设置,因为每个人连接慢的原因都不一样):

1、关闭DNS反向解析

在linux中,默认就是开启了SSH的反向DNS解析,这个会消耗大量时间,因此需要关闭。

# vi /etc/ssh/sshd_config
UseDNS=no
           

在配置文件中,虽然UseDNS yes是被注释的,但默认开关就是yes

2、关闭SERVER上的GSS认证

在authentication gssapi-with-mic有很大的可能出现问题,因此关闭GSS认证可以提高ssh连接速度。

# vi /etc/ssh/sshd_config
GSSAPIAuthentication no
           

3、修改server上nsswitch.conf文件

# vi /etc/nsswitch.conf
           

找到

hosts: files dns

改为

hosts:files

hosts: files dns这一行含义是对于访问的主机进行域名解析的顺序,是先访问file,也就是/etc/hosts文件,如果hosts中没有记录域名,则访问dns,进行域名解析,如果dns也无法访问,就会等待访问超时后返回,因此等待时间比较长。

注意:如果SERVER需要通过域名访问其他服务器,则需要保留此行。

4、修改SERVER上hosts文件

在SERVER上/etc/hosts文件中把客户端的IP和HOSTNAME加入

5、打开SERVER上的IgnoreRhosts参数

IgnoreRhosts参数可以忽略以前登录过主机的记录,设置为yes后可以极大的提高连接速度

# vi /etc/ssh/sshd_config
IgnoreRhosts yes
           

6、注意:修改之后记得重启sshd服务

# service sshd restart
           

重启systemd-logind服务:

systemctl restart systemd-logind
           

或者关闭systemd-logind服务 ,命令:

systemctl stop systemd-logind
           

继续阅读