linux连接慢 Linux怎么学
很多朋友对于linux连接慢和Linux怎么学不太懂,今天就由小编来为大家分享,希望可以帮助到大家,下面一起来看看吧!
如何解决SSH连接Linux服务器变慢的问题
大致是有以下几种原因:
1、SERVER的SSHD会去DNS查找访问的CLIENT IP的HOSTNAME,如果DNS不可用或者没有相关记录,就会消耗一段时间。
2、在authentication gssapi-with-mic有时候也会消耗一段时间
一、测试查找具体原因:
1、使用ssh-v host进行debug
<span style="font-size:18px;"># ssh-v 192.168.100.10</span>
然后就会输出一大堆debug,通过debug信息就可以看到连接到什么地方被耽搁了
比如会显示如下信息:
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
No credentials cache found
2、检测连接时间
<span style="font-size:18px;"># time ssh root@192.168.100.10 exit</span>
二、解决方法(建议一个个设置,因为每个人连接慢的原因都不一样):
注意:修改之后记得重启sshd服务
# service sshd restart
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上resolv.conf文件
4.1、删除/etc/resolv.conf中所有不使用的IP。
4.2、把nameserver全部删除,问题也能解决,但是服务器就无法上网了。
4.3、如果SERVER曾经配置过双网卡,则在该文件中会有一行目前不使用的IP地址,删除该行即可。
5、修改SERVER上hosts文件
在SERVER上/etc/hosts文件中把客户端的IP和HOSTNAME加入
6、打开SERVER上的IgnoreRhosts参数
IgnoreRhosts参数可以忽略以前登录过主机的记录,设置为yes后可以极大的提高连接速度
# vi/etc/ssh/sshd_config
IgnoreRhosts yes
----------------以上的均在SERVER上设置,以下的均在CLIENT上设置-------------------
7、修改客户端的hosts文件
将目标SERVER的IP和域名加上去,使得本机的DNS服务能解析目标地址。
# vi/etc/hosts
192.168.100.11 doiido.com
注:hosts文件格式为'目标SERVER_IP目标SERVER_NAME'。但是使用这个方法有一个弊端,如果需要给每台SERVER都添加一个域名解析。
8、修改客户端配置文件ssh_conf(注意,不是sshd_conf)
# vi/etc/ssh/ssh_conf
找到
GSSAPIAuthentication yes
改为
GSSAPIAuthentication no
Linux服务器端SSH远程连接速度慢的解决方法
解决方法:
1、在ssh服务端上更改/etc/ssh/sshd_config文件中的配置为如下内容:
复制代码代码如下:UseDNS no
# GSSAPI options
GSSAPIAuthentication no
然后,执行/etc/init.d/sshd restart重启sshd进程使上述配置生效,在连接一般就不慢了。
2、如果还慢的话,检查ssh服务端上/etc/hosts文件中,127.0.0.1对应的主机名是否和 uname-n的结果一样,或者把本机ip和hostname(uname-n结果)加入到/etc/hosts里。
复制代码代码如下:[root@C64~]# uname-n
C64
[root@C64~]# cat/etc/hosts
#modi by oldboy 11:12 2013/9/24
127.0.0.1 C64 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.18 C64
################
利用ssh-v的调试功能查找慢的原因
其实可以用下面的命令调试为什么慢的细节(学习这个思路很重要)。
复制代码代码如下:[root@C64~]# ssh-v root@10.0.0.19
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
debug1: Reading configuration data/etc/ssh/ssh_config
debug1: Applying options for*
debug1: Connecting to 10.0.0.19 [10.0.0.19] 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: Remote protocol version 2.0, remote software version OpenSSH_4.3
debug1: match: OpenSSH_4.3 pat OpenSSH_4*
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
The authenticity of host'10.0.0.19(10.0.0.19)' can't be established.
RSA key fingerprint is ca:18:42:76:0e:5a:1c:7d:ef:fc:24:75:80:11:ad:f9.
Are you sure you want to continue connecting(yes/no)? yes
=======>老男孩老师评:这里就是提示保存密钥的交互提示。
Warning: Permanently added'10.0.0.19'(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,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: Next authentication method: password
root@10.0.0.19'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: Tue Sep 24 10:30:02 2013 from 10.0.0.18
在远程连接时如果慢就可以确定卡在哪了。
复制代码代码如下:[root@C64_A~]# ssh-v oldboy@10.0.0.17
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
debug1: Reading configuration data/etc/ssh/ssh_config
debug1: Applying options for*
debug1: Connecting to 10.0.0.17 [10.0.0.17] 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 2
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'10.0.0.17' is known and matches the RSA host key.
debug1: Found key in/root/.ssh/known_hosts:2
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-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
上述配置没配就发现卡到gssapi这。就大概知道是gssapi的问题。
实际上在linux系统优化部分就应该优化SSH服务的此处。
ssh连接其他Linux机器过慢怎么办
ssh连接Linux服务器很慢,主要因为两个原因
1 DNS反向解析的问题。OpenSSH在用户登录的时候会验证IP,它根据用户的IP使用反向DNS找到主机名,再使用DNS找到IP地址。
解决办法是关闭ssh服务器的DNS反响解析,编辑etc/ssh/sshd_config,将UseDNS设置为no;
2 ssh的gssapi认证。
用ssh-v user@server可以看到登录时有如下信息:
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
因此仍然编辑etc/ssh/sshd_config,将GSSAPIAuthentication设置为no。
最后重启sshd服务即可