SSH 远程连接服务慢的解决方案

linux系统管理 struggling 1503次浏览 0个评论

一,连接慢的主要原因是DNS解析导致,DNS解析时会一层一层去找,会消耗大量时间。

解决方法:

1、在ssh服务端上更改/etc/ssh/sshd_config文件中的配置为如下内容:

<code>


←#14#root@www ~  →vim  /etc/ssh/sshd_config

UseDNS no
# GSSAPI options
GSSAPIAuthentication no

←#15#root@www ~  →service sshd restart
</code>

然后,重启sshd进程使上述配置生效,在连接一般就不慢了。

2、如果还慢的话,检查ssh服务端上/etc/hosts文件中,127.0.0.1对应的主机名是否和 uname -n的结果一样,或者把本机ip和hostname(uname -n结果)加入到/etc/hosts里。

<code>
←#15#root@www ~  →uname -n
www.feiyu.com
←#16#root@www ~  →vim /etc/hosts
127.0.0.1  www.feiyu.com localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

</code>

二,利用ssh -v的调试功能查找慢的原因

其实可以用下面的命令调试为什么慢的细节(学习这个思路很重要)。

<code>
←#17#root@www ~  →ssh  -v root@192.168.2.100
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 192.168.2.100 [192.168.2.100] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/identity type -1
debug1: identity file /root/.ssh/identity-cert type -1
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.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
debug1: Host '192.168.2.100' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:4
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,password
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: Trying private key: /root/.ssh/id_ecdsa
debug1: Next authentication method: password
root@192.168.2.100's password:    ----》这里就是提示输入密码的交互提示。
debug1: Authentication succeeded (password).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
Last login: Wed Jun  3 22:11:56 2015 from 192.168.2.101
</code>

在远程连接时如果慢就可以确定卡在哪了,实际上在linux系统优化部分就应该优化SSH服务的此处。

三,连接虚拟机慢及闪断的原因

win下ssh连接linux虚拟机是,有时候反应很慢甚至连接不上:

1,检查防火墙是否设置好

一般要是不做iptables的练习,直接将iptables关闭就行了。

2,与网速有一定的关系,ssh传输时还需要消耗一定的资源,如果网速非常差,也是导致反应慢的一个原因。

3,检测IP是否有冲突,上次在连接时一直报sshd服务未开启,检查了半天,原来我的虚拟机设置为静态IP,导致与其他ip发生冲突,但是不会提示,设置动态IP后,重启网络服务就能连接了。

4,虚拟机网络设置,不做特殊的东西时,像测试iptables及架设局域网服务器测试时,建议设置为桥接。


DevOps-田飞雨 》》转载请注明源地址
喜欢 (0)or分享 (0)
发表我的评论
取消评论
*

表情 贴图 加粗 链接 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址