作者:用户gum5gltoo8 | 来源:互联网 | 2024-12-25 11:31
在局域网环境中使用SSH连接到服务器时,有时会遇到命令执行后需要等待较长时间才能输入密码的情况。具体表现为执行ssh root@192.168.50.176
后,回车之后要等待十几秒甚至更久。以下是详细的排查和解决步骤。
1. 使用ssh -v
查看连接详细信息
为了更好地理解问题所在,可以通过增加详细输出来查看SSH连接过程中的每一步操作。运行以下命令:
ssh -v root@192.168.50.176
这将显示连接过程中每一个阶段的日志信息。从日志中可以看到,连接建立、密钥交换、身份验证等各个步骤的具体情况。例如:
OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5
debug1: Connecting to 192.168.50.176 [192.168.50.176] port 22.
debug1: Connection established.
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_7.7
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.50.176: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: compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:2ePNJ/cQW7AIsD+veSBc5XS2xy0kmB3W6Dk3uVnBwDQ
debug1: Host ‘192.168.50.176‘ is known and matches the ECDSA host key.
debug1: Found key in C:\Users\StaryJie/.ssh/known_hosts:11
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_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:Y5wPUy0NUgYMwwSO4juFhw52YyleflOzomjnNvsokXw C:\Users\StaryJie/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: C:\Users\StaryJie/.ssh/id_dsa
debug1: Trying private key: C:\Users\StaryJie/.ssh/id_ecdsa
debug1: Trying private key: C:\Users\StaryJie/.ssh/id_ed25519
debug1: Trying private key: C:\Users\StaryJie/.ssh/id_xmss
debug1: Next authentication method: password
debug1: read_passphrase: can’t open /dev/tty: No such file or directory
root@192.168.50.176’s password:
从日志中可以看到,在debug1: SSH2_MSG_SERVICE_ACCEPT received
之后,连接似乎卡住了,直到开始尝试密码验证。
2. 解决方法
2.1 修改SSH配置文件
编辑服务器上的/etc/ssh/sshd_config
文件,进行以下修改以减少DNS查询时间:
UseDNS no
此外,确保其他相关配置项也已正确设置,如:
[root@staryjie ~]# egrep -v "^#|^$" /etc/ssh/sshd_config
Host *
ForwardX11Trusted yes
SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
SendEnv XMODIFIERS
SAPIAuthentication no
UseDNS no
2.2 添加本地电脑IP到/etc/hosts
确保本地计算机的IP地址已经添加到服务器的/etc/hosts
文件中。例如:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.50.29 windows
2.3 重启SSH服务
完成上述修改后,重启SSH服务以使更改生效:
systemctl restart sshd.service
3. 查看效果
优化前后的连接速度对比:
通过以上步骤,局域网内的SSH连接延迟问题应得到显著改善。