鍍金池/ 問(wèn)答/Java  Linux/ CentOS SSH連接速度慢

CentOS SSH連接速度慢

CentOS SSH連接速度慢,網(wǎng)上常見(jiàn)解決方法已試,還未解決。

當(dāng)前配置如下,有沒(méi)有大佬出來(lái)說(shuō)兩句。

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
回答
編輯回答
爆扎

連接慢的主要原因是DNS解析導(dǎo)致
一、測(cè)試查找具體原因:
1、使用ssh -v host進(jìn)行debug

ssh -v 192.168.100.10

然后就會(huì)輸出一大堆debug,通過(guò)debug信息就可以看到連接到什么地方被耽擱了
比如會(huì)顯示如下信息:
[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、檢測(cè)連接時(shí)間

time ssh root@192.168.100.10 exit

二、解決方法(建議一個(gè)個(gè)設(shè)置,因?yàn)槊總€(gè)人連接慢的原因都不一樣):

注意:修改之后記得重啟sshd服務(wù)

service sshd restart

1、關(guān)閉DNS反向解析
在linux中,默認(rèn)就是開(kāi)啟了SSH的反向DNS解析,這個(gè)會(huì)消耗大量時(shí)間,因此需要關(guān)閉。

vi /etc/ssh/sshd_config

UseDNS=no

在配置文件中,雖然UseDNS yes是被注釋的,但默認(rèn)開(kāi)關(guān)就是yes

2、關(guān)閉SERVER上的GSS認(rèn)證
在authentication gssapi-with-mic有很大的可能出現(xiàn)問(wèn)題,因此關(guān)閉GSS認(rèn)證可以提高ssh連接速度。

vi /etc/ssh/sshd_config

GSSAPIAuthentication no

3、修改server上nsswitch.conf文件

vi /etc/nsswitch.conf

找到
hosts: files dns
改為
hosts:files

hosts: files dns這一行含義是對(duì)于訪(fǎng)問(wèn)的主機(jī)進(jìn)行域名解析的順序,是先訪(fǎng)問(wèn)file,也就是/etc/hosts文件,如果hosts中沒(méi)有記錄域名,則訪(fǎng)問(wèn)dns,進(jìn)行域名解析,如果dns也無(wú)法訪(fǎng)問(wèn),就會(huì)等待訪(fǎng)問(wèn)超時(shí)后返回,因此等待時(shí)間比較長(zhǎng)。
注意:如果SERVER需要通過(guò)域名訪(fǎng)問(wèn)其他服務(wù)器,則需要保留此行。

4、修改SERVER上resolv.conf文件
4.1、刪除/etc/resolv.conf中所有不使用的IP。
4.2、把nameserver全部刪除,問(wèn)題也能解決,但是服務(wù)器就無(wú)法上網(wǎng)了。
4.3、如果SERVER曾經(jīng)配置過(guò)雙網(wǎng)卡,則在該文件中會(huì)有一行目前不使用的IP地址,刪除該行即可。

5、修改SERVER上hosts文件
在SERVER上/etc/hosts文件中把客戶(hù)端的IP和HOSTNAME加入

6、打開(kāi)SERVER上的IgnoreRhosts參數(shù)
IgnoreRhosts參數(shù)可以忽略以前登錄過(guò)主機(jī)的記錄,設(shè)置為yes后可以極大的提高連接速度

vi /etc/ssh/sshd_config

IgnoreRhosts yes

7、修改客戶(hù)端的hosts文件
將目標(biāo)SERVER的IP和域名加上去,使得本機(jī)的DNS服務(wù)能解析目標(biāo)地址。

vi /etc/hosts

192.168.100.11 doiido.com

注:hosts文件格式為'目標(biāo)SERVER_IP 目標(biāo)SERVER_NAME'。但是使用這個(gè)方法有一個(gè)弊端,如果需要給每臺(tái)SERVER都添加一個(gè)域名解析。

8、修改客戶(hù)端配置文件ssh_conf(注意,不是sshd_conf)

vi /etc/ssh/ssh_conf

找到
GSSAPIAuthentication yes
改為
GSSAPIAuthentication no

2017年4月17日 06:44