A firewall's active rules can be lost during maintenance even when the server's files remain intact. An iptables backup preserves the running ruleset so its chain policies and rules can be restored after an unwanted change.

iptables-save exports IPv4 rules in the format that iptables-restore reads back into the kernel. A backup belongs to the host and compatible iptables backend it came from; it does not capture connection-tracking state or native nftables rules managed outside iptables.

Restoring a backup can remove newer access rules and disconnect remote administration. A working console or out-of-band connection provides recovery if SSH stops working, while saving rules for automatic loading after reboot remains a separate task.

Steps to back up and restore iptables rules:

  1. Inspect the current IPv4 filter policies and rules.
    $ sudo iptables -S
    -P INPUT ACCEPT
    -P FORWARD ACCEPT
    -P OUTPUT ACCEPT
    -A INPUT -p tcp -m tcp --dport 2222 -j ACCEPT

    The port 2222 rule shown is an existing rule on this host; your policy will differ. The save command also captures other available tables, such as nat.
    Related: How to check the active iptables backend

  2. Create a root-only directory for firewall backups.
    $ sudo install -d -m 700 /root/firewall-backups

    The 700 directory permissions protect rule comments and addresses from other users.

  3. Save the active ruleset to an unused backup filename.
    $ sudo iptables-save -f /root/firewall-backups/iptables-before.rules

    An existing file at this path would be overwritten. On hosts with IPv6 rules, ip6tables-save and ip6tables-restore handle a separate IPv6 backup.

  4. Read the saved backup before relying on it for recovery.
    $ sudo cat /root/firewall-backups/iptables-before.rules
    # Generated by iptables-save v1.8.10 (nf_tables) on Mon Nov  9 07:19:13 2026
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    -A INPUT -p tcp -m tcp --dport 2222 -j ACCEPT
    COMMIT
    # Completed on Mon Nov  9 07:19:13 2026

    The file should contain the expected tables, chain policies, rules, and a COMMIT for each table. An empty file is not a usable backup.

  5. Test whether the backup can be loaded without applying it.
    $ sudo iptables-restore --test /root/firewall-backups/iptables-before.rules

    No output means the ruleset was parsed and constructed without committing it. This does not prove that the restored policy will permit SSH or other required traffic.

  6. Restore the backup when the saved rules are needed.

    Restoring replaces the contents of the tables present in the backup, including rules added since it was saved. Do not run this over your only SSH connection; a working console or out-of-band connection is needed if access is lost.

    $ sudo iptables-restore /root/firewall-backups/iptables-before.rules
  7. Compare the restored policies and rules with the saved backup.
    $ sudo iptables-save
    # Generated by iptables-save v1.8.10 (nf_tables) on Mon Nov  9 07:19:13 2026
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    -A INPUT -p tcp -m tcp --dport 2222 -j ACCEPT
    COMMIT
    # Completed on Mon Nov  9 07:19:13 2026

    The tables, chain policies, and ordered rules should match the backup, including the 2222 rule shown here. Generated timestamps and packet counters can differ. On a remote server, a new management connection must also succeed before the recovery console is closed.
    Related: How to list iptables rules with counters

Share this guide