ThisCalculator

Everyday

Port Knocking to Hide SSH Daemon: Setup Guide

Updated · Standard formula, shown below

Port knocking hides your SSH daemon from port scanners and casual attackers by making it ignore connection attempts until a specific sequence of ports is “knocked” in order. This guide walks through setting up port knocking on a Linux server using knockd, so SSH only responds after the correct knock sequence is sent. You’ll need root access to your server and the ability to edit system files.

What You’ll Need

  • A Linux server (Debian/Ubuntu, CentOS/RHEL, or similar) with SSH already working.
  • Root or sudo access to install packages and edit configuration files.
  • A client machine (your laptop) from which you’ll knock and connect. It should have knock client software or you can use nc (netcat) or nmap.
  • Basic familiarity with the command line and editing files with nano or vi.

Step-by-Step

  1. Install knockd on the server. On Debian/Ubuntu: sudo apt update && sudo apt install knockd. On CentOS/RHEL: sudo yum install knock (the package may be named knockd on some versions).
  2. Decide on a knock sequence. Choose three or more ports that are unlikely to be used by other services. For example: 7000, 8000, 9000. Avoid using ports below 1024 unless you run as root (knockd can bind to them, but it’s simpler to use high ports).
  3. Edit the knockd configuration file. On Debian/Ubuntu, edit /etc/knockd.conf. On CentOS/RHEL, it’s usually /etc/knockd.conf as well. Replace the contents with something like:
    [options]
        UseSyslog
    
    [openSSH]
        sequence = 7000,8000,9000
        seq_timeout = 5
        command = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
        tcpflags = syn
    
    [closeSSH]
        sequence = 9000,8000,7000
        seq_timeout = 5
        command = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
        tcpflags = syn
        

    Adjust the sequence and timeout as needed. The %IP% variable is replaced with the knocking client’s IP address.

  4. Ensure iptables is the firewall. If you’re using ufw or firewalld, you’ll need to either disable them or adapt the commands. For simplicity, this guide assumes iptables is in use. If you have ufw, you can use ufw allow from %IP% to any port 22 in the command, but you’ll need to adjust the command accordingly.
  5. Set the default policy for SSH to DROP. Before starting knockd, you want SSH to be closed by default. Add a rule to drop incoming SSH connections: sudo iptables -A INPUT -p tcp --dport 22 -j DROP. This will block all new SSH connections until a knock opens the port for your IP.
  6. Start knockd. On Debian/Ubuntu, edit /etc/default/knockd and set START_KNOCKD=1. Then start the service: sudo systemctl start knockd. On CentOS/RHEL, you may need to enable the service: sudo systemctl enable knockd and then sudo systemctl start knockd.
  7. Test from your client machine. From your laptop, install a knock client if needed. On Debian/Ubuntu: sudo apt install knockd (the client is included). On macOS, use brew install knock. Then send the knock sequence: knock server_ip 7000 8000 9000. Immediately after, try to SSH: ssh user@server_ip. It should connect.
  8. Close the port after use. To close SSH for your IP again, send the reverse sequence: knock server_ip 9000 8000 7000. This triggers the close rule, removing the iptables ACCEPT rule.
  9. Make knockd start on boot. Enable the service: sudo systemctl enable knockd. Also ensure your iptables DROP rule is persistent. Use iptables-save and restore on boot, or use a tool like iptables-persistent.

Common Mistakes

  • Using the wrong sequence order. The close sequence must be the exact reverse of the open sequence, or it won’t work.
  • Not setting the DROP rule before testing. If SSH is still open by default, knocking does nothing.
  • Firewall conflicts. If you have ufw or firewalld active, iptables rules may be overridden or ignored. Make sure you’re using only one firewall management tool.
  • Typo in the iptables command. A missing -A or wrong port can silently fail. Test the iptables command manually before putting it in knockd.
  • Forgetting to enable knockd in the default file. On Debian, START_KNOCKD=0 prevents the service from starting.

Tips

  • Use a longer sequence (4-5 ports) for better security, but keep it memorable.
  • Change the sequence periodically, especially if you suspect it’s been discovered.
  • Combine port knocking with SSH key authentication instead of passwords for stronger security.
  • Use seq_timeout of 5-10 seconds to allow for human typing but not too long for automated scans.
  • Consider using a single knock that both opens and closes after a delay, but the two-sequence method is simpler.
  • Log knock attempts to see if anyone is probing your ports.

Troubleshooting

Knock doesn’t open the port. Check that knockd is running: sudo systemctl status knockd. Look at logs: sudo tail -f /var/log/syslog or journalctl -u knockd. Verify the sequence and timeout. Test the iptables command manually: sudo iptables -L INPUT -n to see if the rule was added.

SSH still doesn’t connect after knocking. Ensure the client sends the knock from the same IP you intend to SSH from. If you’re behind NAT, that’s your public IP. Also check that the iptables ACCEPT rule is before any DROP rule. If you have a DROP rule that matches first, it will block.

knockd fails to start. Check the config file syntax. On Debian, the default file might have START_KNOCKD=0. Also ensure the command paths are correct (e.g., /sbin/iptables).

Port knocking works but SSH closes immediately. The close sequence might be triggered accidentally, or the seq_timeout is too short. Increase the timeout or use a more complex sequence.

Client can’t send knocks. Ensure the knock client is installed and the ports are reachable (no firewall blocking them). Use nc -z server_ip port to test each port individually.

Frequently Asked Questions

Is port knocking secure?

It adds a layer of obscurity, but it’s not foolproof. Attackers can sniff network traffic to learn the sequence, so it should be combined with other security measures like SSH keys and fail2ban.

Can I use UDP ports for knocking?

Yes, but TCP is more common and easier to test. If you use UDP, you need to set tcpflags to 0 or omit it, and the client must send UDP packets.

What if I lock myself out?

If you lose the ability to knock, you’ll need to access the server via a console (like a VPS provider’s web terminal) to disable knockd or add an iptables rule to allow your IP.

Does port knocking work with IPv6?

Yes, but you need to adjust iptables commands to use ip6tables and ensure knockd supports IPv6. Some versions may not, so test carefully.

Can I knock from my phone?

Yes, there are mobile apps that send knock sequences, or you can use a terminal app with netcat.

How do I change the knock sequence?

Edit /etc/knockd.conf, change the sequence numbers, and restart knockd. Remember to update your client.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.