
在我们公司为跨境电商客户维护的香港机房www.a5idc.com中,曾遇到过一台配备 Dell PowerEdge R640 、运行 Ubuntu Server 22.04 LTS 的重要业务节点出现 SSH 无法远程登录 的故障。该节点承担订单同步与库存数据分发,SSH 登录失败导致我们无法正常进行维护与日志采集,影响了故障恢复效率。
A5数据将结合真实现场经验,深入分析 SSH 配置文件错误导致登录失败的典型原因,并给出逐步解决方案、产品参数、技术细节、代码示例和对比评估表,帮助你在生产环境快速定位与修复类似问题。
一、故障现象与环境概览
1.1 故障现象
-
使用
ssh user@server-ip时提示:bashssh: handshake failed: Connection closed by remote host -
或者出现:
bashPermission denied (publickey,password). -
服务器本地通过
console登录正常。 -
无法用正常密码或密钥登录。
1.2 受影响硬件配置(示例)
| 项目 | 规格 |
|---|---|
| 香港服务器型号 | Dell PowerEdge R640 |
| CPU | 2× Intel Xeon Silver 4214 (12C/24T, 2.2GHz) |
| 内存 | 128GB DDR4 ECC |
| 硬盘 | 2× 960GB NVMe SSD (RAID1) |
| 网络 | Dual 10GbE, BGP CN2+/国际链路 |
| 系统 | Ubuntu Server 22.04.4 LTS |
| SSH 版本 | OpenSSH 8.9p1 Ubuntu 1ubuntu0.3 |
二、初步诊断与收集信息
2.1 使用控制台登录检查系统服务状态
通过机房 KVM / IPMI 控制台登录后,我们首先检查 SSH 服务:
bash
sudo systemctl status ssh
输出可能显示:
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since ...
这说明 SSH 服务启动失败而非网络问题。
2.2 检查 SSH 服务启动错误日志
查看最近的 SSH 错误日志:
bash
sudo journalctl -u ssh -n 100
常见错误包括:
sshd[12345]: /etc/ssh/sshd_config line 67: Bad configuration option: PubkeyAuthen
该错误指出配置文件第 67 行有拼写错误:PubkeyAuthen(拼写错误)应为 PubkeyAuthentication。
三、SSH 配置文件错误修复
3.1 备份当前配置
在进行配置前务必备份:
bash
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak_$(date +%F_%T)
3.2 修复常见的配置错误
以下是修复后的核心 SSH 配置片段示例:
bash
# /etc/ssh/sshd_config
# 基础设置
Port 22
AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::
# 认证方式
PermitRootLogin prohibit-password
PubkeyAuthentication yes
PasswordAuthentication yes # 允许密码登录(根据策略调整)
# 密钥相关
AuthorizedKeysFile .ssh/authorized_keys
# 安全策略
ChallengeResponseAuthentication no
UsePAM yes
# 日志级别
LogLevel VERBOSE
重点检查项:
| 配置项 | 作用 | 推荐值 |
|---|---|---|
PubkeyAuthentication |
开启公钥认证 | yes |
PasswordAuthentication |
开启密码登录 | no/yes(需安全策略评估) |
PermitRootLogin |
root 登录策略 | prohibit-password |
AuthorizedKeysFile |
公钥存放位置 | .ssh/authorized_keys |
注意:生产环境建议关闭
PasswordAuthentication并强制使用密钥登录,但初次排查可开启以排除配置错误。
3.3 检查配置是否语法正确
使用 OpenSSH 内置的配置测试:
bash
sudo sshd -t
若输出为空,则表示语法无误;否则返回错误及行号。
3.4 重启 SSH 服务并验证
bash
sudo systemctl restart ssh
sudo systemctl status ssh
四、登录测试与日志分析
4.1 客户端详细调试
在客户端开启详细调试登录:
bash
ssh -vvv user@server-ip
重点观察以下输出:
- 认证方式是否命中
- 是否发送了公钥
- 服务器返回错误
示例输出片段:
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: RSA SHA256:...
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug1: Authentication succeeded (publickey).
五、其他相关因素排查
5.1 防火墙 (UFW) & 网络访问控制
检查 UFW 配置:
bash
sudo ufw status
确保允许 SSH:
22/tcp ALLOW Anywhere
若使用 iptables:
bash
sudo iptables -L -n | grep 22
5.2 AppArmor 与 PAM
Ubuntu 默认启用了 AppArmor 与 PAM,确保 PAM 配置未阻止登录:
bash
sudo aa-status
sudo pam-auth-update
六、SSH 性能与安全性评估表
在排查登录问题的同时,我们对 SSH 配置的性能与安全性进行了评估:
| 项 | 默认值 | 优化建议 | 是否影响登录故障 |
|---|---|---|---|
| PubkeyAuthentication | yes | 保持 | 否 |
| PasswordAuthentication | no | 根据策略调整 | 否 |
| PermitRootLogin | prohibit-password | 不允许 | 否 |
| MaxAuthTries | 6 | 调低至 3 | 否 |
| LoginGraceTime | 120s | 调低至 60s | 否 |
| LogLevel | INFO | VERBOSE | 辅助诊断 |
七、真实故障还原与解决
在现场,我们发现是以下几处配置错误导致 SSH 登录失败:
PubkeyAuthentication拼写错误- 重复定义
AuthorizedKeysFile导致路径冲突 Match块未正确关闭
例如:
bash
Match User deploy
PasswordAuthentication no
# Missing "Match all" or closing logic
修复上述问题后:
bash
sudo sshd -t && sudo systemctl restart ssh
登录恢复正常。
八、经验总结与防止复发
| 建议 | 实施方法 |
|---|---|
| 配置变更必须备份 | 每次修改前 cp 备份 |
| 使用语法检查工具 | sshd -t |
| 增强日志级别 | LogLevel VERBOSE |
| 使用密钥登录替代密码 | 强化安全 |
| 审计历史变更 | 使用版本控制管理 /etc/ssh/sshd_config |
九、参考代码清单
bash
# 备份
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
# 语法检测
sudo sshd -t
# 重启服务
sudo systemctl restart ssh
十、结语
SSH 登录失败,往往不是网络简单阻断,而是配置文件中一个字符的拼写错误、权限设置不当或字段冲突导致。在生产环境中尤其要严谨对待 SSH 配置文件,通过备份、语法检查、详细日志分析和安全策略评估来快速定位与解决问题。