ssh连接缓慢的问题分析

之前遇到ssh连接缓慢的问题 一般会检查Server端 /etc/ssh/sshd_config配置文件的两个地方

1.设置UseDNS no

因为我们ssh连接服务器的话 如果UseDNS选项是打开的话 Server端收到Client端的连接请求之后 Server端会根据客户端的IP地址 进行DNS解析 反向查询出客户端的主机名 然后根据查询出的Client端主机名 进行DNS正向A记录查询 验证与原始的Client IP地址是否一致 这样的话 可以避免客户端欺骗。

如果我们的环境中 没有配置DNS服务器的话 或者没有对Client端的IP地址 主机名进行解析配置的话 则会影响到ssh连接服务器的速度。

2.设置GSSAPIAuthentication no

这次又遇到ssh连接慢的问题 需要等待大约20几秒 才会提示输入密码。首先检查了/etc/ssh/sshd_config配置文件中这两个选项的参数,配置已经是

UseDNS no

GSSAPIAuthentication no

但是通过tcpdump抓包发现  Server侧还是会对Client的IP进行解析。

所以临时解决办法是 在Server侧的 /etc/hosts文件中 添加了Client ip 主机名一行。问题暂时得到了解决。

后来查询了文档 发现 可能是这个原因。

As has been noted the delay is due to DNS lookups which are timing out. The reason for the lookups is due to your use of tcp_wrappers through configuration of the /etc/hosts.allow and /etc/hosts.deny files. These TCP Wrappers files require DNS to function (as is mentioned in the documentation). To resolve your issue you either need to fix the DNS lookups, or remove the use of the TCP wrappers and transition to using iptables to restrict access.

之前这台Server服务器做过 安全加固。配置了/etc/hosts.allow和/etc/hosts.deny文件。

根据上面的英文文档描述 可知。因为配置了/etc/hosts.allow和/etc/hosts.deny文件 在/etc/ssh/sshd_config下配置的UseDNS no不生效。Server侧依旧会对Client侧的IP地址进行解析。

解决办法:

1.添加DNS记录,使主机名正常解析,就不会存在DNS解析超时。

2.移除/etc/hosts.allow和/etc/hosts.deny文件,采用Iptables来对网段进行限制

3.把Client侧IP地址 主机名 添加到Server端的/etc/hosts文件中

这里 我们采用了第三种方法来解决。

原文地址:https://www.cnblogs.com/atwo/p/9430566.html