Marcio Cunha

How to replace iptables with nftables and speed up your network

Learn how to replace the legacy iptables with nftables on Linux to gain better network performance, clean syntax, and unified packet filtering.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • The nftables framework unifies network operations into a single modern kernel tool, replacing the old iptables ecosystem
  • Unified tables, chains, and rules eliminate the need to duplicate commands for both IPv4 and IPv6 network traffic
  • Native sets and map lists allow administrators to build dynamic, highly optimized rules for heavy traffic volumes
  • Migrating legacy scripts requires understanding the new declarative syntax oriented toward tree data structures
  • Validating rule persistence with systemd ensures your firewall remains intact after server reboots

Why nftables replaces legacy iptables on Linux

If you have managed Linux servers for a while, you have probably spent hours building complex rules in iptables, dealing with repetitive syntax for IPv4 and IPv6, and suffering from performance drops under heavy traffic. In practice, iptables suffers from deep architectural limitations because every rule change required recompiling and replacing the entire packet list in the kernel, creating processing bottlenecks. To solve this, the Linux community created nftables, a modern replacement that operates directly within the operating system kernel's netfilter subsystem with a unified and highly efficient approach.

When discussing network performance, every millisecond of routing and packet filtering latency matters. While iptables split its tools into separate packages for IPv4, IPv6, ARP, and network bridges, nftables centralizes everything into a single tool with a clean, modular syntax. Before diving into hands-on configuration, it is worth remembering that efficient infrastructure management goes hand in hand with keeping hardware optimized, something you can explore further by understanding how to calculate the real electrical power consumption of your homelab to guarantee complete operational stability.

Understanding the new nftables architecture and syntax

The great revolution of nftables lies in its internal structure, moving away from the old procedural model and adopting tables, chains, and rules based on tree data structures. In iptables, rules were evaluated linearly, meaning that if you had five hundred rules, every packet had to pass through all of them sequentially until finding a match. In nftables, we can create sets and maps that allow instant indexed searches, drastically reducing CPU usage under heavy load.

In practice, this means that blocking hundreds of malicious IP addresses is no longer a giant list of individual commands, but a single check against an optimized set residing in kernel memory. Furthermore, the syntax has become much closer to a friendly programming language, allowing complex logical operations without concatenating multiple commands or resorting to external tools like ipset. This configuration clarity reduces human errors and simplifies security auditing in corporate production environments.

Step-by-step to install and configure nftables

To get nftables running, the first step is to ensure the utility is installed on your Linux distribution and that the legacy firewall service is disabled to prevent port conflicts and duplicate rules. On modern Debian or Ubuntu-based distributions, installation is quick via the standard system package manager.

  1. Remove or disable competing firewall services by executing the stop command in your terminal:
    sudo systemctl stop ufw && sudo systemctl disable ufw
  2. Install the main nftables package using the system package manager:
    sudo apt update && sudo apt install nftables -y
  3. Enable and start the nftables service to ensure it loads during system boot:
    sudo systemctl enable nftables && sudo systemctl start nftables

With the service active, the next step is creating the initial configuration file that defines the default security policy for incoming, outgoing, and forwarding traffic. The primary configuration file is usually located in the system configuration directory, housing structured rules that define inet-type tables capable of handling both IPv4 and IPv6 traffic simultaneously without code redundancy.

Creating your first table and filtering rules

Configuring nftables always begins by declaring a table, which acts as a logical container for filtering chains. We use the inet family to cover both IPv4 and IPv6 networks in a unified manner, saving writing time and preventing inconsistencies in security policies. Inside this table, we create input, output, and forward chains associated with appropriate netfilter hooks.

table inet firewall {chain input {type filter hook input priority filter; policy drop;iif lo acceptcomment 'Accept all local loopback traffic'ct state established,related acceptcomment 'Allow already established connections'tcp dport 22 acceptcomment 'Allow SSH access'tcp dport 80 acceptcomment 'Allow HTTP traffic'tcp dport 443 acceptcomment 'Allow HTTPS traffic'}}

This configuration block defines a restrictive default policy (policy drop), meaning everything not explicitly authorized is dropped by the firewall. Next, we open safe exceptions for the loopback interface (essential for internal service communication on the machine itself), allow network connections established previously, and open vital ports for web services and remote SSH access, ensuring you do not lose control of the server during the process.

Managing dynamic sets and rule persistence is equally straightforward. One of the greatest operational differentials of nftables is the ease with which we can manage dynamic lists of blocked or allowed IPs without restarting the entire service. We can create named sets that store multiple addresses and query them directly in filtering rules, vastly simplifying the automation of intrusion prevention systems or custom monitoring scripts.

Final considerations on migrating to nftables

The transition from iptables to nftables represents an undeniable evolutionary leap in Linux system administration, offering a cleaner architecture, superior performance on high-speed networks, and native unification between IPv4 and IPv6. Although it requires an initial learning curve due to syntax changes, the gain in code readability and flexibility amply rewards the migration effort. By adopting native sets and well-structured restrictive policies, you build a robust security barrier prepared for modern infrastructure and cloud computing challenges.