Any server with port 22 reachable from the Internet gets scanned within minutes. Noisy login attempts can be contained with fail2ban and key-only authentication, but the port stays visible: anyone can talk to sshd and try to exploit a vulnerability, should one exist. sshknock starts from a different idea: if the port is closed to everyone, there is nothing to scan and nothing to attack.
The project is open source (BSD 3-Clause license) and the code is at github.com/axiom4/sshknock.
The idea: Single Packet Authorization
sshknock implements a Single Packet Authorization (SPA) for SSH. Port 22 stays closed to everyone. When the client sends a single signed UDP packet, the daemon on the server verifies the signature and opens the port only for the client’s IP. The window closes at the first connection and in any case after at most 60 seconds. Anyone without an authorized key gets no reply at all.

--ssh-after, automatic knock from ~/.ssh/config, and RHEL with firewalld.Why it is not ordinary port knocking
- No new secrets. The packet is signed with
ssh-keygen -Y sign, so with the same SSH key you already use, even through ssh-agent. The server verifies the signature against theauthorized_keysthatsshdalready reads (resolved viasshd -T). Certificates,cert-authority,from=andexpiry-timeare supported. - The same policy as sshd. Before opening anything, the daemon queries
sshd -T -C user=...,addr=...,laddr=...,lport=.... It therefore honorsMatchblocks,AllowUsers/DenyUsers,AllowGroups/DenyGroups,PermitRootLogin,nologinshells and expired accounts: a knock cannot open access that SSH would refuse anyway. - No listening port. The daemon captures packets with an AF_PACKET socket, as tcpdump would, before netfilter. There is no UDP socket to scan. The confirmation is sent through a raw socket only after a valid signature.
How it works
- The client signs a message containing the user, the target host, an optional source IP, a timestamp and a nonce, and sends it over UDP.
- The daemon checks the timestamp, host, source and nonce.
- It verifies the signature against
authorized_keysand checks thesshdpolicy. - It adds the IP to the firewall set for 60 seconds and replies “open”.
- The client opens the SSH connection. As soon as conntrack sees it as
ESTABLISHED, the IP is removed from the set.
No UDP port open, no daemon listening
This is what sets sshknock apart from almost every knocking solution. On the server there is no firewall rule that opens a UDP port and no process listening on UDP: ss -ulpn shows nothing from sshknock, and a scan from the outside sees port 22 closed and no UDP port open.
The knock packet is observed, not received. The daemon opens an AF_PACKET socket, the same capture mechanism that tcpdump and libpcap rely on, and reads frames straight from the network interface, before the firewall processes them. To the kernel, the UDP packet is a datagram addressed to a closed port and it is dropped like any other: no application receives it and no rule had to accept it. Meanwhile a copy has already been read by the daemon, which verifies its signature.
The practical consequences are three:
- Zero network attack surface. With no listening socket there is no service to discover, probe or crash with malformed packets.
- Total silence toward strangers. Probes get exactly the behavior of a closed port. Only after a valid signature does the daemon send the confirmation, through a raw socket.
- Firewall unchanged. The only change to the rules is the temporary addition of the client’s IP to an existing set, and only for the SSH port, never for UDP.
A detail for the pedantic: sshknock does not use the libpcap library. It reads frames directly from the AF_PACKET socket using only the Python standard library, so it stays one file per side with no extra dependencies.
Security by design
Anti-replay. The timestamp is accepted with a ±20 second tolerance, the nonce is persisted in /var/lib/sshknock, the target host is signed and, with --src-ip, so is the source IP (with auto the client discovers it via STUN, useful behind NAT).
Least privilege. The daemon runs as the sshknock system user with only the CAP_NET_RAW, CAP_NET_ADMIN and CAP_DAC_READ_SEARCH capabilities, inside the systemd sandbox. ssh-keygen runs without capabilities through setpriv, and the path to authorized_keys is walked safely.
Flood resistance. Four worker threads, a global limit of 20 signature verifications per second (burst 40) and rate-limited rejection logs. The iptables rule restores itself if fail2ban recreates its chains.
Compatibility
- Firewalls: iptables (with ipset, compatible with ufw and fail2ban), nftables (sets inside your own ruleset) and firewalld (D-Bus and polkit, ipsets with timeout).
- Network: IPv4 and IPv6, NAT, WireGuard.
- System: SELinux, tested on Debian stable and on RHEL 9.9 with firewalld and SELinux in enforcing mode. The client also runs on macOS.
- Dependencies: only the Python standard library (3.8+), one file per side; you need OpenSSH 8.0 or later, systemd, conntrack, setpriv and your chosen firewall backend.
Try it in a few minutes
On the server, after cloning the repository:
sudo useradd --system --no-create-home --shell /usr/sbin/nologin sshknock
sudo install -d /opt/sshknock
sudo install -m 755 server/sshknockd.py /opt/sshknock/sshknockd.py
sudo install -m 644 server/sshknockd.service /etc/systemd/system/sshknockd.service
sudo systemctl daemon-reload && sudo systemctl enable --now sshknockd
sudo journalctl -u sshknockd -f
Configuration lives in /etc/sshknock/sshknockd.conf (INI format; there is an example file in the repository). On the client:
install -D -m 755 client/sshknock-client.py ~/.local/bin/sshknock-client.py
sshknock-client.py --host <server> --identity <user> --key ~/.ssh/id_ed25519 --ssh-after
To make the knock transparent, even for scp and rsync, a single entry in ~/.ssh/config is enough:
Host myserver
HostName <server>
User <user>
Match originalhost myserver exec "~/.local/bin/sshknock-client.py --host %h --identity <user> --key ~/.ssh/id_ed25519 >/dev/null"
The client also offers --port, --src-ip, --stun, --wait and --ssh-user, and returns exit codes 0, 1 and 2 for use in scripts. On top of that, the daemon can send Telegram notifications for successful knocks, connections and denied requests.
Why RSA keys cannot be used
The whole knock must fit in a single UDP datagram: the signed JSON header and the ssh-keygen -Y sign signature travel together in one packet. The daemon reads frames straight from the interface and does not reassemble IP fragments, so a packet larger than the network MTU (1500 bytes, i.e. about 1472 bytes of UDP payload on IPv4 and 1452 on IPv6) would never be verified.
An Ed25519 signature is tiny, while an RSA signature is as long as the key. Estimating sizes from the sshsig format (a header of about 115 bytes plus the armored signature), the packet comes out as follows:
- Ed25519: about 420 bytes, well within the limit;
- ECDSA P-256: about 510 bytes, still comfortable;
- RSA-4096: about 1700 bytes, over the limit and therefore unusable;
- RSA-2048 and 3072: about 990 and 1330 bytes, close to the MTU, with no headroom for VPNs, tunnels or PPPoE.
The practical choice is simple: use an Ed25519 key, which is also what is recommended for SSH today. These sizes are estimates derived from the signature format, not measurements on real packets, and SSH certificates, being larger, are likely not to fit either.
Known limitations
A security component deserves an honest presentation. These are the limitations documented in the README:
- without
--src-ipthe source IP is not signed; - behind a shared NAT the window opens for the public IP, so it applies to every client sharing it;
- key options such as
command=andrestrictare not evaluated at knock time, andAuthorizedKeysCommandand PAM are not mirrored; - client and server clocks must differ by less than 20 seconds;
- IPv6 extension headers are not handled;
- a flood can exhaust the signature-verification budget, delaying legitimate knocks;
- with firewalld the window lasts for the whole
open_seconds, and a reload closes all open windows.
Project status
Version 1.0.0 came out on 20 November 2026 and 1.1.0 the day after. The project is young, so feedback, bug reports and pull requests are welcome. The code, INSTALL.md and the full changelog are on GitHub.
Leave a Reply