返回首页

SSH 连接调试完全指南 — ssh -vvv 输出解读 + 服务端日志联查 + 典型问题排查

📅 创建于 2026-05-15 🔄 更新于 2026-05-15 📝 924 字

SSH 连接调试完全指南

作者:Linux能量补给站

排查 SSH 连接问题,核心在于 ssh -vvv 客户端输出 + 服务端日志联查。本文覆盖四个调试阶段(连接建立 → 协议协商 → 身份认证 → 会话),配合真实输出示例和分析方法。


一、整体排查流程

ssh -vvv 输出 + 服务端日志 → 判断失败阶段 → 定位根因

关键原则:ssh -vvv 最后几条日志,找到卡住或报错的位置;再用服务端日志确认是"根本没收到请求"还是"收到了但拒绝"。


二、读取 ssh -vvv 的关键阶段

1. 连接建立阶段

debug1: Connecting to 192.168.1.100 [192.168.1.100] port 22.
debug1: Connection established.
  • 卡在 Connecting to... 后无 established网络/防火墙/端口问题
  • 出现 Connection refused → 服务端 sshd 未运行或端口错误
  • 出现 No route to host → 路由不通

2. 协议协商阶段

debug1: Remote protocol version 2.0, remote software version OpenSSH_8.9p1
debug1: match: OpenSSH_8.9p1 pat OpenSSH* compat 0x04000000
debug1: SSH2_MSG_KEXINIT sent
debug3: receive packet: type 20
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
...
debug1: SSH2_MSG_NEWKEYS sent
debug1: SSH2_MSG_NEWKEYS received
SSH2_MSG 含义 问题信号
KEXINIT 密钥交换初始化 卡在 KEXINIT → 不兼容的算法或网络中断
NEWKEYS 加密层建立成功 看到即加密通道可用,继续排查认证阶段

看到 SSH2_MSG_NEWKEYS received 说明加密层建立成功,问题在更之后的阶段。

3. 身份认证阶段(问题最高发区域)

debug1: Authentications that can continue: publickey,password
debug3: preferred gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /home/user/.ssh/id_ed25519 ED25519 SHA256:xxxx
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password

关键数据包:

包类型 含义
type 50 客户端发送公钥认证请求
type 51 服务端拒绝该公钥(原因在服务端日志里)

认证失败信号链:

  1. 所有密钥被拒绝或跳过
  2. 最后出现 Permission denied (publickey,password)No more authentication methods to try
  3. 服务端日志中会有对应的拒绝记录

4. 认证成功/失败

# 成功
debug1: Authentication succeeded (publickey).

# 失败
debug1: No more authentication methods to try.
Permission denied (publickey,password).

三、服务端日志的查找方式

强烈建议: 先将服务端日志级别调成 VERBOSEsshd_config 里改 LogLevel VERBOSE,重启 sshd),这样能看到认证失败的具体原因。

不同系统的日志路径

系统类型 查看命令 说明
Systemd 系统 journalctl -u sshd -f 实时观察
Debian/Ubuntu tail -f /var/log/auth.log 传统 syslog
RHEL/CentOS tail -f /var/log/secure 传统 syslog
自定义配置 sshd_configSyslogFacility AUTH + LogLevel VERBOSE 灵活性最高

服务端日志常见拒绝原因

# 密钥权限错误
Authentication refused: bad ownership or modes for directory /home/user
Authentication refused: bad ownership or modes for file /home/user/.ssh/authorized_keys

# 使用不支持的算法
Unable to negotiate with 192.168.1.100 port 22: no matching key exchange method found.

# Match 块限制
Authentication refused: User matches Match block with restriction

# 用户不存在
Failed password for invalid user admin from 192.168.1.100 port 50000 ssh2

四、典型问题排查(附真实输出示例)

4.1 密钥权限错误 —— 文件/目录权限过于开放

现象: 客户端日志显示公钥被忽略,最后 Permission denied (publickey)

客户端 ssh -vvv 关键片段:

debug1: Offering public key: /home/user/.ssh/id_ed25519 ED25519 SHA256:xyz
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey

服务端日志(LogLevel VERBOSE):

Authentication refused: bad ownership or modes for directory /home/user
# 或
Authentication refused: bad ownership or modes for file /home/user/.ssh/authorized_keys

根因: ~/.ssh 目录或 authorized_keys 文件权限过于开放,sshd 拒绝读取。

解决:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 644 ~/.ssh/id_ed25519.pub     # 公钥可读
chmod 600 ~/.ssh/id_ed25519         # 私钥必须 600

运维检查命令:stat 确认权限,ls -la ~/.ssh/ 一目了然。

4.2 密钥交换算法不匹配

现象: 握手阶段直接卡死或报 no matching key exchange method found

客户端 ssh -vvv:

debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: (not found)
Unable to negotiate with 192.168.1.100 port 22: no matching key exchange method found.

服务端日志:

sshd[12345]: fatal: Unable to negotiate a key exchange method

根因: 客户端/服务端没有共同支持的算法(常见于老旧系统连接新版 OpenSSH,或新版客户端连接老旧系统)。

客户端侧解决(临时兼容):

ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 user@host

服务端侧解决(推荐):

# /etc/ssh/sshd_config
KexAlgorithms curve25519-sha256,diffie-hellman-group-exchange-sha256

4.3 认证方法被限制(Match 块配置)

现象: 用户名或来源 IP 被 sshd 的 Match 块限制。

客户端 ssh -vvv:

debug1: Authentications that can continue: publickey

支持的方法列表很短——"KbdInteractive"、"password" 等被过滤掉了,说明服务端将"password"等方法排除在外。如果连 publickey 也不在列表里,说明服务端彻底拒绝了认证。

服务端日志:

Authentication refused: User matches Match block with restriction
Connection closed by authenticating user admin 192.168.1.100 port 50000 [preauth]

解决: 检查 /etc/ssh/sshd_config 中的 Match 块配置:

Match Address *,!192.168.1.0/24
    AuthenticationMethods publickey
Match User someuser
    PasswordAuthentication no

4.4 主机密钥验证失败

现象: 之前连过的服务器重装后,SSH 报主机密钥不匹配。

客户端 ssh -vvv:

debug1: Host '192.168.1.100' is known and matches the ED25519 host key.
# 如果匹配失败会报:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

解决:

# 确认服务器无异常后,移除旧密钥
ssh-keygen -R "192.168.1.100"

五、实战案例复盘

场景:用户无法 SSH 连接到服务器

步骤 1:客户端运行 ssh -vvv

debug1: Connecting to 203.0.113.50 [203.0.113.50] port 22.
debug1: Connection established.
...
debug1: SSH2_MSG_NEWKEYS received    ← 加密层OK
...
debug1: Offering public key: /home/user/.ssh/id_ed25519
debug3: send packet: type 50
debug3: receive packet: type 51      ← 密钥被拒
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/user/.ssh/id_rsa
debug3: no such identity: /home/user/.ssh/id_rsa: No such file or directory
...
debug1: No more authentication methods to try.
Permission denied (publickey,password).

步骤 2:检查服务端日志

journalctl -u sshd -n 20 --no-pager
# 输出:Authentication refused: bad ownership or modes for directory /home/user

根因: /home/user 目录权限被错误修改(chmod 777 /home/user),sshd 认为不安全。

修复:

chmod 755 /home/user
chmod 700 /home/user/.ssh
chmod 600 /home/user/.ssh/authorized_keys
systemctl restart sshd

验证: 再次 ssh user@host 即可成功登录。


六、调试流程速查表

阶段 ssh -vvv 关键信号 可能问题 排查方向
连接建立 卡在 Connecting to... 网络/防火墙/端口 pingtelnet host 22、检查安全组
协议协商 卡在 KEXINITno matching algorithm 算法不兼容 服务端升级 OpenSSH,或加 -oKexAlgorithms=
认证 type 51Permission denied 密钥/权限/配置 查服务端日志(journalctl/tail)
主机密钥 REMOTE HOST IDENTIFICATION HAS CHANGED 服务器重装/中间人 ssh-keygen -R host 后重连
认证成功但断连 Authentication succeeded 后立即断开 shell/授权问题 检查 ~/.bashrc/etc/nologin、PAM 配置

关联页面

页面关联点
ssh-brute-force-protection-guideSSH 暴力破解纵深防御:公钥认证 / fail2ban / 2FA(调试与安全是 SSH 运维一体两面)
network-troubleshooting-order服务器网络排障方法论:分层定位七步法(SSH 连接建立的网络层基础)
linux-user-management-guideLinux 用户管理避坑指南(~/.ssh 目录权限是用户管理安全的典型场景)