For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /general/ssh_security/how_to_secure_ssh_service_with_port_knocking.md.

How to Secure SSH Service with Port Knocking on Ubuntu/Debian and CentOS

Port knocking is a security technique that keeps the SSH port closed until a predefined sequence of "knocks" (specific port requests) is detected. The SSH port opens for the client only after the correct sequence is sent, which makes it harder for attackers to find the port.

Warning

Make sure you have console or out-of-band access to the server before configuring port knocking. Incorrect firewall or knockd rules can block your SSH access. Keep your current SSH session open until you have confirmed that the knock sequence works.

Set Up the Server

Install knockd

On Ubuntu / Debian, install the knockd package from the default repository:

apt-get install knockd -y

On CentOS, enable the EPEL repository first, then install knockd:

yum install epel-release -y
yum install knockd -y

Edit the knockd configuration

Open the knockd.conf configuration file:

nano /etc/knockd.conf

Replace the default port sequences with your own custom sequences. The first opens the SSH port and the second (the reverse) closes it:

sequence = 7777,8888,9999
sequence = 9999,8888,7777

Modify the line that opens port 22:

command = /sbin/iptables -I INPUT -s %IP% -p tcp --dport 22 -j ACCEPT

Save and close the file.

Enable the knockd service

Edit the file that enables the service, and change the lines shown.

On Ubuntu / Debian, edit /etc/default/knockd:

nano /etc/default/knockd
START_KNOCKD=1
KNOCKD_OPTS="-i eth0"

On CentOS, edit /etc/sysconfig/knockd:

nano /etc/sysconfig/knockd
START_KNOCKD=YES
KNOCKD_OPTS="-i eth0"

Save and close the file.

Start knockd

Restart and enable the knockd service:

systemctl restart knockd
systemctl enable knockd

Check the status to make sure it is running:

systemctl status knockd

Install iptables

On Ubuntu / Debian, install iptables and the persistent module:

apt-get install iptables iptables-persistent -y

On CentOS, install iptables and iptables-services:

yum install iptables iptables-services -y

Block SSH port 22

Block SSH port 22 for all incoming connections, while keeping already established connections open:

iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j REJECT

Save the iptables rules

On Ubuntu / Debian, save the rules and reload them:

netfilter-persistent save
netfilter-persistent reload

On CentOS, save the rules and restart iptables:

service iptables save
systemctl restart iptables

Check the Setup from a Client

Check that port 22 is filtered

From the client machine, use nmap to verify that port 22 is filtered:

nmap your-server-ip

You should see:

PORT   STATE    SERVICE
22/tcp filtered ssh

Test an SSH connection

Try to connect via SSH:

ssh root@your-server-ip

You should receive:

ssh: connect to host your-server-ip port 22: Connection refused

Open and Close the SSH Port with a Knock

Replace your-server-ip with the actual IP address of your server, and use the same knock sequences you configured on the server.

Install the knock client

Install knockd on your local machine, which includes the knock client.

On Ubuntu / Debian:

apt-get install knockd -y

On CentOS:

yum install knockd -y

Send the knock sequence

Send the configured knock sequence to open the SSH port on the server:

knock -v your-server-ip 7777 8888 9999

Connect over SSH

Once the correct sequence is received, the SSH port opens and you can connect:

ssh root@your-server-ip

Close the SSH port

After you have finished your task, send the reverse knock sequence to close port 22:

knock -v your-server-ip 9999 8888 7777

Verify that the reverse sequence closes SSH port 22 before you end your session.