Connect via SSH and Secure the Server with iptables

in this page i am implementation how to make ssh connection to server where severer have stick rule of iptable


Drop All Traffic by Default

These commands set the default policy for all chains to DROP, effectively blocking all incoming, outgoing, and forwarded packets:

sudo iptables -P INPUT DROP
sudo iptables -P OUTPUT DROP
sudo iptables -P FORWARD DROP


Allow SSH Traffic

Make sure you don’t lock yourself out! Allow SSH (default port 22):

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT


Connect using SSH
Open your terminal and run:


to Block specific ip
If you want to block SSH access from another machine (e.g., 192.168.0.102), use:

sudo iptables -A INPUT -p tcp --dport 22 -s 192.168.0.102 -j DROP

Updated on