basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    OverviewLinuxFirewall

    nftables: the Linux kernel firewall that replaces iptables

    nftables is the Linux kernel firewall that has been replacing iptables since 2014: one nft tool instead of four, one rule for IPv4 and IPv6, address sets inside the kernel, and a config that applies as a whole or not at all. On Ubuntu 24.04 it already runs under ufw and Docker, even if nobody has ever run nft. Write your own rules into your own table and leave the others’ alone.

    What problem it solves

    The Linux kernel filters packets, in its netfilter subsystem. A tool — iptables or nft — only loads rules into the kernel and shows them. nftables is the new part of netfilter: the nf_tables kernel module and its nft tool. It replaces four old tools at once (iptables, ip6tables, arptables, ebtables) plus ipset.

    Who needs to know it and why:

    • Anyone with an Ubuntu server running ufw and Docker. You don’t have to write rules in nft, but the ufw, Docker and CrowdSec rules already live in nftables, and sudo nft list ruleset is the only place where you see them all together. Without it, “why is this port open” comes down to guessing.
    • Anyone for whom ufw isn’t enough: lists of thousands of addresses, a one-hour ban without cron, a connection limit per address, rules for container ports, NAT on a gateway.
    • Linux gateways and routers: NAT, forwarding between networks and speeding up established connections with a flowtable.

    On a laptop or a workstation there’s nothing to do: firewalld sets the rules there, or there are none at all.

    How it used to be: iptables and its neighbours

    The Linux kernel firewall has changed four times: ipfwadm in kernel 2.0, ipchains in 2.2, iptables in 2.4 (2001) and nftables, which entered kernel 3.13 in January 2014. iptables lived the longest, and its limits grew out of its design:

    • A separate tool for each family. A rule for IPv4 is iptables, the same for IPv6 is ip6tables. Forget the second, and the port is closed over IPv4 and open over IPv6. Bridges are ebtables, ARP is arptables, each with its own syntax.
    • Tables and chains are hard-wired: filter, nat, mangle, raw, and inside them INPUT, FORWARD, OUTPUT. Docker, ufw and fail2ban insert rules into the same chains, and the order depends on who started last.
    • An edit rewrites the whole table. Every iptables -A call reads the entire table from the kernel, changes it and writes it back. With thousands of rules (Kubernetes with hundreds of services) one change takes seconds, and parallel calls wait for the /run/xtables.lock lock with the error Another app is currently holding the xtables lock.
    • Rules are checked one by one. Blocking ten thousand addresses means ten thousand rules, and every packet goes through them in turn. For lists, a separate ipset with its own tool was made.
    • Every new match is a kernel module xt_* plus a tool extension.

    The transition went through the iptables-nft compatibility layer: the iptables command stayed the same, but it writes rules into nftables. That’s the default since Debian 10 and RHEL 8 (both 2019), and in Ubuntu 24.04 too. The version shows which one the server has:

    sudo iptables -V
    # iptables v1.8.10 (nf_tables)

    (nf_tables) — the iptables command writes into nftables; (legacy) — into the old subsystem.

    From here, software moves to nftables directly. Kubernetes: kube-proxy’s nftables mode is stable since 1.33. Docker 29: an experimental "firewall-backend": "nftables" mode in /etc/docker/daemon.json; in it Docker writes its own ip docker-bridges and ip6 docker-bridges tables without iptables, and there’s no DOCKER-USER chain.

    How it works: table, chain, hook

    A packet passes through the kernel via points called hooks. At each hook, netfilter hands it to the chains attached to that hook:

    packet from outside
      └─ prerouting           Docker does DNAT to the container's address here
           ├─ addressed to the host ─→ input   ──→ sshd, services on the host itself
           └─ not to the host ───────→ forward ─┐  containers with published ports
    process on the host ──→ output ─────────────┴─→ postrouting ──→ out
                                                    masquerade for containers

    Before prerouting there’s also the ingress hook — the packet straight from the network card, before fragment reassembly and conntrack.

    What rules are made of:

    • A table is a container with a family: ip (IPv4), ip6, inet (both at once), arp, bridge, netdev. There are no predefined tables: an empty kernel has zero rules, and everything is created by some program or by you. The name is arbitrary; every program has its own table.
    • A base chain is attached to a hook with a line like type filter hook input priority filter; policy drop;. A regular chain without a hook gets packets only by jump from another one.
    • The priority is a number; lower runs earlier. The names are for readability: raw = −300, mangle = −150, dstnat = −100, filter = 0, security = 50, srcnat = 100. filter - 10 means 10 earlier than the regular filter.
    • A chain’s rules are conditions plus an action. They’re checked in order until the first verdict: accept, drop, reject, jump, return. counter and log aren’t verdicts; after them the check continues.

    Several tables on one hook

    On the input hook of a server with Docker, ufw and CrowdSec there are several base chains from different tables at once. The kernel runs them by priority, and one rule applies to them: drop is final, accept isn’t. accept ends only its own chain, and the packet goes to the next one. A packet gets through only if nobody dropped it.

    sudo nft list ruleset | grep -E '^table|hook'
    # table ip filter {
    #         type filter hook input priority filter; policy drop;
    #         type filter hook forward priority filter; policy drop;
    #         type filter hook output priority filter; policy accept;
    # table ip crowdsec {
    #         type filter hook input priority filter - 10; policy accept;
    #         type filter hook forward priority filter - 10; policy accept;

    ip filter is the iptables-nft table, holding the ufw rules (drop policy on input) and Docker’s (drop policy on forward). The crowdsec table sits 10 earlier, drops banned addresses and passes the rest on — that’s why it gets along with both. And that’s also why an accept in your table won’t open a port that ufw closed.

    Sets and maps

    A set is a list of values inside the kernel: addresses, ports, networks, “address . port” pairs. A set lookup is a search in a hash table or a tree, not a walk through rules: blocking ten thousand addresses is one rule.

    tcp dport { 22, 80, 443 } accept                          anonymous set right in the rule
    ip saddr @blocked4 drop                                   named: elements are added on the fly
    ip saddr . tcp dport { 10.0.0.5 . 5432 } accept           an "address . port" pair
    iifname vmap { "lo" : accept, "eth0" : jump wan }         verdict map: value → verdict

    A set has flags: interval — elements are networks (10.0.0.0/8), timeout — an element drops out on its own after a set time, dynamic — rules fill the set as packets pass. That’s how the CrowdSec ban works: addresses sit in sets with a timeout and vanish when the decision expires.

    Transaction

    nft -f file applies the whole file in one transaction. An error on any line — nothing changes, and the old rules stay in the kernel. nft -c -f file checks the file with the same parser and doesn’t apply it.

    How it differs from iptables

    iptables nftables
    Tools iptables, ip6tables, arptables, ebtables, ipset one, nft
    IPv4 and IPv6 two rule sets the inet family: one rule for both
    Tables and chains predefined, shared by all programs each program has its own, attached to a hook with a priority
    Address lists a separate ipset sets and maps in the kernel, with networks and timeouts
    Editing the whole table is read and written again, a global lock only what changed is changed; a file goes in one transaction
    Counters always, on every rule only where counter is written
    A new match a new xt_* kernel module usually no module: the rule is compiled into bytecode for a small virtual machine in the kernel
    Output for scripts parsing the text of iptables-save nft -j — JSON

    Who already writes into nftables

    Ubuntu 24.04

    ufw and Docker

    They write through iptables-nft: their rules are in the ip filter, ip nat, ip6 filter, ip6 nat tables. Don’t edit these tables with nft: the next time ufw or Docker starts, it rewrites them its own way.

    firewalld

    The firewall of Fedora, RHEL and Rocky. It writes into nftables directly and presents zones and services rather than rules.

    Kubernetes

    kube-proxy in nftables mode keeps routes to services in sets and maps instead of thousands of iptables rules.

    A Linux gateway

    NAT, forwarding between networks and a flowtable: after the first packets, an established connection takes a short path that bypasses the chains.

    Your own server firewall

    Your own inet table with sets — when ufw isn’t enough. An example is below.

    What to pick

    If… Use
    an Ubuntu server that needs 22, 80 and 443 open ufw (The ufw firewall); nft — to read what came out
    ban brute force and scanners based on logs CrowdSec with a bouncer in nftables (CrowdSec against SSH brute force)
    you need sets, timed bans, per-address limits, rules for container ports your own nftables table, with ufw turned off
    a server on Fedora, RHEL, Rocky firewalld — it already runs on nftables
    close everything, Docker ports included, before traffic reaches the server the provider’s firewall as a second layer
    a gateway between networks nftables: NAT and a flowtable

    Two firewalls on one hook — ufw and your own table — both work, and a packet gets through only if both let it pass. So pick one; a table of your own next to ufw should hold only drop rules, like CrowdSec’s.

    What you get and what it costs

    What you get

    • IPv6 doesn’t get forgotten. An inet table means one rule for both protocols.
    • Sets in the kernel. Thousands of addresses are one rule; a one-hour ban is an element with timeout 1h, no cron needed to lift it.
    • All or nothing. nft -f applies a file in one transaction: a typo won’t leave the server with half its rules.
    • Your table is separate from the others. Docker and ufw don’t touch it; to remove all your rules — delete table.
    • Everything is visible at once. nft list ruleset shows every program’s rules, nft monitor trace the path of a specific packet through each rule.

    What it costs

    • Instructions online are mostly for iptables. iptables-translate converts individual rules, but not everything, and not always literally.
    • Docker and ufw stay on iptables-nft. Their tables are visible in nft, but you can’t edit them there, and your own rules for containers have to sit alongside, with an earlier priority.
    • accept isn’t final. Two firewalls on one hook bring surprises: a port allowed in one place and closed in another.
    • flush ruleset wipes everything, other programs’ tables included. It’s the first line of the stock config.
    • The syntax depends on the version. Some examples from wiki.nftables.org need a recent kernel: NAT in inet — kernel 5.2, the destroy command — kernel 6.3 and nftables 1.0.8.

    Your own server firewall

    A config for a server where your own rules replace ufw, while Docker and CrowdSec stay. The rules live only in your own inet server table; the file doesn’t touch other tables.

    /etc/nftables.conf
    #!/usr/sbin/nft -f
    
    define SSH_PORT = 22

    The SSH port as a variable: it changes in one place. #!/usr/sbin/nft -f makes the file an executable script.

    table inet server
    delete table inet server
    Key part

    Instead of flush ruleset, delete only your own table. The first line creates it empty if it doesn’t exist yet — otherwise delete would fail on the first run. All of this is inside one transaction: the old table disappears and the new one appears at once.

    table inet server {
        set allowed_tcp {
            type inet_service
            elements = { $SSH_PORT, 80, 443 }
        }

    Ports open to everyone. 80 and 443 are needed if the web server runs on the host itself. Traefik in a container with ports: 80:80 goes through forward, Docker opens its port, and this rule doesn’t concern it. With HTTP/3, also add udp dport 443 accept to the chain.

        set blocked4 { type ipv4_addr; flags timeout; }
        set blocked6 { type ipv6_addr; flags timeout; }

    A manual ban with a timeout, a set per family. Add an address with a command, without reloading the rules: sudo nft add element inet server blocked4 '{ 198.51.100.66 timeout 1h }'. On nft -f the set is recreated empty.

        chain input {
            type filter hook input priority filter; policy drop;
    
            iif lo accept
            ct state established,related accept
            ct state invalid drop
            ip saddr @blocked4 drop
            ip6 saddr @blocked6 drop

    The drop policy: everything not allowed below is dropped. lo is the server talking to itself. established,related lets through replies to the server’s own requests and sessions that are already open, the current SSH one included: without this line it breaks right after the rules are applied.

            icmp type echo-request accept
            icmpv6 type { echo-request, nd-neighbor-solicit,
                          nd-neighbor-advert, nd-router-advert } accept

    ICMP. Ping is for monitoring. Errors like “packet too big” pass as related. conntrack doesn’t track Neighbor Discovery messages, so allow them explicitly: without them IPv6 doesn’t work at all.

            tcp dport @allowed_tcp ct state new accept
    
            limit rate 5/minute log prefix "nft drop: " level info
        }
    }

    New connections only to the ports from the set. Everything else is logged to the kernel journal at most 5 times a minute — otherwise scanners flood the log — and dropped by the policy.

    The file has no forward and output chains. Without a chain the kernel lets the packet through, and on a Docker host Docker itself handles forward. A forward of your own with policy drop would cut off every container: an accept in Docker’s table doesn’t cancel your drop.

    1. See which tables already exist

      sudo nft list tables
      # table ip filter
      # table ip nat
      # table ip6 filter
      # table ip6 nat
      # table ip crowdsec
      # table ip6 crowdsec6

      ip filter, ip nat and the ip6 pair are iptables-nft, holding Docker and ufw. crowdsec is the bouncer. The config touches none of them. There’s no inet server table yet.

    2. Write the config and check it without applying

      sudo apt install nftables
      sudo nano /etc/nftables.conf
      sudo nft -c -f /etc/nftables.conf

      Silence means the file parsed without errors. An error is printed with the line and ^^^ under the spot where parsing stumbled.

    3. Set a 3-minute rollback

      If SSH won’t let you in with the new rules, the inet server table deletes itself after 3 minutes, and the server returns to the previous rules.

      sudo systemd-run --on-active=3min --unit=nft-rollback /usr/sbin/nft delete table inet server
      # Running timer as unit: nft-rollback.timer
      # Will run service as unit: nft-rollback.service
    4. Apply and log in from a second window

      Keep the current SSH session open (Keep the old session open).

      sudo nft -f /etc/nftables.conf

      In a second window, log in over SSH again. It let you in — cancel the rollback: sudo systemctl stop nft-rollback.timer. It didn’t — wait 3 minutes, fix the config and repeat from the rollback step. No session at all — use the provider’s console (Provider console).

    5. Turn off ufw

      sudo ufw disable
      # Firewall stopped and disabled on system startup

      With your own table ufw isn’t needed anymore, and if you leave it, it’s a second firewall on the same hook. Docker’s rules in ip filter stay after this.

    6. Enable autostart and override the stop

      sudo systemctl edit nftables
      [Service]
      ExecStop=
      ExecStop=/usr/sbin/nft delete table inet server
      sudo systemctl enable --now nftables

      The empty ExecStop= clears the line from the stock service, the second one sets your own. Now stop and restart delete only your table. After editing the config — sudo systemctl reload nftables: the service runs the same nft -f.

    7. Check from the outside

      From another machine, or from your phone on mobile data:

      nc -vz 203.0.113.10 22
      # Connection to 203.0.113.10 22 port [tcp/ssh] succeeded!
      nc -vz -w 5 203.0.113.10 5432
      # nc: connect to 203.0.113.10 port 5432 (tcp) timed out: Operation now in progress

      A timeout, not Connection refused: the packet was dropped by drop, not rejected. On the server it shows in the journal: sudo journalctl -k -g 'nft drop'.

    Everyday commands

    Command What it does
    sudo nft list ruleset every rule of every table
    sudo nft list tables which tables exist
    sudo nft list table inet server only your own rules
    sudo nft -a list chain inet server input rules with their handle numbers
    sudo nft delete rule inet server input handle 12 delete a rule by number
    sudo nft add element inet server blocked4 '{ 198.51.100.66 timeout 1h }' ban an address for an hour
    sudo nft list set inet server blocked4 who’s banned and how long is left
    sudo nft add element inet server allowed_tcp '{ 8443 }' open a port until the next nft -f
    sudo nft -c -f /etc/nftables.conf check the file without applying
    sudo nft monitor who changes what in the rules right now
    sudo nft -j list ruleset all rules as JSON — for jq and scripts
    iptables-translate -A INPUT -p tcp --dport 22 -j ACCEPT convert an iptables rule to nft
    sudo journalctl -k -g 'nft drop' what was dropped, from the kernel journal

    A rule can’t be deleted by its text, the way ufw delete allow 8080 works: only by handle. That’s why permanent rules live in the file, and commands are for editing sets.

    Fine points

    A container port in forward is already the internal one

    Docker publishes 8080:80, DNAT changes the destination port to 80, and the packet reaches forward with tcp dport 80. To close a published port from the outside, match the port before DNAT — conntrack remembers it. The chain is your own, in the inet server table, with a priority earlier than Docker’s and an accept policy, holding only drop:

    chain forward {
        type filter hook forward priority filter - 10; policy accept;
        iifname "eth0" meta l4proto tcp ct original proto-dst 8080 drop
    }

    With this rule, port 8080 is silent from the outside, while 8081 of the same container answers; a tcp dport 8080 rule in the same place never fires. It’s simpler not to publish such a port at all: 127.0.0.1:8080:80 (A host-only port goes on 127.0.0.1).

    A limit on new connections per address

    Like ufw limit, but with a threshold per address and on any port — a dynamic set: the rule adds the source address itself and counts the limit for it.

    set ssh_meter4 { type ipv4_addr; flags dynamic, timeout; timeout 1m; }
    
    tcp dport $SSH_PORT ct state new add @ssh_meter4 { ip saddr limit rate over 10/minute burst 10 packets } drop

    Put the rule before tcp dport @allowed_tcp … accept. Write burst explicitly: without it nftables uses burst 5 packets, and the sixth connection in a row gets dropped, not the eleventh. A retransmitted SYN of the same connection also counts as new and uses up the limit. With key-only login, brute force is pointless anyway, and CrowdSec bans based on logs (CrowdSec against SSH brute force); the limit is for places without CrowdSec.

    A packet’s path through every rule

    When it’s unclear which table drops a packet, turn on tracing for one address. The trace chain runs before everything else (raw - 1) and only marks the packet:

    sudo nft 'add chain inet server trace { type filter hook prerouting priority raw - 1; }'
    sudo nft add rule inet server trace ip saddr 198.51.100.7 meta nftrace set 1
    sudo nft monitor trace

    The output lists every chain of every table the marked packet went through, and the outcome: the rule number with its verdict, or policy drop. When you’re done — sudo nft delete chain inet server trace (or nft -f the config, which recreates the table without this chain).

    Quotes in the terminal

    {, } and ; mean their own things in bash. Put a command containing them in single quotes, everything after nft: sudo nft 'add rule inet server input tcp dport { 8080, 8443 } accept'. For a series of commands, use sudo nft -i, the interactive mode with no bash in the middle.

    A ruleset snapshot isn’t a config

    sudo nft list ruleset > /etc/nftables.conf saves the Docker, ufw and CrowdSec tables too. On the next start the service loads their copy, and then Docker and the bouncer add their rules once more — with duplicates and stale container addresses. For your own config take only your own table: sudo nft list table inet server.

    legacy and nft at the same time

    Both subsystems can run in the kernel at once, and a packet goes through both. If some program writes through iptables-legacy, its rules show neither in nft list ruleset nor in a plain iptables -L — only the warning Warning: iptables-legacy tables present. Keep one variant on the server: iptables -V should show (nf_tables) everywhere.

    Converting old rules

    iptables-translate converts one rule, iptables-restore-translate -f rules.v4 a whole file from iptables-save:

    iptables-translate -A INPUT -p tcp --dport 22 -j ACCEPT
    # nft 'add rule ip filter INPUT tcp dport 22 counter accept'

    The result is in the ip family and the INPUT chain, as before. For your own config, move it to inet and fold repeats into sets: ten rules with different ports become one tcp dport { … }. What the translator doesn’t know, it leaves as a comment # -A INPUT …; rewrite those rules by hand.

    Where to start

    If it doesn’t work

    After systemctl restart nftables containers can’t reach the internet: even names don’t resolve, wget: bad address

    Cause
    A flush ruleset ran somewhere: as the first line of the stock /etc/nftables.conf or in the service’s ExecStop. It deleted every table in the kernel, including Docker’s, ufw’s and CrowdSec’s.
    Fix
    sudo systemctl restart docker — Docker writes its rules again; after it, sudo ufw reload and sudo systemctl restart crowdsec-firewall-bouncer. Then remove flush ruleset from the config and override the service’s ExecStop (the “Enable autostart” step).

    The port is allowed in your table, yet from the outside it still times out

    Cause
    There’s another base chain on the same hook, and it drops the packet: accept ends only its own chain, drop ends everything. Usually it’s ufw with a deny incoming policy.
    Fix
    Look at every chain on the hook: sudo nft list ruleset | grep -E '^table|hook input'. Either allow the port there too, or turn off the second firewall.

    After enabling your firewall the server stopped answering over IPv6, while IPv4 works fine

    Cause
    ICMPv6 is closed. Without Neighbor Discovery messages the server can’t find the gateway and its neighbours, and IPv6 stops working entirely.
    Fix
    Allow icmpv6 type { nd-neighbor-solicit, nd-neighbor-advert, nd-router-advert } before the drop policy — as in the config above.

    A container can’t connect to a service on the host itself — a database or host.docker.internal

    Cause
    For the host this is incoming traffic: it arrives in input from a br-… or docker0 interface and runs into policy drop.
    Fix
    Allow the port from Docker’s interfaces: iifname "br-*" tcp dport 5432 accept in the input chain.

    A tcp dport 8080 drop rule in forward doesn’t close the published port 8080:80

    Cause
    The packet reaches forward after DNAT, and the port in it is already the internal one — 80.
    Fix
    Match the port before DNAT: meta l4proto tcp ct original proto-dst 8080 drop.

    iptables -L shows one thing, nft list ruleset another, and nft prints Warning: iptables-legacy tables present

    Cause
    The server runs both subsystems at once: some programs write through iptables-legacy, some into nftables. A packet goes through both.
    Fix
    See who writes to legacy: sudo iptables-legacy-save. Move everyone to iptables-nft: sudo update-alternatives --config iptables and restart those programs.

    An nft add … { … ; } command in the terminal fails with a syntax error

    Cause
    bash parses ; and { } first: the semicolon splits the command in two, and only half of it reaches nft.
    Fix
    Put the whole tail of the command in single quotes: sudo nft 'add rule inet server input tcp dport { 80, 443 } accept'. Or edit the file and apply it with nft -f.

    The rules are gone after a reboot

    Cause
    The rules were added with nft add commands: they live in the kernel until a reboot. Or the nftables service isn’t enabled.
    Fix
    Move the rules into /etc/nftables.conf and enable the service: sudo systemctl enable nftables.