我关闭了 SSH 22 端口(以及我所替代的方案)
I close SSH port 22 (and what I use instead)

原始链接: https://www.michelebologna.net/2026/ssh-port-22-fwknop-single-packet-authorization/

标准的 SSH 加固(使用密钥和 `fail2ban`)仍会将 22 端口暴露给扫描器和潜在的零日漏洞。要真正保障服务器安全,应在用户证明身份之前让 SSH 服务处于不可达状态。 作者建议使用 **fwknop**(防火墙敲门守护程序)并配合 **单包授权 (SPA)**。与容易受到数据包嗅探和重放攻击的传统端口敲门(Port Knocking)不同,SPA 会向服务器发送一个经过加密且带有 HMAC 验证的 UDP 数据包。验证通过后,服务器会动态更新防火墙,在有限时间内为特定客户端 IP 开放 22 端口。 从攻击者的角度来看,服务器保持完全静默;22 端口显示为“已过滤”(关闭),从而有效消除了日志冗余并防止了针对 SSH 的漏洞攻击。 **实现细节:** * **自动化:** 服务器端的部署和防火墙规则通过 Ansible 管理。 * **无缝访问:** SSH 配置中的客户端 `ProxyCommand` 会自动执行敲门操作,使用户无需感知该过程。 * **冗余:** Tailscale 接口提供了一条独立的并行访问路径作为安全保障。 这种方法将 SSH 从面向互联网的服务转变为一种经过身份验证的“隐形”资源,显著减少了服务器的攻击面。

这篇 Hacker News 讨论探讨了关闭 SSH 22 端口并改用“端口敲门”(port knocking)或其他访问方式的利弊。 批评者认为,用定制且可能存在安全隐患的代码来取代“久经考验”的标准 SSH 服务,反而会引入新的漏洞。而支持者则提倡“纵深防御”策略,认为尽管 SSH 本身很稳健,但通过地理屏蔽、fail2ban、基于 HMAC 的端口敲门或将 SSH 运行在 443 端口等手段增加防护层,可以有效减小攻击面。 许多评论者指出,比起定制的端口敲门方案,WireGuard、Tailscale 等现代替代方案,或是通过 VPN 和防火墙限制 SSH 访问更为高效且易于管理。也有人认为,只要服务器配置得当,仅强制执行公钥认证就已足够。总的来说,目前的共识趋向于避免将服务直接暴露在公网,转而倾向于通过现代隧道协议或严格的网络级访问控制来实现受限访问。
相关文章

原文

Most SSH hardening guides stop at key-only auth and fail2ban. That helps, but if your machine has a public IP, port 22 is still open to the internet. Every automated scanner can probe it and get a response: the SSH version string, the banner, proof that something is listening.

The log noise alone is annoying: even with key-only auth, failed attempts pile up every day. But the bigger problem is exposure. If a zero-day drops in OpenSSH, every server with port 22 open is a target before you have time to patch. That has happened before.

I wanted the SSH daemon itself to be unreachable: no banner, no version string, nothing for nmap to work with. Not just rate-limited or hidden behind a non-standard port, but genuinely not connectable unless you already hold the key.

The idea: port knocking, and why it is not enough on its own