How-to

Delete an iptables Rule Without Losing SSH

There are two ways to delete an iptables rule: by its position in the chain or by repeating the exact rule with -D. Both are safe if you check the list first.

How do I see the rules with their numbers?

List the chain with line numbers before touching anything. Numbers are per chain and they shift after every delete, so always list again before a second change.

iptables -L INPUT -n -v --line-numbers
iptables -t nat -L PREROUTING -n --line-numbers

-n skips DNS lookups so the list appears instantly, and -v adds packet and byte counters plus interface names. The counters are useful: a rule that has matched zero packets for a week is often the one you can drop. Filter is the default table; NAT, mangle and raw rules only show when you name the table with -t.

How do I delete a rule by its number?

iptables -D INPUT 4

That removes the fourth rule in the INPUT chain of the filter table. For NAT, add the table: iptables -t nat -D PREROUTING 2. Deleting by number is quick when the list is short. In long chains it is easy to read the wrong row, so compare the counters and the target of the rule you mean before pressing Enter.

How do I delete a rule by its specification?

Repeat the exact rule you added, but replace -A or -I with -D:

# the rule was added with:
iptables -A INPUT -p tcp -s 198.51.100.0/24 --dport 5432 -j ACCEPT
# delete it with:
iptables -D INPUT -p tcp -s 198.51.100.0/24 --dport 5432 -j ACCEPT

The match must be exact in content, though not in option order. A handy way to get the precise text is iptables -S INPUT, which prints each rule in the same form you would type it. Copy the line, swap -A for -D and run it. If two identical rules exist, each -D removes only the first one, so run it again for the duplicate.

To check whether a rule exists without changing anything, use -C with the same specification. It exits 0 when the rule is present, which makes scripts idempotent: iptables -C INPUT ... 2>/dev/null || iptables -A INPUT ....

How do I clear a whole chain without locking myself out?

Flushing INPUT while its default policy is DROP cuts your own SSH session immediately. Set the policy to ACCEPT first, then flush:

iptables -P INPUT ACCEPT
iptables -F INPUT
iptables -t nat -F
iptables -X fw-temp     # delete an empty user chain

-F empties a chain, -X deletes a user-defined chain once nothing jumps to it and it is empty, and -Z zeroes the counters if you only want fresh statistics. Our machines have no out-of-band console, so a lockout is recovered through the panel: a reboot clears rules that were never saved, while a saved bad rule set means a reinstall. That is reason enough to be careful.

Is there a safety net for remote changes?

Yes. Schedule an undo before you start, and cancel it once you confirm you still have access:

iptables-save > /root/fw-before.rules
( sleep 300 && iptables-restore < /root/fw-before.rules ) &
# make changes, open a second SSH session to test, then:
kill %1

If the new rules cut you off, the old set comes back in five minutes. Debian and Ubuntu also offer iptables-apply, which asks you to confirm from a live session and rolls back when nobody answers.

How do I make the deletion permanent?

Changes made with the command live in kernel memory only. Write them to disk with the tool your distribution uses:

DistributionSave commandFile loaded at boot
Ubuntu 24.04 / 22.04, Debian 13 / 12netfilter-persistent save/etc/iptables/rules.v4
Rocky Linux 9, AlmaLinux 9 with iptables-servicesiptables-save > /etc/sysconfig/iptables/etc/sysconfig/iptables

Install the Debian helper with apt install iptables-persistent. On the Enterprise Linux family, the usual default is firewalld; if you manage rules with firewalld, remove them with firewall-cmd --permanent --remove-... instead, because a firewalld reload rewrites the tables and undoes hand edits. See Stop firewalld on CentOS Stream for how the two interact.

Am I editing iptables or nftables?

On current Ubuntu, Debian and Enterprise Linux releases, the iptables command is usually the iptables-nft front end, which stores rules in nftables. Run iptables -V: output ending in (nf_tables) confirms it. Deleting through iptables works fine in that mode, but rules created directly with nft do not show up in iptables -L. Check nft list ruleset when a rule seems to exist but cannot be found.

For a full walk through the tables, chains and targets, read iptables basics. If you are cleaning up after a forwarding setup, iptables port forwarding shows which NAT and FORWARD rules belong together, so you remove both halves.

Budget Machines for Firewall Practice

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
E1Intel Xeon E3-1240 v34 / 816 GB DDR32x 240 GB SSD2x 1 Gbps$30.52$91.56 (3 months)
S2Intel Xeon E-2356G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$58.39$58.39 (1 month)
S5Intel Xeon E-2276G6 / 12128 GB DDR42x 1 TB NVMe2x 10 Gbps$88.72$88.72 (1 month)
D12x Intel Xeon Gold 5315Y16 / 32128 GB DDR42x 1 TB NVMe2x 25 Gbps$202.94$202.94 (1 month)

Keep Reading

iptables basics

How iptables tables, chains and targets fit together, plus a minimal default-deny rule set for a new bare metal server.

Open

iptables port forwarding

Forward a public port to a VM, container or remote host with DNAT, enable routing, and keep replies flowing back the right way.

Open

Set up fail2ban

Install fail2ban, write a jail.local for SSH and nginx, read the ban list and unban an address you locked out by mistake.

Open

Harden SSH on Linux

A practical hardening list for sshd on a fresh dedicated box: keys, users, rate limits, firewall scope and an outside audit.

Open

FAQ

Quick Answers

List the chain with --line-numbers, then run iptables -D CHAIN N with the number you saw.

It was still in the saved rules file. Save again with netfilter-persistent save or iptables-save to the file your system loads.

Set the chain policy to ACCEPT, then run iptables -F. Do it in that order on a remote machine, or you lose your session.

Delete the DNAT rule with iptables -t nat -D and the matching FORWARD accept rule in the filter table.

Reboot from the panel to clear unsaved rules. If the bad set was saved, reinstall the image from the panel and restore your data from backup.