The Wild

How to Migrate from Firewalld to Iptables on CentOS

· 4 min read · Mammals

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

Migrating from firewalld to iptables on CentOS provides direct, granular control over packet filtering rules without the abstraction layer of dynamic zones and services. This process is typically undertaken by administrators who require legacy compatibility, specific rule ordering, or deeper debugging capabilities that firewalld’s high-level interface may obscure. Before beginning, ensure you have root or sudo privileges and a stable network connection, as misconfiguring the firewall can result in immediate loss of remote access.

What You'll Need

  • Root or sudo access to the CentOS system
  • A terminal emulator or SSH session with active connectivity
  • Backup copies of current firewalld configurations (optional but recommended)
  • Familiarity with basic iptables syntax and rule management

Step-by-Step

  1. Stop the firewalld service: Begin by halting the active firewall daemon to prevent conflicts. Run sudo systemctl stop firewalld. Verify it is stopped using systemctl status firewalld.
  2. Disable firewalld at boot: Prevent the service from restarting automatically after a reboot. Execute sudo systemctl disable firewalld.
  3. Install iptables-services: CentOS 7 and later do not include full iptables management tools by default. Install the package using sudo yum install iptables-services. This provides the necessary scripts to save and load rules persistently.
  4. Flush existing rules: Clear any residual rules that might interfere. Use sudo iptables -F for the filter table, sudo iptables -t nat -F for NAT, and sudo iptables -t mangle -F for mangling. Also reset counters with -Z if desired.
  5. Create your new rules: Define your inbound and outbound policies. For example, allow established connections with sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT. Open specific ports like SSH with sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT.
  6. Set default policies: Decide on the default behavior for chains. To drop all incoming traffic by default, run sudo iptables -P INPUT DROP. Ensure you allow loopback traffic (-i lo) to maintain local service communication.
  7. Save the rules: Unlike firewalld, iptables does not save changes automatically. Save your current configuration to /etc/sysconfig/iptables by running sudo service iptables save or sudo /usr/libexec/iptables/iptables.init save.
  8. Enable and start iptables: Activate the service to start immediately and enable it for future boots. Run sudo systemctl start iptables followed by sudo systemctl enable iptables.
  9. Verify functionality: Check the status with sudo systemctl status iptables and test connectivity. If you are connected via SSH, ensure port 22 is open before disabling firewalld completely to avoid locking yourself out.

Common Mistakes

One frequent error is failing to save rules before stopping firewalld. Since iptables operates in memory, any unsaved changes vanish upon service restart or reboot. Another common pitfall is setting a default DROP policy on the INPUT chain without first allowing essential services like SSH or DNS resolution. This results in an immediate disconnect. Administrators also often overlook the need to flush rules from other tables (NAT, Mangle) when switching contexts, leading to unexpected packet handling behaviors. Finally, assuming that installing the package alone configures the firewall is incorrect; manual rule definition and saving are required steps.

Tips

Always test rules in a non-production environment first. When writing rules, order matters significantly; iptables processes rules top-to-bottom and stops at the first match. Place specific allow rules before general deny rules. Use comments within your scripts to document why certain rules exist, as raw iptables output lacks descriptive context. Consider using ip6tables for IPv6 traffic if your system supports dual-stack networking, ensuring both protocols are secured equally. Keep a backup of your working iptables file in a safe location outside the server, such as a version control system or external drive, to facilitate rapid recovery.

Troubleshooting

If you lose SSH access after making changes, you will need physical console access or KVM/IPMI to log in directly. Once logged in, stop the iptables service with sudo systemctl stop iptables to restore connectivity, then review your rules. If the service fails to start, check logs at /var/log/messages or /var/log/audit/audit.log for syntax errors. Ensure that the iptables-services package is correctly installed and that no other firewall modules are conflicting. If rules appear to be ignored, verify that the target chain policies are set correctly and that no earlier rule is dropping the packets prematurely. Restarting the network service sometimes helps reload interface-specific rules.

Frequently Asked Questions

Is it safe to switch from firewalld to iptables?

Yes, provided you carefully configure your rules to allow essential traffic, particularly SSH, before applying restrictive default policies. Always have an alternative access method available during the transition.

Do I need to uninstall firewalld?

No, you only need to stop and disable it. Uninstalling is optional and generally unnecessary unless you want to free up space or remove potential confusion between two active firewall managers.

Will my existing firewalld rules transfer automatically?

No. firewalld uses XML-based zone configurations, while iptables uses flat text files. You must manually recreate equivalent rules in iptables format.

How do I view current iptables rules?

Use the command sudo iptables -L -n -v to list all rules in the filter table with numeric addresses and packet counts for detailed inspection.

Can I use nftables instead?

CentOS 8 and newer support nftables as the successor to iptables. However, iptables remains fully supported and compatible via a translation layer. The steps above apply specifically to iptables.

Why did my internet stop working after migration?

This usually happens because the default policy was set to DROP without allowing outgoing traffic or established connections. Add rules to accept OUTPUT traffic and related/established inbound packets to resolve this.