Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Migrate iptables Firewall Rules to a New Server Safely

· 7 min read · Home Networking

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks you through moving iptables firewall rules from an existing server to a new one without accidentally locking yourself out or losing important filtering. You’ll export the current rules, adapt them for the new environment, and test them safely before making them permanent. The process works for both IPv4 (iptables) and IPv6 (ip6tables) rule sets.

What You'll Need

  • Root or sudo access on both the old and new servers.
  • SSH access to both servers (or console access to the new server as a fallback).
  • The same firewall toolset installed on the new server (usually iptables and ip6tables).
  • A text editor on the new server (e.g., nano or vi).
  • Knowledge of the new server’s network interfaces and IP addresses (they may differ from the old server).

Step-by-Step

  1. Review the current rules on the old server. Run sudo iptables -S and sudo ip6tables -S to see the active rules in a format that can be saved. Also run sudo iptables -L -v -n and sudo ip6tables -L -v -n to see packet counts and identify which rules are actually being used. This helps you spot dead rules you might want to drop.
  2. Export the rules to files. Use sudo iptables-save > /tmp/iptables.rules and sudo ip6tables-save > /tmp/ip6tables.rules. These files contain the full rule set in a format that iptables-restore can read.
  3. Copy the rule files to the new server. Use scp or any secure file transfer method. For example: scp /tmp/iptables.rules user@newserver:/tmp/. Do the same for the ip6tables file.
  4. Inspect the rules on the new server before applying. Open the files with a text editor. Look for any references to old IP addresses, interfaces (like eth0 vs ens3), or specific network ranges that may not exist on the new server. Update them to match the new server’s configuration. For instance, if the old server used 192.168.1.0/24 and the new one uses 10.0.0.0/24, replace the old subnet.
  5. Back up the new server’s current rules (if any). If the new server already has a firewall, save it: sudo iptables-save > /tmp/iptables.backup and sudo ip6tables-save > /tmp/ip6tables.backup. This gives you a restore point.
  6. Apply the rules temporarily. On the new server, run sudo iptables-restore < /tmp/iptables.rules and sudo ip6tables-restore < /tmp/ip6tables.rules. This loads the rules into the kernel but does not make them persistent. Do this over an SSH session, but be prepared to use the console if you get disconnected.
  7. Test connectivity and functionality. Open a new SSH session (or test from another machine) to confirm you can still connect. Check that services like web, DNS, or database are reachable as expected. Use sudo iptables -L -v -n to see if packets are being counted correctly.
  8. If the rules cause problems, revert immediately. If you lose SSH access or services break, restore the backup: sudo iptables-restore < /tmp/iptables.backup and sudo ip6tables-restore < /tmp/ip6tables.backup. If you’re locked out, use the console or a rescue mode to flush the rules with sudo iptables -F and sudo ip6tables -F.
  9. Make the rules persistent. Once you’re satisfied, install the appropriate persistence package. On Debian/Ubuntu, use sudo apt install iptables-persistent and then save with sudo netfilter-persistent save. On RHEL/CentOS, use sudo yum install iptables-services and then sudo systemctl enable iptables and sudo systemctl enable ip6tables, followed by sudo service iptables save and sudo service ip6tables save. Alternatively, you can add the restore commands to a startup script, but the package method is cleaner.
  10. Verify after reboot. Reboot the new server and confirm that the rules are active: sudo iptables -S and sudo ip6tables -S. Also verify that your services still work.

Common Mistakes

  • Not updating IP addresses and interfaces. The most common error is copying rules verbatim without adjusting for the new server’s network setup. This can block traffic to the wrong subnet or fail to filter the correct interface.
  • Applying rules without a backup. Always save the new server’s current rules first. If something goes wrong, you can quickly revert.
  • Forgetting IPv6. Many servers have IPv6 enabled, and if you only migrate IPv4 rules, you might leave IPv6 unprotected or accidentally block it. Always handle both iptables and ip6tables.
  • Making rules persistent before testing. If you enable persistence immediately and then reboot, you might find yourself locked out after a restart. Test thoroughly before saving permanently.
  • Ignoring rule order. iptables processes rules in order, so a misplaced ACCEPT or DROP can break everything. Review the order carefully, especially if you added new rules.

Tips

  • Use a rule file that is easy to read. Add comments to your rules (lines starting with #) to explain what each section does. This makes future edits much easier.
  • Test with a temporary rule that allows your IP. Before applying the full rule set, add a rule that explicitly accepts SSH from your current IP address. For example: sudo iptables -I INPUT -p tcp --dport 22 -s YOUR_IP -j ACCEPT. This ensures you won’t get locked out even if the imported rules have a flaw.
  • Use a cron job to auto-revert. As a safety net, you can set up a cron job that flushes the firewall after 5 minutes if you don’t cancel it. This is useful for remote servers where console access is limited.
  • Keep the old server’s rules as a reference. Don’t delete the old server’s rules until the new one has been running for a few days without issues.
  • Check for kernel modules. Some rules may require specific kernel modules (e.g., for NAT or conntrack). Ensure the new server has the same modules available, or the restore might fail.

Troubleshooting

  • You get “Permission denied” when running iptables. Make sure you’re using sudo and that your user has the necessary privileges. If you’re in a container, you may need extra capabilities.
  • iptables-restore fails with an error. The error message usually points to the line number. Open the file and check that line for syntax errors, missing modules, or invalid addresses. Common issues include using a hostname instead of an IP (iptables doesn’t resolve DNS) or specifying an interface that doesn’t exist.
  • You lose SSH access after applying rules. Use the console or out-of-band management to log in. Then flush the rules with sudo iptables -F and sudo ip6tables -F, and restore the backup. If you can’t get console access, you may need to reboot the server and interrupt the boot process to enter single-user mode.
  • Services are not accessible even though rules look correct. Check the packet counters with sudo iptables -L -v -n. If packets are hitting a DROP rule, you may have a rule ordering issue. Also check if the service is listening on the correct interface and port (use ss -tulpn).
  • Rules are not persistent after reboot. Ensure you installed the persistence package and saved the rules correctly. On systemd systems, check that the service is enabled: sudo systemctl is-enabled iptables. If not, enable it.
  • IPv6 rules are not being applied. Make sure the ip6tables service is enabled and that the kernel has IPv6 support. You can check with sysctl net.ipv6.conf.all.disable_ipv6 – it should be 0.

Frequently Asked Questions

Can I copy the rule files directly without editing?

You can, but it’s risky. If the new server has different IP addresses, interfaces, or network topology, the rules may block legitimate traffic or fail to protect the right interfaces. Always review and edit the files for your new environment.

What’s the difference between iptables-save and iptables -S?

iptables-save outputs the rules in a format that can be restored later with iptables-restore. iptables -S is a human-readable listing that is not directly restore-ready. For migration, use iptables-save.

Do I need to migrate both IPv4 and IPv6 rules?

Yes, if your server uses IPv6. Many servers have both stacks enabled, and ignoring IPv6 can leave a security gap or cause unexpected behavior. Always export and import both iptables and ip6tables rule sets.

How do I make the rules persistent on a systemd-based distro?

Install the appropriate package (e.g., iptables-persistent on Debian/Ubuntu, iptables-services on RHEL/CentOS), save the rules with the provided command, and enable the service. For example, on Debian: sudo netfilter-persistent save and sudo systemctl enable netfilter-persistent.

What if I accidentally lock myself out during migration?

Use the console or out-of-band management (like a virtual console from your hosting provider) to log in. Then flush all rules with sudo iptables -F and sudo ip6tables -F, and restore your backup. If you don’t have a backup, you can start with a clean slate and rebuild the rules.

Can I migrate rules from a server with a different kernel or distro?

Yes, as long as the new server supports iptables and has the necessary kernel modules. The rule syntax is standard, but you may need to adjust for differences in default policies or available matches (e.g., some newer features may not be supported on older kernels).