Beginners Guide to nftables Traffic Filtering (2024)

Traffic filtering with nftables

Many Linux administrators became familiar with iptables and ip6tables. Less familiar are tools like arptables and ebtables. Meet the successor of them all: nftables, a packet filtering framework, with the goal to replace all the previous ones. After reading this guide you will be able to configure your own firewall configuration. Step by step we will show how nftables work. Although no knowledge of iptables is needed, we will share some differences with iptables where applicable. This way we can avoid inefficient rules.

Introduction: Netfilter

Before we start with this guide info nftables, it is good to know about netfilter. Both iptables and nftables use the netfilter components in the Linux kernel. This explains also the first two letters from this new traffic filtering solution.

One of the flaws in iptables is the slightly cryptic way of expressing which information flows are allowed. For that reason, the nftables syntax is shorter and easier to understand. Instead of saying “-p tcp”, it simplifies it to just “tcp”. This syntax is also very similar to what tcpdump is using. This is no surprise, as the project was inspired by the elegant syntax of tcpdump. If you know pf from operating systems FreeBSD or OpenBSD, you may also like the new syntax.

Suggested read: differences between iptables and nftables explained

Preparations

For this beginners guide, we assume that you have a recent Linux kernel. Support for nftables should also be compiled into the kernel, together with the related nftables modules. To be sure, check if you have the nf_tables kernel module available. and if the nftbinary is installed.

Kernel Modules

To determine if you have the nf_tables kernel module, use the modinfocommand.

modinfo nf_tables

The output will look something like this:

Beginners Guide to nftables Traffic Filtering (1)

Check if nf_tables module is available on your system.

Use lsmod to show any active nftables kernel module.

lsmod | grep nf_tables

This should at least reveal the nf_tables modules: nf_tables_inet, nf_tables_ip, nf_tables_ip6. On newer versions, this might be nf_tables_ipv4 and nf_tables_ipv6.

Beginners Guide to nftables Traffic Filtering (2)

Disable iptables

It is possible to mix iptables and nftables. However, this increases complexity and also the chance to introduce errors. So keep it simple and flush out all iptables rules, and make sure it is not loaded.

iptables -F

Do the same for IPv6:

ip6tables -F

Ensure that during system reboots the iptables configuration or modules are no longer loaded.

Kernel and client

We already have seen the active kernel modules in the sections before. Here is a big difference with iptables. The intelligence of the rulesets has been moved to the client utilitynft. This utility parses the rules and compiles them into a language that the kernel module understands. This way the kernel receives an optimized set of instructions.Another benefit is that you can manipulate the rules, instead of having to flush the configuration with every new change.

Configuration of nftables

Before showing examples, it is good to know some basic rules regarding the configuration syntax.

The hash sign (#) is used for comments, similar to your shell. To combine several commands, use the semicolon (;) sign.To split an instructioninto several lines, use a backslash (\) at the end of the line. Then continue with your nft command on the next one.

So in short:

  • # = comment
  • ; = more commands or parameters to follow
  • \ = break a rule into multiple lines

Tip: when a statementincludes a semicolon, you can tell your shell to ignore it, by adding a backslash.

Variables

Repetition is bad. To simplify things, nftables supports variables. Instead of repeating an interface multiple times, you define it at the beginning of your configuration file. After that, you will be using the variable.

Example: defining interfaces
define ext_if = eth0define int_if = eth1define all_if = { $ext_if, $int_if }

Tables

Within the configuration of nftables, a table is at the top of the ruleset. It consists of chains, which are containers for rules. Overview: Tables –> Chains –> Rules.

The maximum length of a table name is 27 characters. At this moment you can create a table (add), delete it (delete), display it (list) or empty it (flush).

Address Families

All objects within nftables have a so called namespace, which includes the address family. This address family specifies what kind of hooks will be applied for further analysis of the information stream. For example this can beip for IPv4 traffic, or ip6 for IPv6 traffic. As nftables is aware of the ongoing usage of IPv6, it simplifies usage for both protocol families. It does so by combining them both within theinet address family.

For filtering arp traffic, we previously used arptables. With nftables that kind of network traffic belongs to the arp address family.

If you have configured a bridged interface, you may want to use bridge (previously ebtables). Then there is netdev, which is used for ingress filtering, or traffic coming into the system. It allows for early filtering traffic, before it reaches other filters (below layer 3 on OSI model).

Overview:

  • arp
  • bridge
  • inet (= ip + ip6)
  • ip
  • ip6
  • netdev

As you can see, the names are as short as possible. You can combine rulesets for IPv4 and IPv6 traffic with the inet address family. Netdev is one of the latest additions and allows filtering before it is

Chains

After creating a table, the next step is to create chains. Chains are containers holding rules, and are of a defined type. Chains can be 1 of the two types: base or non-base. Being a base type chain, it has a related hook in the kernel. With a hook, the related chain can “see” the traffic, otherwise it can’t.

nft add chain ip traffic-filter output { type filter hook output priority 0 \; policy accept\; }

Chain types: base, non-base

Hook: input, output

Rules

The basic building blocks of rule in nftables consists of the following components:

  • expression(s)
  • operator
  • action

The expressions within a rule are evaluated from left to right. When the first expression matches, it continues with the other parts. If the expression does not result in a positive outcome, the next rule in line will be evaluated.

Examples

nft add rule Firewall Incoming ip daddr 192.168.0.1-192.168.0.19 drop

For example, tcp dport 22 accept

Advanced Configuration

Sets

A set is a collection of data elements. This could be for example filled with IPv4 addresses, or port numbers.Maximum length of a set name is 15 characters. If you exceed this is, an error will follow:

Could not process rule: Numerical result out of range
Anonymous Sets

dport { 22, 23, 80, 443 }

Usage: Directly used in rules

Named Sets

nft add set inet blacklist blacklist4-perm { type ipv4_addr \; }nft add element inet blacklist blacklist4-perm { 192.168.1.21, 192.168.1.22 }nft add rule inet blacklist input ip saddr @blacklist4-perm drop

Usage: Filled with data, then referenced in a rule

Mappings

A map is used to do a mapping. You use one field, to look up the value of another, and act on that.

Examples needed

Dictionaries

Another type is the dictionary (or verdict maps). They use the structure of a set and are a powerful component within nftables, as they can include the verdict.

nft add rule ip Firewall Forward ip daddr vmap {\ 192.168.1.1-192.168.1.10: jump chain-dmz, \ 192.168.2.1-192.168.2.99 : jump chain-ssn1, \ 192.168.2.100-192.168.2.199 : jump chain:ssn2, \ 192.168.3.1-192.168.3.50: jump chain-desktops \}

Traffic Hooks

Each type of traffic has one or more possible traffic hooks. They can be used to make more specific filters.

Interfaces

iifname = Incoming interface
oifname = Outgoing interface

Protocols

Basic syntax
<protocol> <dport/sport> <port> <action>
icmp
udp
ip
tcp

dport/sport: destination port or source port. For example SSH running on our system, would indicate port 22 as destination for incoming traffic. So in this case: tcp dport 22

Outgoing traffic to another server, would be outgoing traffic to the SSH daemon on the target, which would be dport as well.

oifname lo accept

icmp type {echo-reply} drop
icmp accept
udp sport bootpc dport bootps accept
ip daddr 127.0.0.1 tcp dport {http, postgresql, ipp} accept
udp dport dns accept
tcp dport {dns, http, ntp, https, 9418} accept

Creating Tables, Chains and Rules

Next step is creating tables.

nft add table inet incoming-traffic

Note: if you don’t specify inet, the ip address family will be used by default

Within a table, we then create a chain. A chain is a container of one or more rules and used for the organization of the rule. In other words, it is a ruleset.

Now we have created our table, we add an input chain.

nft list table inet incoming-traffictable inet incoming-traffic { chain input { }}

nft add chain inet incoming-traffic management

nft add rule inet incoming-traffic management tcp dport 22

nft add chain inet incoming-traffic web-traffic

nft add rule inet incoming-traffic web-traffictcp dport 80 counter

nft add rule inet incoming-traffic web-traffictcp dport 443 counter

Best Practices for nftables

Use clear names

Like in the world ofsoftware development, you have to use self-declaring names for your tables. Some examples use the name “filter”, which is confusing on what it is doing specifically.

Frequently Asked Questions

How can I see all tables for IPv4 and IPv6?

Use the inet address family when using both IPv4 (ip) and IPv6 (ip6).

nft list tables inet

Why do I get an error when trying to show an existing table?

You might have forgotten to specify the address family. Use nft list tables first.

How can I see the rule numbers within each table and/or chain?

nft list table inet filter-traffic -a

How can I export my rules and backup them?

Use nft export xml or nft export json

Common Mistakes

Like most firewall types, it is easy to make mistakes. We have collected a few common mistakes, so you can avoid them early on.

Loading rules without flushing

When loading rules from a file, flush them first. Or better, make a backup first by exporting them. Then flush the rules and import them.

Double firewall rules in nftables

Splitting IPv4 and IPv6

nftables has a great facility to combine traffic for IPv4 (ip) and IPv6 (ip6), named “inet”. This way you can enable incoming traffic for your web server on port 80, for both protocol families.

Making rules too complicated

Like in life, more rules result in more complexity. Keep things as simple as possible. Most likely you are not the only one who has to understand your firewall rules, so building rules needs some attention. Another important step is documenting specific rules which are not obvious.

Forgetting the protocol family

When requesting the list of active tables, the result set might seem to be empty. By default, the nft utility will use the ip protocol family. For example, when using the inet family, this will result in no entries listed.

Errors

Invalid table

<cmdline>:1:1-12: Error: Could not process rule: Table 'x' does not exist

If you try to list a non-existing table, you will receive this error. Show the tables with list tables.

nft list tables

Other useful resources

Here are some other resources to use

  • nftables wiki
  • Quick reference guide
  • http://lists.netfilter.org/pipermail/netfilter-announce/2014/000211.html
  • http://people.netfilter.org/pablo/netdev0.1/slides/nftables-netdev-2015.pdf
  • https://wiki.gentoo.org/wiki/Nftables#Adding_chains
  • https://lwn.net/Articles/657933/
Beginners Guide to nftables Traffic Filtering (2024)

FAQs

Is nftables better than iptables? ›

nftables incorporates advanced data structures such as dictionaries, maps and concatenations that do not exist with iptables. Making use of these can significantly reduce the number of chains and rules needed to express a given packet filtering design.

Does UFW work with nftables? ›

Ufw stands for Uncomplicated Firewall, and is a program for managing a netfilter firewall. It provides a command line interface and aims to be uncomplicated and easy to use. Note: It should be noted that UFW can use either iptables or nftables as the back-end firewall.

Is nftables a firewall? ›

Two of the most common uses of nftables is to provide firewall support and Network Address Translation (NAT). nftables is the default and recommended firewalling framework in Debian, and it replaces the old iptables (and related) tools.

How do I make nftables persistent? ›

Making iptable rules persistent
  1. Add rules to the iptables according to your requirment.
  2. Verify that all the rules are present using the command “iptables -L“. # iptables -L.
  3. Save the iptables. # service iptables save.
  4. Restart the service. # service iptables restart.
  5. Making service permanently ON using chkconfig.

Does Ubuntu 20.04 use nftables? ›

nftables is now the default in Debian 10, Ubuntu 20.04, RHEL 8, SUSE 15 and Fedora 32.

Can Docker use nftables? ›

If I have iptables running, Docker DNS seems to work but there are no rules added to iptables. I don't understand this, why does it require iptables but make no rules? Docker doesn't support nftables .

Can nftables and iptables coexist? ›

This is possible because nftables provides implementation of the main iptables userland tools (iptables, ip6tables, arptables, ebtables) which automatically translate iptables rules to nftables rules 7.

Is iptables obsolete? ›

As of Rocky Linux 9.0, iptables and all of the utilities associated with it, are deprecated. This means that future releases of the OS will be removing iptables . For that reason, it is highly recommended that you not use this process. If you are familiar with iptables, we recommend using iptables Guide To firewalld .

Which is better ufw or Firewalld? ›

ufw is a full featured interface for the CLI, while firewalld mostly just provides an API and you'd have to use another program on top of that. I haven't used firewalld much myself, but ufw does have a lot of experience/exposure as it's the recommended tool for Ubuntu-based distros.

Should I use ufw or iptables? ›

IPtables and UFW both are Linux system firewalls, the difference between them is UFW is built upon IPtables, IPtables a very flexible tool but it's more complex as compared to UFW, other difference is that IPtables requires a deeper understanding of TCP/IP, which might not be the case with every Linux user, so UFW is ...

Is ufw a good firewall? ›

The Uncomplicated Firewall (ufw) is a frontend for iptables and is particularly well-suited for host-based firewalls. ufw provides a framework for managing netfilter, as well as a command-line interface for manipulating the firewall.

Is nftables open source? ›

According to Netfilter project, nftables is an open-source and free packet classification framework, released in 2014 for Linux, and provides packet filtering, and network address translation (NAT).

Where are nftables rules stored? ›

nftables user-space utility nft performs most of the rule-set evaluation before handing rule-sets to the kernel. Rules are stored in chains, which in turn are stored in tables.

What is the difference between firewalld and iptables? ›

The firewall

On the one hand, iptables is a tool for managing firewall rules on a Linux machine. On the other hand, firewalld is also a tool for managing firewall rules on a Linux machine.

Do I need to restart iptables after adding a rule? ›

Yes, once you have added a rule to iptables it becomes active immediately - this is why you should be careful with your rules as it is possible to lock yourself out.

Is iptables a firewall? ›

Simply put, iptables is a firewall program for Linux. It will monitor traffic from and to your server using tables. These tables contain sets of rules, called chains, that will filter incoming and outgoing data packets.

How do I permanently save iptables rules? ›

Saving and restoring iptables rules
  1. These can be saved in a file with the command iptables-save for IPv4. Debian/Ubuntu: iptables-save > /etc/iptables/rules.v4. ...
  2. These files can be loaded again with the command iptables-restore for IPv4. ...
  3. If you want to use IPv6 rules, these can be stored in a separate file.

Does Ubuntu use iptables or nftables? ›

A fresh server installation of Ubuntu 21.10 Server shows exactly what you are seeing - that in fact the back-end is still using iptables on a standard server installation. xtables-nft-multi (or simply xtables-multi)'s manpage shows an explanation: xtables-nft are versions of iptables that use the nftables API.

Does Docker have its own firewall? ›

Docker inserts its own iptables rules, which bypass ufw's own iptables rules. So those ufw rules that you think are protecting your docker services, are not actually doing that. You will notice both ufw and docker have inserted their own rules.

Does Docker need iptables? ›

Docker installs two custom iptables chains named DOCKER-USER and DOCKER , and it ensures that incoming packets are always checked by these two chains first. All of Docker's iptables rules are added to the DOCKER chain.

How do I switch from iptables to nftables? ›

Migrate existing Iptables to Nftables in RHEL8/CentOS
  1. Introduction. Export IPtables to a file. Verify the Existing Rules. Convert the Iptables to nftables. Load and Import the rules. List and verify the nftables.
  2. Conclusion.
28 Aug 2019

Does Rhel 8 use iptables? ›

In RHEL 8 nftables replaces iptables as the default Linux network packet filtering framework.

Does firewalld use netfilter? ›

Firewalld is at the top and iptables or nftables is running on the backend. Iptables or nftables running on the backend is operating netfilter. Older versions of firewalld use iptables as the backend, and newer versions of firewalld use nftables as the backend.

What can I use instead of iptables? ›

Nftables is a new packet classification framework that aims to replace the existing iptables, ip6tables, arptables and ebtables facilities. It aims to resolve a lot of limitations that exist in the venerable ip/ip6tables tools.

Is iptables a stateful firewall? ›

The raw table: iptables is a stateful firewall, which means that packets are inspected with respect to their “state”. (For example, a packet could be part of a new connection, or it could be part of an existing connection.)

What was before iptables? ›

The ipchains software was superseded by the iptables system in Linux kernel 2.4 and above.
...
ipchains.
Developer(s)Rusty Russell
Written inC
Operating systemLinux
PlatformLinux kernel
Successoriptables
7 more rows

Why is firewalld better than iptables? ›

Firewalld is relatively newer. It functions on a different security metaphor, and in certain ways, can be thought of as easier to configure. Iptables and Firewalld cannot, however, run at the same time, on the same system. And they require some mutual exclusion rules before we can get started.

Is UFW a firewalld? ›

Here is a quick tutorial for how to migrate from FirewallD, the default firewall in Fedora Linux and CentOS, to the Uncomplicated Firewall ( UFW ). UFW is the default firewall in Ubuntu and has more intuitive commands that require less typing.

What is firewalld used for? ›

firewalld is a firewall management tool for Linux operating systems. It provides firewall features by acting as a front-end for the Linux kernel's netfilter framework. firewalld's current default backend is nftables.

Is UFW stateful? ›

UFW comes with a default incoming policy of deny , a default forward policy of deny , and a default outgoing policy of allow , with stateful tracking for new connections for incoming and forwarded connections.

What type of firewall is UFW? ›

Uncomplicated Firewall (UFW) is a program for managing a netfilter firewall designed to be easy to use. It uses a command-line interface consisting of a small number of simple commands, and uses iptables for configuration.

Does Ubuntu use UFW? ›

Ubuntu ships with a firewall configuration tool called UFW (Uncomplicated Firewall). It is a user-friendly front-end for managing iptables firewall rules. Its main goal is to make managing firewall easier or, as the name says, uncomplicated.

Do I need to use UFW? ›

Most home Ubuntu users don't need to or use ufw . Both ufw and iptables are installed by default and are configured to do nothing. Why there is no need, is explained in more detail below.

Which firewall is most commonly used on Linux? ›

The most widely used command-line-based firewall is Iptables/Netfilter. It is the initial line of defense for the security of a Linux server. It's used by many system administrators to fine-tune their systems. Within the kernel, it filters packets in the network stack.

Which firewall is best for Linux? ›

Best Linux firewalls of 2022 in full:
  1. IPFire. Best free Linux firewall that's easy-to-use. ...
  2. OPNsense. Best free Linux firewall for scalability. ...
  3. pfSense. Best free Linux firewall that's feature-rich and FreeBSD-based. ...
  4. ClearOS. Best free Linux firewall for easy deployment. ...
  5. OpenWRT. Best free Linux firewall for routers.
12 Aug 2022

Where are nftables rules stored? ›

nftables user-space utility nft performs most of the rule-set evaluation before handing rule-sets to the kernel. Rules are stored in chains, which in turn are stored in tables.

Can Docker use nftables? ›

If I have iptables running, Docker DNS seems to work but there are no rules added to iptables. I don't understand this, why does it require iptables but make no rules? Docker doesn't support nftables .

What is Nf_tables? ›

nftables is the successor to iptables. It replaces the existing iptables, ip6tables, arptables, and ebtables framework. It uses the Linux kernel and a new userspace utility called nft. nftables provides a compatibility layer for the ip(6)tables and framework.

What iptables can do? ›

Simply put, iptables is a firewall program for Linux. It will monitor traffic from and to your server using tables. These tables contain sets of rules, called chains, that will filter incoming and outgoing data packets.

How do you check NFT rules? ›

nft - Man Page
  1. View current configuration: sudo nft list ruleset.
  2. Add a new table with family "inet" and table "filter": sudo nft add table inet filter.
  3. Add a new chain to accept all inbound traffic: sudo nft add chain inet filter input \{ type filter hook input priority 0 \; policy accept \}

What is the difference between firewalld and iptables? ›

The firewall

On the one hand, iptables is a tool for managing firewall rules on a Linux machine. On the other hand, firewalld is also a tool for managing firewall rules on a Linux machine.

Does Debian have firewall? ›

A Debian firewall can also be installed in order to protect, with filtering rules, access to systems behind it, limiting their exposure to the Internet. A firewall can be configured to prevent access from systems outside of the local network to internal services (ports) that are not public.

Does Docker have its own firewall? ›

Docker inserts its own iptables rules, which bypass ufw's own iptables rules. So those ufw rules that you think are protecting your docker services, are not actually doing that. You will notice both ufw and docker have inserted their own rules.

Does Docker need iptables? ›

Docker installs two custom iptables chains named DOCKER-USER and DOCKER , and it ensures that incoming packets are always checked by these two chains first. All of Docker's iptables rules are added to the DOCKER chain.

Is nftables open source? ›

According to Netfilter project, nftables is an open-source and free packet classification framework, released in 2014 for Linux, and provides packet filtering, and network address translation (NAT).

What is nft command? ›

nft is the command line tool used to set up, maintain and inspect packet filtering and classification rules in the Linux kernel, in the nftables framework. The Linux kernel subsystem is known as nf_tables, and 'nf' stands for Netfilter.

Is iptables being deprecated? ›

The ipset and iptables-nft packages have been deprecated.

What are the advantages of iptables? ›

IPTables Basics

IPTables is a very powerful security tool used to block unwanted traffic, allow desired traffic, redirect packets to alternate TCP/UDP ports, redirect packets to alternate IP addresses, protect against Denial of Service attacks (DoS) and so much more.

What is IP tables and filtering? ›

iptables is a user-space utility program that allows a system administrator to configure the IP packet filter rules of the Linux kernel firewall, implemented as different Netfilter modules. The filters are organized in different tables, which contain chains of rules for how to treat network traffic packets.

What type of firewall is iptables? ›

Netfilter/iptables is a stateful inspection type firewall.

Top Articles
Latest Posts
Article information

Author: Duncan Muller

Last Updated:

Views: 5680

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Duncan Muller

Birthday: 1997-01-13

Address: Apt. 505 914 Phillip Crossroad, O'Konborough, NV 62411

Phone: +8555305800947

Job: Construction Agent

Hobby: Shopping, Table tennis, Snowboarding, Rafting, Motor sports, Homebrewing, Taxidermy

Introduction: My name is Duncan Muller, I am a enchanting, good, gentle, modern, tasty, nice, elegant person who loves writing and wants to share my knowledge and understanding with you.