Chapter 21. Migrating from iptables to nftables
If your firewall configuration still uses iptables
rules, you can migrate your iptables
rules to nftables
.
The ipset
and iptables-nft
packages have been deprecated in Red Hat Enterprise Linux 9. This includes deprecation of nft-variants
such as iptables
, ip6tables
, arptables
, and ebtables
utilities. If you are using any of these tools, for example, because you upgraded from an earlier RHEL version, migrate to the nft
command-line tool provided by the nftables
package.
21.1. Concepts in the deprecated iptables framework
Similar to the actively-maintained nftables
framework, the deprecated iptables
framework enables you to perform a variety of packet filtering tasks, logging and auditing, NAT-related configuration tasks, and more.
The iptables
framework is structured into multiple tables, where each table is designed for a specific purpose:
filter
- The default table, ensures general packet filtering
nat
- For Network Address Translation (NAT), includes altering the source and destination addresses of packets
mangle
- For specific packet alteration, enables you to do modification of packet headers for advanced routing decisions
raw
- For configurations that need to happen before connection tracking
These tables are implemented as separate kernel modules, where each table offers a fixed set of builtin chains such as INPUT
, OUTPUT
, and FORWARD
. A chain is a sequence of rules that packets are evaluated against. These chains hook into specific points in the packet processing flow in the kernel. The chains have the same names across different tables, however their order of execution is determined by their respective hook priorities. The priorities are managed internally by the kernel to make sure that the rules are applied in the correct sequence.
Originally, iptables
was designed to process IPv4 traffic. However, with the inception of the IPv6 protocol, the ip6tables
utility needed to be introduced to provide comparable functionality (as iptables
) and enable users to create and manage firewall rules for IPv6 packets. With the same logic, the arptables
utility was created to process Address Resolution Protocol (ARP) and the ebtables
utility was developed to handle Ethernet bridging frames. These tools ensure that you can apply the packet filtering abilities of iptables
across various network protocols and provide comprehensive network coverage.
To enhance the functionality of iptables
, the extensions started to be developed. The functionality extensions are typically implemented as kernel modules that are paired with user-space dynamic shared objects (DSOs). The extensions introduce "matches" and "targets" that you can use in firewall rules to perform more sophisticated operations. Extensions can enable complex matches and targets. For instance you can match on, or manipulate specific layer 4 protocol header values, perform rate-limiting, enforce quotas, and so on. Some extensions are designed to address limitations in the default iptables
syntax, for example the "multiport" match extension. This extension allows a single rule to match multiple, non-consecutive ports to simplify rule definitions, and thereby reducing the number of individual rules required.
An ipset
is a special kind of functionality extension to iptables
. It is a kernel-level data structure that is used together with iptables
to create collections of IP addresses, port numbers, and other network-related elements that you can match against packets. These sets significantly streamline, optimize, and accelerate the process of writing and managing firewall rules.
For more details, see the iptables(8)
man page on your system.
21.2. Converting iptables and ip6tables rule sets to nftables
Use the iptables-restore-translate
and ip6tables-restore-translate
utilities to translate iptables
and ip6tables
rule sets to nftables
.
Prerequisites
-
The
nftables
andiptables
packages are installed. -
The system has
iptables
andip6tables
rules configured.
Procedure
Write the
iptables
andip6tables
rules to a file:iptables-save >/root/iptables.dump ip6tables-save >/root/ip6tables.dump
# iptables-save >/root/iptables.dump # ip6tables-save >/root/ip6tables.dump
Copy to Clipboard Copied! Convert the dump files to
nftables
instructions:iptables-restore-translate -f /root/iptables.dump > /etc/nftables/ruleset-migrated-from-iptables.nft ip6tables-restore-translate -f /root/ip6tables.dump > /etc/nftables/ruleset-migrated-from-ip6tables.nft
# iptables-restore-translate -f /root/iptables.dump > /etc/nftables/ruleset-migrated-from-iptables.nft # ip6tables-restore-translate -f /root/ip6tables.dump > /etc/nftables/ruleset-migrated-from-ip6tables.nft
Copy to Clipboard Copied! -
Review and, if needed, manually update the generated
nftables
rules. To enable the
nftables
service to load the generated files, add the following to the/etc/sysconfig/nftables.conf
file:include "/etc/nftables/ruleset-migrated-from-iptables.nft" include "/etc/nftables/ruleset-migrated-from-ip6tables.nft"
include "/etc/nftables/ruleset-migrated-from-iptables.nft" include "/etc/nftables/ruleset-migrated-from-ip6tables.nft"
Copy to Clipboard Copied! Stop and disable the
iptables
service:systemctl disable --now iptables
# systemctl disable --now iptables
Copy to Clipboard Copied! If you used a custom script to load the
iptables
rules, ensure that the script no longer starts automatically and reboot to flush all tables.Enable and start the
nftables
service:systemctl enable --now nftables
# systemctl enable --now nftables
Copy to Clipboard Copied!
Verification
Display the
nftables
rule set:nft list ruleset
# nft list ruleset
Copy to Clipboard Copied!
21.3. Converting single iptables and ip6tables rules to nftables
RHEL provides the iptables-translate
and ip6tables-translate
utilities to convert an iptables
or ip6tables
rule into the equivalent one for nftables
.
Prerequisites
-
The
nftables
package is installed.
Procedure
Use the
iptables-translate
orip6tables-translate
utility instead ofiptables
orip6tables
to display the correspondingnftables
rule, for example:iptables-translate -A INPUT -s 192.0.2.0/24 -j ACCEPT
# iptables-translate -A INPUT -s 192.0.2.0/24 -j ACCEPT nft add rule ip filter INPUT ip saddr 192.0.2.0/24 counter accept
Copy to Clipboard Copied! Note that some extensions lack translation support. In these cases, the utility prints the untranslated rule prefixed with the
#
sign, for example:iptables-translate -A INPUT -j CHECKSUM --checksum-fill
# iptables-translate -A INPUT -j CHECKSUM --checksum-fill nft # -A INPUT -j CHECKSUM --checksum-fill
Copy to Clipboard Copied!
21.4. Comparison of common iptables and nftables commands
The following is a comparison of common iptables
and nftables
commands:
Listing all rules:
iptables nftables iptables-save
nft list ruleset
Listing a certain table and chain:
iptables nftables iptables -L
nft list table ip filter
iptables -L INPUT
nft list chain ip filter INPUT
iptables -t nat -L PREROUTING
nft list chain ip nat PREROUTING
The
nft
command does not pre-create tables and chains. They exist only if a user created them manually.Listing rules generated by firewalld:
nft list table inet firewalld nft list table ip firewalld nft list table ip6 firewalld
# nft list table inet firewalld # nft list table ip firewalld # nft list table ip6 firewalld
Copy to Clipboard Copied!