1.SSH慢(卡住)现象
SSH(Xshell)连接远程Linux(Centos 7.6)到如下界面后,卡住2-3分钟后才成功连接上。
2.分析慢原因
2.1.查看方法
按WIN+R键后弹出的对话框输入cmd后点击确定,输入 ssh -v root@192.168.118.201 回车,即ssh 加上-v参数,查看ssh远程连接的详细过程,可以看慢在什么位置。简约过程如下:
# 注意:以下"..."代表省略了一部分内容
C:\Users\HOST_NAME>ssh -v root@192.168.118.201
OpenSSH_for_Windows_9.2p1, LibreSSL 3.6.1
debug1: Reading configuration data C:\\Users\\GY_LL/.ssh/config
debug1: Connecting to 192.168.118.201 [192.168.118.201] port 22.
debug1: Connection established.
...
debug1: SSH2_MSG_EXT_INFO received ## 卡住一段时间
...
debug1: Next authentication method: password
root@192.168.118.201's password:
...
debug1: pledge: fork
Last login: Mon Dec 16 01:21:45 2024 from gateway
[root@localhost ~]# ## 连接成功
2.2.本文遇到的问题
观察到是"debug1: SSH2_MSG_SERVICE_ACCEPT received"处慢。
debug1: Found key in C:\\Users\\BB_AA/.ssh/known_hosts:13
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: get_agent_identities: ssh_fetch_identitylist: agent contains no identities
debug1: Will attempt key: C:\\Users\\BB_AA/.ssh/id_rsa
debug1: Will attempt key: C:\\Users\\BB_AA/.ssh/id_ecdsa
debug1: Will attempt key: C:\\Users\\BB_AA/.ssh/id_ecdsa_sk
debug1: Will attempt key: C:\\Users\\BB_AA/.ssh/id_ed25519
debug1: Will attempt key: C:\\Users\\BB_AA/.ssh/id_ed25519_sk
debug1: Will attempt key: C:\\Users\\BB_AA/.ssh/id_xmss
debug1: Will attempt key: C:\\Users\\BB_AA/.ssh/id_dsa
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 ## 该处会卡住30秒左右
2.3.可能原因
"debug1: SSH2_MSG_SERVICE_ACCEPT received" 处卡住可能的原因是DNS解析导致,如果UseDNS打开(默认打开),服务器先会根据客户端IP地址进行DNS RPT反向查询客户端主机名,然后根据查询的客户端主机名进行DNS正向A记录查询,并验证是否与原始IP地址一致,通过该方法防止客户端欺骗。
3.尝试处理方法
3.1.修改ssh中DNS配置
Linux(Centos 7.6)配置文件/etc/ssh/sshd_config修改UseDNS为no。
修改前:
修改后:
3.2.重启sshd服务后尝试重新连接
[root@localhost ~]# systemctl restart sshd.service
[root@localhost ~]#
使用Xshell重新尝试连接即可。
注:本文记录的问题是DNS的问题,"debug1: SSH2_MSG_SERVICE_ACCEPT received"卡住也有可能是其他问题导致的,如后续有越到再更新到本文。