必须只启用SSHv2并配置强加密算法:设置Protocol 2,禁用CBC等弱加密,仅保留chacha20-poly1305、AES-GCM等安全密钥交换与MAC算法,停用RSA主机密钥、密码登录和root直连。

只允许 SSH 协议 v2,并强制使用现代强加密算法,是加固 SSH 的基础动作。OpenSSH 不会保留默认算法,必须显式列出你认可的算法,否则服务启动时可能拒绝连接或降级使用弱算法。
明确禁用 SSHv1 并锁定协议版本
编辑 /etc/ssh/sshd_config,确保只存在这一行(删除或注释掉所有含 Protocol 1 的配置):
Protocol 2
该设置可阻断所有基于 SSH-1 的连接尝试,包括自动化扫描工具常用的 CRC32 漏洞利用路径。
只启用安全的加密、认证与密钥交换算法
先查本机支持项,再精简配置——不要照搬网上模板:
- 运行 ssh -Q cipher,剔除含 cbc、arcfour、3des、blowfish 的算法
- 运行 ssh -Q mac,只保留带 etm@openssh.com 后缀的 SHA2 类型(如 hmac-sha2-256-etm@openssh.com)
- 运行 ssh -Q kex,排除 group1-sha1、diffie-hellman-group14-sha1 等已知弱交换方式
在配置文件末尾顶格写入三行(无缩进、无多余空格、不换行):
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com
KexAlgorithms curve25519-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256
配套关闭高风险入口
仅改算法不够,还需切断其他攻击面:
- 停用 RSA 主机密钥:注释掉所有 HostKey /etc/ssh/ssh_host_rsa_key 行,只保留 ed25519 和 ecdsa 密钥
- 禁用密码登录(若已配好密钥):PasswordAuthentication no
- 禁止 root 直连:PermitRootLogin no
验证并安全重载
保存后执行:
- sudo sshd -t —— 零输出表示语法正确
- sudo systemctl reload sshd —— 不中断已有连接
- 新开终端执行 ssh -vv user@localhost,搜索日志中 kex:、cipher:、mac: 行,确认实际协商的是你指定的算法


















