How to Set Up Port Knocking for SSH Security

Port knocking adds an extra layer of obscurity to SSH access — the SSH port stays closed to everyone until a specific sequence of connection attempts ("knocks") on other ports is received, at which point it opens briefly for the knocking IP.

What Port Knocking Adds (and Doesn't)

Port knocking is a form of security through obscurity — it doesn't replace strong authentication (SSH keys, disabled password auth) but adds a layer that hides SSH from casual port scans and automated brute-force attempts targeting the open port directly.

Prerequisites

  • SSH already secured with key-based authentication (see How to Secure SSH: Key-Based Authentication & Disabling Root Login)
  • UFW or iptables configured

Step 1 — Install knockd

sudo apt install knockd -y

Step 2 — Configure the Knock Sequence

sudo nano /etc/knockd.conf
[options]
    UseSyslog

[openSSH]
    sequence    = 7000,8000,9000
    seq_timeout = 5
    command     = /usr/sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
    tcpflags    = syn

[closeSSH]
    sequence    = 9000,8000,7000
    seq_timeout = 5
    command     = /usr/sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
    tcpflags    = syn

Choose your own unique port sequence rather than these example values.

Step 3 — Block SSH by Default

sudo ufw deny 22/tcp

SSH is now closed to everyone until the correct knock sequence is received.

Step 4 — Configure knockd to Start on Boot

sudo nano /etc/default/knockd
START_KNOCKD=1
KNOCKD_OPTS="-i eth0"
sudo systemctl enable --now knockd

Step 5 — Perform a Knock from the Client

sudo apt install knockd -y  # provides the client-side knock command
knock YOUR_SERVER_IP 7000 8000 9000
ssh user@YOUR_SERVER_IP

Step 6 — Close the Port After Use (Optional)

knock YOUR_SERVER_IP 9000 8000 7000

Important Caution: Maintain Alternative Access

Before fully relying on port knocking, ensure you have your hosting provider's console/KVM access available as a fallback — a misconfiguration in the knock sequence or firewall rule could lock you out entirely.

Limitations to Understand

  • The knock sequence itself could theoretically be observed if someone captures your traffic (though this is a fairly narrow attack window)
  • Not a substitute for strong authentication — combine with, never replace, SSH key-based auth
  • Adds friction to your own legitimate access, a trade-off against the obscurity benefit

Common Errors

Knock doesn't open the port — verify the sequence, timing (seq_timeout), and that knockd is actually running and monitoring the correct interface.

Locked out after enabling — use your provider's console access to review and correct the configuration; this is exactly why maintaining console access before relying on port knocking matters.

Best Practices

  • Always verify console/KVM access works before blocking SSH by default
  • Use port knocking as a supplementary layer, never as your only security measure
  • Document your knock sequence securely — losing it means losing convenient access

Continue Reading

Browse more articles in Advanced Networking & VPN.

  • port knocking, ssh security, knockd, hidden ssh port
  • 0 Los Usuarios han Encontrado Esto Útil
¿Fue útil la respuesta?

Artículos Relacionados

How to Set Up a VPN Server with WireGuard

WireGuard is a modern, fast, and simple VPN protocol — significantly easier to configure...

How to Set Up an OpenVPN Server on a VPS

OpenVPN is a mature, widely-supported VPN protocol — a solid choice when you need broad...

How to Configure a VPS as a Forward Proxy with Squid

A forward proxy routes outbound requests through your VPS, useful for accessing geo-restricted...

How to Set Up IPv6 on Your VPS

IPv6 adoption continues to grow, and many VPS providers now offer IPv6 addresses alongside IPv4....

How to Bond Multiple Network Interfaces for Redundancy

Network interface bonding combines multiple physical/virtual network interfaces into a single...