When experimenting with firewall rules, it is essential to have a reliable way to revert to a known-good configuration. This article explains how to save the current iptables / ip6tables state, apply temporary changes, and restore the original rules if needed.
All commands shown below must be executed with root privileges.
The most reliable way to back up firewall rules is to use iptables-save and ip6tables-save. These commands dump the entire ruleset in a format suitable for restoration.
iptables-save > /root/iptables.bakip6tables-save > /root/ip6tables.bakOptionally, you can include a timestamp for traceability:
iptables-save > /root/iptables.bak.$(date +%F_%H%M%S)
ip6tables-save > /root/ip6tables.bak.$(date +%F_%H%M%S)Once the backup is complete, you can safely experiment with firewall rules.
Example: temporarily allow SSH traffic (TCP port 22) at the top of the INPUT chain.
iptables -I INPUT 1 -p tcp --dport 22 -j ACCEPT
ip6tables -I INPUT 1 -p tcp --dport 22 -j ACCEPTVerify the current ruleset:
iptables -S
ip6tables -STip:
When working over SSH, always start with non-disruptive changes (such as adding ACCEPT or LOG rules) to avoid locking yourself out.
If the changes do not behave as expected, you can restore the original configuration instantly using iptables-restore and ip6tables-restore.
iptables-restore < /root/iptables.bak
ip6tables-restore < /root/ip6tables.bakConfirm that the rules have been restored:
iptables-save
ip6tables-save-
iptables-nft backend On modern distributions,
iptablesmay be backed by nftables (iptables-nft). The save/restore commands still work correctly and will update the nftables ruleset internally. -
Firewall management services If services such as
firewalldorufware running, they may automatically overwrite manual changes. Check their status before testing:systemctl is-active firewalld systemctl is-active ufw
-
Fail-safe recovery For remote systems, consider scheduling an automatic rollback (e.g., via
atorsystemd-run) before applying risky rules. This ensures recovery even if network access is lost.
By using iptables-save and iptables-restore (and their IPv6 equivalents), you can safely experiment with firewall rules and always return to a stable baseline. This workflow is simple, fast, and highly recommended for both testing and troubleshooting scenarios.