加速ssh连接的方法(优化ssh服务)

Posted liuwei-xd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加速ssh连接的方法(优化ssh服务)相关的知识,希望对你有一定的参考价值。

加速ssh连接的方法(优化ssh服务)

本文参考于https://www.cnblogs.com/fwdxl/p/6723820.html,在其基础上略做整理。

一、修改 /etc/ssh/sshd_config

经常出现ssh连接缓慢,让人误以为是ssh连接不上。原因是因为server的sshd会去DNS查找访问
client IP的hostname,如果DNS不可用或者没有相关记录,就会耗费大量时间。

vim /etc/ssh/sshd_config
UseDNS no
GSSAPIAuthentication no
GSSAPICleanupCredentials no

配置文件中[UseDNS yes]被注释,但默认开关就是yes...(SSH服务默认启用了DNS反向解析的功能)

二、修改被连接端的文件 /etc/nsswitch.conf

hosts: files dns 去掉dns

这行的含义是对于访问的主机进行域名解析的顺序,是先访问file,也就是/etc/hosts文件,如果hosts中没有记录域名,则访问dns,进行域名解析;如果dns也无法访问,就会等待访问超时后返回,因此等待时间比较长。那如果将这一行屏蔽掉是不是也可以达到同样的效果呢?应该是可以的,但是如果本机要通过域名访问其他服务器,则肯定无法访问,因此这行理论上应该需要保留。这个问题也提示我们,dns如果不可用,会带来的一些副作用的。

三、修改主动连接端的 /etc/hosts

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

四、查看连接过程 ssh -v

[[email protected] ~]# ssh -v [email protected]
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 192.168.75.136 [192.168.75.136] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5* compat 0x0c000000
debug1: Authenticating to 192.168.75.136:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: diffie-hellman-group-exchange-sha256
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: [email protected] compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: [email protected] compression: none
debug1: kex: diffie-hellman-group-exchange-sha256 need=16 dh_need=16
debug1: kex: diffie-hellman-group-exchange-sha256 need=16 dh_need=16
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<3072<8192) sent
debug1: got SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: got SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: ssh-rsa SHA256:U+p+ybfgROyI0fCuyLffRhMMBeDIuzR4AdVWfNO7MhY
The authenticity of host '192.168.75.136 (192.168.75.136)' can't be established.
RSA key fingerprint is SHA256:U+p+ybfgROyI0fCuyLffRhMMBeDIuzR4AdVWfNO7MhY.
RSA key fingerprint is MD5:a5:98:06:58:84:35:d3:f4:4c:f8:43:a1:a5:12:f5:8e.
Are you sure you want to continue connecting (yes/no)? yes    保存秘钥的交互信息
Warning: Permanently added '192.168.75.136' (RSA) to the list of known hosts.
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 4294967296 blocks
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
debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)

debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)

debug1: Next authentication method: publickey
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: Trying private key: /root/.ssh/id_ed25519
debug1: Next authentication method: password
[email protected]'s password:     输入root用户登录密码
debug1: Authentication succeeded (password).
Authenticated to 192.168.75.136 ([192.168.75.136]:22).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: pledge: network
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
Last login: Tue Jun 18 22:35:15 2019 from 192.168.75.145

以上是关于加速ssh连接的方法(优化ssh服务)的主要内容,如果未能解决你的问题,请参考以下文章

Linux优化远程SSH连接

centos7配置ssh连接

Debian 打开SSH服务以及SSH基本排错方法

Linux SSH端口更改和SSH远程连接服务慢原因排查

如何调试SSH连接

ansible的优化