CentOS SSH连接速度慢

CentOS SSH连接速度慢,网上常见解决方法已试,还未解决。

当前配置如下,有没有大佬出来说两句。

cat /etc/ssh/sshd_config

#    $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# The default requires explicit activation of protocol 1
#Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Ciphers and keying
#RekeyLimit default none

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#RSAAuthentication yes
#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile    .ssh/authorized_keys

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes

# GSSAPI options
#GSSAPIAuthentication yes
GSSAPIAuthentication no
GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding no
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
UsePrivilegeSeparation sandbox        # Default for new installations.
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

# override default of no subsystems
Subsystem    sftp    /usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#    X11Forwarding no
#    AllowTcpForwarding no
#    PermitTTY no
#    ForceCommand cvs server

以上是几年前机器遇到的,现在已经没有验证机器了。
最近遇到了另一个情况,ssh连接卡在: debug1: pledge: network
解决办法:
truncate -s 0 /var/log/btmp

阅读 4.9k
1 个回答

连接慢的主要原因是DNS解析导致
一、测试查找具体原因:
1、使用ssh -v host进行debug

ssh -v 192.168.100.10

然后就会输出一大堆debug,通过debug信息就可以看到连接到什么地方被耽搁了
比如会显示如下信息:
[html] view plain copy
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
No credentials cache found

2、检测连接时间

time ssh root@192.168.100.10 exit

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

注意:修改之后记得重启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

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题