SSH老旧算法兼容问题总结

本文最后更新于 2025年12月10日 下午

SSH老旧算法兼容问题总结

1. 背景与问题描述

在现代 Windows 系统预装的 OpenSSH(8.1–9.x 版本)中,为了提升安全性,默认禁用了一批老旧加密/认证算法,包括:

  • 主机密钥算法:ssh-rsassh-dss
  • 密钥交换算法:diffie-hellman-group1-sha1diffie-hellman-group14-sha1
  • 加密算法:3des-cbc、aes128-cbc、aes256-cbc 等 CBC 模式算法
  • MAC 算法:hmac-md5、hmac-sha1 等弱哈希算法

而大量老旧网络设备(华为、华三、锐捷、中兴、Cisco、Juniper 等)仅支持上述老旧算法,导致 Windows 终端通过 SSH 连接时出现以下典型报错:

1
Unable to negotiate: no matching host key type found
1
Unable to negotiate: no matching key exchange method
1
Bad SSH2 cipher spec

Windows 版 OpenSSH 不支持 Linux 系统的 + 算法追加语法,必须配置完整的算法列表才能实现兼容。本文提供可直接落地的手动配置模板与自动化脚本方案。

2. Windows 手动兼容配置方案

2.1 配置文件路径

Windows 系统的 SSH 客户端配置文件路径为:

1
%userprofile%/.ssh/config

.ssh 文件夹不存在,可手动在用户目录下创建。

2.2 完整兼容配置模板

将以下内容写入上述配置文件,可直接兼容绝大多数老旧网络设备(已整合现代算法与老旧算法,兼顾兼容性与基础安全性):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Windows OpenSSH 老旧设备兼容配置
# 适配 OpenSSH 8.1-9.x 版本,支持华为/华三/锐捷等老旧网络设备
Host *
# 主机密钥算法(兼容 ssh-rsa/ssh-dss 及证书模式)
HostkeyAlgorithms ssh-rsa,ssh-dss,ssh-rsa-cert-v01@openssh.com,ssh-dss-cert-v01@openssh.com
# 公钥接受算法(旧设备核心依赖)
PubkeyAcceptedAlgorithms ssh-rsa,ssh-dss

# 密钥交换算法(整合老旧与现代算法)
KexAlgorithms diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha256

# 加密算法(支持 CBC 老旧模式与现代 CTR/GCM 模式)
Ciphers 3des-cbc,aes128-cbc,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com

# MAC 算法(兼容弱哈希与现代安全算法)
MACs hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96,hmac-sha2-256,hmac-sha2-512,umac-64@openssh.com,umac-128@openssh.com,hmac-sha1-etm@openssh.com,hmac-sha1-96-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-md5-etm@openssh.com,hmac-md5-96-etm@openssh.com,umac-64-etm@openssh.com,umac-128-etm@openssh.com

3. Windows 自动化配置脚本(Python)

手动编写完整算法列表易出错,可通过 Python 脚本自动检测本机 OpenSSH 支持的算法,生成适配的兼容配置,并自动备份原有配置。

3.1 脚本功能

  1. 自动调用 ssh -Q 命令获取本机支持的所有算法
  2. 过滤并整合老旧设备所需的兼容算法
  3. 自动备份原有 config 文件(避免配置丢失)
  4. 生成并写入完整的兼容配置

3.2 完整脚本代码

将代码保存为 generate_windows_ssh_config.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import subprocess
import os
import sys
from pathlib import Path

def run_ssh_query(category):
"""
执行: ssh -Q <category>
返回支持的算法列表(list[str])
"""
try:
result = subprocess.run(
["ssh", "-Q", category],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
shell=False
)
if result.returncode != 0:
print(f"警告: 无法执行 ssh -Q {category}: {result.stderr}")
return []
return [line.strip() for line in result.stdout.splitlines() if line.strip()]
except FileNotFoundError:
print("错误: 系统未安装 ssh 客户端(OpenSSH)。")
sys.exit(1)


def generate_config():
cipher_list = run_ssh_query("cipher")
kex_list = run_ssh_query("kex")
mac_list = run_ssh_query("mac")
key_list = run_ssh_query("key")

# 过滤掉 Windows 某些 SSH 不支持作为 HostKeyAlgorithms 的键(只保留 ssh-rsa/ssh-dss 及其 cert)
hostkey_algorithms = [
k for k in key_list
if k.startswith("ssh-rsa") or k.startswith("ssh-dss")
]

# PubkeyAcceptedAlgorithms 同 HostkeyAlgorithms(旧设备最常用)
pubkey_algorithms = hostkey_algorithms.copy()

config_text = f"""
Host *
HostkeyAlgorithms {",".join(hostkey_algorithms)}
PubkeyAcceptedAlgorithms {",".join(pubkey_algorithms)}

KexAlgorithms {",".join(kex_list)}

Ciphers {",".join(cipher_list)}

MACs {",".join(mac_list)}
""".strip()

return config_text


def write_config_file(config_text):
"""
写入 ~/.ssh/config 文件
"""
ssh_dir = Path.home() / ".ssh"
ssh_dir.mkdir(parents=True, exist_ok=True)

config_path = ssh_dir / "config"

backup_path = ssh_dir / "config.backup"

# 若已有 config,先备份
if config_path.exists():
config_path.replace(backup_path)
print(f"已备份旧配置到: {backup_path}")

with open(config_path, "w", encoding="utf-8") as f:
f.write(config_text)

print(f"新 SSH 配置已写入: {config_path}")


def main():
print("正在自动生成 SSH 全算法兼容配置...")
config = generate_config()

print("\n========== 生成的配置如下 ==========\n")
print(config)
print("\n===================================\n")

choice = input("是否写入 ~/.ssh/config (y/n)? ").strip().lower()
if choice == "y":
write_config_file(config)
print("完成。")
else:
print("已取消写入,仅输出配置文本。")


if __name__ == "__main__":
main()

3.3 脚本执行步骤

  1. 确保 Windows 已安装 Python 环境和 OpenSSH 客户端(OpenSSH 可在「设置-应用-可选功能」中安装)
  2. 双击运行脚本或在终端执行命令:
    1
    python generate_windows_ssh_config.py
  3. 查看生成的配置并确认是否写入,完成后即可正常连接老旧设备

4. 配置验证方法

配置完成后,可通过以下命令验证配置是否生效:

  1. 查看生效算法列表(替换为目标设备 IP):

    1
    ssh -G admin@1.2.3.4

    输出中可查看 hostkeyalgorithmskexalgorithms 等字段的最终生效列表,确认包含老旧算法。

  2. 测试设备连接

    1
    ssh admin@1.2.3.4

    若能正常建立连接,说明配置生效;若仍报错,可通过 ssh -v admin@1.2.3.4 开启调试模式,查看算法协商失败的具体原因。

5. 注意事项

  1. 安全性提醒:老旧算法存在一定安全风险,建议仅对必要的老旧设备启用,现代服务器仍使用默认安全配置。
  2. 版本适配:若为极新版本 OpenSSH(10.0+),部分超老旧算法可能被彻底移除,需确认设备支持的算法后针对性配置。
  3. 权限问题.ssh 文件夹及 config 文件需保证当前用户有读写权限,避免配置无法生效。

SSH老旧算法兼容问题总结
https://xinhaojin.github.io/2025/12/10/SSH老旧算法兼容问题总结/
作者
xinhaojin
发布于
2025年12月10日
许可协议