Iptables - Rule to Block All Other Incoming Traffic Except Ssh

I currently have the following rules to allow connections to my SSH server on port 2233:

iptables -A INPUT -p tcp --dport 2233 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 2233 -m conntrack --ctstate ESTABLISHED -j ACCEPT

However, I now need to write a rule that blocks all other incoming traffic apart from the incoming traffic to the SSH on port 2233. I also need to enable logging for all other blocked traffic to be labelled as "BLOCKED TRAFFIC >"

Any help would be greatly appreciated. Thanks!

3

1 Answer

Just append this:

iptables -A INPUT -j LOG --log-prefix "BLOCKED TRAFFIC > " --log-level 4
iptables -A INPUT -j DROP

Packets not matched by previous ACCEPTs (or DROPs) will be processed and so get LOGged and then DROPped...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest