Curious Minds

Clear answers to science and history questions

Iptables Delete Rule: How to Remove Firewall Rules

· 5 min read · Science

To delete an iptables rule, you first list the current rules with line numbers using iptables -L --line-numbers, then run iptables -D INPUT <line-number> to remove it. You can also delete by specifying the exact rule specification with iptables -D INPUT -s 192.168.1.1 -j DROP. This article explains both methods, shows examples, and covers common pitfalls like persistence and flushing.

Background: What iptables Rules Are

iptables is the classic Linux firewall tool that filters network packets by examining headers like source IP, destination port, and protocol. Rules are organized into chains—INPUT for incoming packets, OUTPUT for outgoing, FORWARD for routed traffic—and each rule contains a set of match conditions and a target (like ACCEPT, DROP, or REJECT). Deleting a rule is a routine maintenance task: you might need to remove a temporary block, correct a typo, or clean up after testing. The command syntax is consistent across most Linux distributions, though the underlying packet filtering engine (netfilter) is the same.

How to List Rules with Line Numbers

Before deleting, you must identify the rule. The safest way is to display rules with line numbers:

  • iptables -L INPUT --line-numbers shows the INPUT chain with numbers.
  • iptables -L OUTPUT --line-numbers for the OUTPUT chain.
  • iptables -L FORWARD --line-numbers for FORWARD.
  • iptables -L --line-numbers -n adds -n to show IP addresses and ports numerically (faster, no DNS resolution).

Example output:

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  203.0.113.5          0.0.0.0/0
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0  tcp dpt:22

The number in the num column is what you use with -D.

Deleting by Line Number

The simplest deletion method uses the rule’s position in the chain:

  1. List rules with line numbers as above.
  2. Run iptables -D INPUT 1 to delete rule number 1 from the INPUT chain.
  3. Verify with iptables -L INPUT --line-numbers.

This deletes the rule at that exact position. Be careful: after deletion, subsequent rules shift up by one, so if you need to delete multiple rules, either re-list each time or delete from the bottom up.

Deleting by Rule Specification

Instead of a line number, you can specify the rule exactly as it appears. For example, if you added a rule with iptables -A INPUT -s 203.0.113.5 -j DROP, you can delete it with:

iptables -D INPUT -s 203.0.113.5 -j DROP

This is safer when rules may have moved, but the specification must match exactly—including protocol, interface, ports, and target. Use iptables -S to see the full rule in a format you can copy directly. For instance, iptables -S INPUT prints rules like -A INPUT -s 203.0.113.5/32 -j DROP; you can then use that exact string after -D.

Flushing and Deleting All Rules

To remove all rules in a chain, use iptables -F INPUT (flush). To flush all chains, iptables -F. This does not change the default policy (ACCEPT or DROP). To delete all rules and reset the default policy to ACCEPT, run iptables -P INPUT ACCEPT and then iptables -F. Be extremely careful: flushing a chain with a DROP policy will immediately block all traffic matching that chain until you add new rules.

Common Problems and How to Avoid Them

  • Rule not found: If you get “Bad rule (does a matching rule exist in that chain?)”, your specification doesn’t match exactly. Check with iptables -S and copy the exact rule.
  • Changes lost after reboot: iptables rules are not persistent by default. Use iptables-save > /etc/iptables/rules.v4 (Debian/Ubuntu) or iptables-save > /etc/sysconfig/iptables (RHEL/CentOS) and enable the appropriate service, or use iptables-persistent package.
  • Deleting from the wrong chain: Always specify the chain name. iptables -D INPUT 3 is not the same as iptables -D OUTPUT 3.
  • Numbering shifts: If you delete rule 2, the old rule 3 becomes rule 2. Re-list if you are deleting multiple.

Alternatives: nftables and firewalld

Modern Linux systems often use nftables as a successor to iptables, with the nft command. The syntax differs: nft list ruleset shows rules, and deletion uses handles (e.g., nft delete rule ip filter INPUT handle 3). firewalld, common on RHEL-based distributions, provides a higher-level interface with zones and services; you typically remove rules via firewall-cmd --permanent --remove-rich-rule=.... However, iptables remains widely used and is still the default on many systems, so knowing both methods is useful.

Safety and Best Practices

  • Always list rules before deleting to confirm you are removing the intended one.
  • Test firewall changes in a safe environment or over a connection that won’t lock you out (e.g., a local console or out-of-band management).
  • If you manage a remote server, avoid deleting rules that allow your SSH connection. If you do, you may lose access.
  • Keep a backup of your current rules with iptables-save > backup.txt before making changes.
  • After deleting, verify the result with iptables -L and test the expected traffic.

Frequently Asked Questions

How do I delete a rule without line numbers?

Use the rule specification method: iptables -D INPUT -s 192.168.1.1 -j DROP. You must include all match options exactly as the rule was added. Use iptables -S to see the full rule syntax.

Why does my iptables rule come back after reboot?

iptables rules are stored in memory only. To persist, save them with iptables-save to a file and restore at boot, or install the iptables-persistent package which handles this automatically on Debian/Ubuntu.

What is the difference between -D and -F?

-D deletes a specific rule (by number or specification), while -F flushes (removes) all rules in a chain or all chains. Flushing is more drastic and can leave you unprotected.

Can I delete a rule by its comment?

No, iptables does not support deleting by comment directly. You must use the line number or full rule specification. Comments are only for your reference in the rule listing.

How do I delete a rule from a specific table like NAT?

Use iptables -t nat -D PREROUTING 1 to delete from the NAT table’s PREROUTING chain. Remember to specify the table with -t; otherwise it defaults to the filter table.

What if I accidentally delete the wrong rule?

If you have a backup (e.g., iptables-save > backup), restore it with iptables-restore < backup. Otherwise, you can re-add the rule manually if you know its specification. There is no undo command.

In this article
  1. Background: What iptables Rules Are
  2. How to List Rules with Line Numbers
  3. Deleting by Line Number
  4. Deleting by Rule Specification
  5. Flushing and Deleting All Rules
  6. Common Problems and How to Avoid Them
  7. Alternatives: nftables and firewalld
  8. Safety and Best Practices
  9. Frequently Asked Questions