Linux SSH登录慢问题解决

Linux服务器ssh登录时超级慢,需要几十秒。其它服务器均没有这个问题。平时登录操作都默默忍了。今天终于忍不住想搞清楚到底什么原因。搜索了一下发现了很多关于ssh登录慢的资料,于是自己也学着来分析、印证一下ssh登录慢的原因。

出现ssh登录慢一般有两个原因:DNS反向解析的问题和ssh的gssapi认证

1:ssh的gssapi认证问题

GSSAPI ( Generic Security Services Application Programming Interface) 是一套类似Kerberos 5 的通用网络安全系统接口。该接口是对各种不同的客户端服务器安全机制的封装,以消除安全接口的不同,降低编程难度。但该接口在目标机器无域名解析时会有问题,默认情况下,GSSAPIAuthentication在服务器端和客户端都激活的。如果DNS服务出现问题,那么登录过程要等到DNS查询超时后才能继续,这就是为什么SSH登录提示符要等很久才出现的原因。 为什么ssh登录过程中要用到DNS解析服务呢?这个是GSSAPI认证方式需要的缘故。

所以在配置文件/etc/ssh/sshd_config(服务器)或/etc/ssh/ssh_config(客户端)将参数GSSAPIAuthentication设置为no可以解决ssh登录慢的问题。

2:DNS反向解析的问题

OpenSSH在用户登录的时候会验证IP,它根据用户的IP使用反向DNS找到主机名,再使用DNS找到IP地址,最后匹配一下登录的IP是否合法。如果客户机的IP没有域名,或者DNS服务器很慢或不通,那么登录就会很花时间。

首先可以在ssh命令后面加上“-v“ 参数,输出debug信息定位问题。 具体操作为ssh -v root@serverip

root@rruoam-KVM:~# ssh -v root@172.21.6.76
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 172.21.6.76 [172.21.6.76] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /home/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory
debug1: identity file /home/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 172.21.6.76:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:YmF4NFFQLbxSuTyNLHh9UtekoRyB8/Vt3b9RcLYWvdY
debug1: Host '172.21.6.76' is known and matches the ECDSA host key.
debug1: Found key in /home/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
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: FILE:/tmp/krb5cc_0)

debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: FILE:/tmp/krb5cc_0)

debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:3M1SY2a+dVHLKnbsK3Je2MaG4uvnNWTgCEZ26SLvP0M /home/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /home/.ssh/id_dsa
debug1: Trying private key: /home/.ssh/id_ecdsa
debug1: Trying private key: /home/.ssh/id_ed25519
debug1: Next authentication method: password
root@172.21.6.76's password: 

从上面输出信息看到有关于Unspecified GSS failure,于是我将/etc/ssh/sshd_config(服务器)或/etc/ssh/ssh_config(客户端)将参数GSSAPIAuthentication设置为no,重启了sshd服务,测试发现ssh登录还是很慢。

那么原因应该是DNS反向解析的问题,关于DNS反向解析的问题有几个解决方法:

1:在server上/etc/hosts文件中把常用的ip和hostname加入,然后在/etc/nsswitch.conf看看程序是否先查询hosts文件

2:在server上/etc/ssh/sshd_config文件中修改或加入UseDNS=no。然后重启sshd服务

我在/etc/ssh/sshd_config上将UseDNS设置为no,重启sshd服务后,然后测试ssh连接速度。果然飞快连接上。看来主要还是DNS反向解析的问题。

SSH连接的原因有多种可能性。根据引用中的分析结果,可能的原因有以下几种: 1. DNS解析问题:可以在服务器的/etc/hosts文件中将本机的IP和主机名加入,或者在/etc/ssh/sshd_config文件中将UseDNS设置为no来解决。 2. resolv.conf配置问题:注释掉不使用的IP地址行可以解决。 3. nsswitch.conf配置问题:将/etc/nsswitch.conf文件中的hosts修改为hosts: files即可。 4. GSSAPIAuthentication配置问题:在/etc/ssh/sshd_config文件中将GSSAPIAuthentication设置为no可以解决。 其中,方法1和方法5的修改可能会解决问题。同时,如果服务器无法连接外网,可以将/etc/resolv.conf中的nameserver全部注释掉来解决。 此外,根据引用中的信息,SSH连接时系统会依次尝试publickey、gssapi-keyex、gssapi-with-mic和password等认证方式。如果用户只使用password认证方式,可以在客户端的ssh_config文件中进行相应的修改来提高连接速度。 总之,在排查SSH连接问题时,可以结合上述方法进行逐一排查和修改,以找到最适合的解决方案。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [linux ssh连接](https://blog.csdn.net/weixin_33682790/article/details/85145970)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【已解决linux ssh 远程连接服务器,出现登陆、打字卡顿输入卡顿解决方案](https://blog.csdn.net/idiot5lie/article/details/114167486)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值