FG
💻 Software

How can I check if an iptables rule already exists?

Fresh7 days ago
Mar 15, 2026105293 views
Confidence Score1%
1%

Problem

I need to add a rule to iptables to block connections to a tcp port from the Internet. Since my script may be called multiple times and there is not a script to delete the rule, I want to check if an iptables rule already exists before inserting it - otherwise there will be a lot of dup rules in the INPUT chain. How can I check if an iptables rule already exists?

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: How can I check if an iptables rule already exists?

Low Risk

There is a new option in recent iptables versions. For older iptables versions, I would use Garrett suggestion :

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment