Quick reference-nftables in 10 minutes (2024)

Find below some basic concepts to know before using nftables.

table refers to a container of chains with no specific semantics.

chain within a table refers to a container of rules.

rule refers to an action to be configured within a chain.

Contents

  • 1 nft command line
    • 1.1 Tables
    • 1.2 Chains
    • 1.3 Rules
      • 1.3.1 Matches
        • 1.3.1.1 Ip
        • 1.3.1.2 Ip6
        • 1.3.1.3 Tcp
        • 1.3.1.4 Udp
        • 1.3.1.5 Udplite
        • 1.3.1.6 Sctp
        • 1.3.1.7 Dccp
        • 1.3.1.8 Ah
        • 1.3.1.9 Esp
        • 1.3.1.10 Comp
        • 1.3.1.11 Icmp
        • 1.3.1.12 Icmpv6
        • 1.3.1.13 Ether
        • 1.3.1.14 Dst
        • 1.3.1.15 Frag
        • 1.3.1.16 Hbh
        • 1.3.1.17 Mh
        • 1.3.1.18 Rt
        • 1.3.1.19 Vlan
        • 1.3.1.20 Arp
        • 1.3.1.21 Ct
        • 1.3.1.22 Meta
      • 1.3.2 Statements
        • 1.3.2.1 Verdict statements
        • 1.3.2.2 Log
        • 1.3.2.3 Reject
        • 1.3.2.4 Counter
        • 1.3.2.5 Limit
        • 1.3.2.6 Nat
        • 1.3.2.7 Queue
    • 1.4 Extras
      • 1.4.1 Export Configuration
      • 1.4.2 Monitor Events
  • 2 Nft scripting
    • 2.1 List ruleset
    • 2.2 Flush ruleset
    • 2.3 Load ruleset
  • 3 Examples
    • 3.1 Simple IP/IPv6 Firewall

nft is the command line tool in order to interact with nftables at userspace.

Tables

family refers to a one of the following table types: ip, arp, ip6, bridge, inet, netdev. It defaults to ip.

% nft list tables [<family>]% nft [-n] [-a] list table [<family>] <name>% nft (add | delete | flush) table [<family>] <name>

The argument -n shows the addresses and other information that use names in numeric format. The -a argument is used to display each rule's handle (i.e., a numerical identifier).

Chains

type refers to the kind of chain to be created. Possible types are:

  • filter: Supported by arp, bridge, ip, ip6 and inet table families.
  • route: Mark packets (like mangle for the output hook, for other hooks use the type filter instead), supported by ip and ip6.
  • nat: In order to perform Network Address Translation, supported by ip and ip6.

hook refers to an specific stage of the packet while it's being processed through the kernel. More info in Netfilter hooks.

  • The hooks for ip, ip6 and inet families are: prerouting, input, forward, output, postrouting.
  • The hooks for arp family are: input, output.
  • The bridge family handles ethernet packets traversing bridge devices.
  • The hooks for netdev are: ingress, egress.

priority refers to a number used to order the chains or to set them between some Netfilter operations. Possible values are: NF_IP_PRI_CONNTRACK_DEFRAG (-400), NF_IP_PRI_RAW (-300), NF_IP_PRI_SELINUX_FIRST (-225), NF_IP_PRI_CONNTRACK (-200), NF_IP_PRI_MANGLE (-150), NF_IP_PRI_NAT_DST (-100), NF_IP_PRI_FILTER (0), NF_IP_PRI_SECURITY (50), NF_IP_PRI_NAT_SRC (100), NF_IP_PRI_SELINUX_LAST (225), NF_IP_PRI_CONNTRACK_HELPER (300).

policy is the default verdict statement to control the flow in the base chain. Possible values are: accept (default) and drop. Warning: Setting the policy to drop discards all packets thathave not been accepted by the ruleset.

% nft (add | create) chain [<family>] <table> <name> [ \{ type <type> hook <hook> [device <device>] priority <priority> \; [policy <policy> \;] \} ]% nft (delete | list | flush) chain [<family>] <table> <name>% nft rename chain [<family>] <table> <name> <newname>

Rules

handle is an internal number that identifies a certain rule.

% nft add rule [<family>] <table> <chain> <matches> <statements>% nft insert rule [<family>] <table> <chain> [position <handle>] <matches> <statements>% nft replace rule [<family>] <table> <chain> [handle <handle>] <matches> <statements>% nft delete rule [<family>] <table> <chain> [handle <handle>]

Inserted rules are placed at the beginning of the chain, by default. However, if you specify a position handle, then the new rule is inserted just before the existing rule with that handle.

Matches

matches are clues used to access to certain packet information and create filters according to them.

Ip

ip match
dscp <value>
ip dscp cs1ip dscp!= cs1ip dscp 0x38ip dscp!= 0x20ip dscp { cs0, cs1, cs2, cs3, cs4, cs5, cs6, cs7, af11, af12, af13, af21, af22, af23, af31, af32, af33, af41, af42, af43, ef }
length <length>Total packet length
ip length 232ip length!= 233ip length 333-435ip length!= 333-453ip length { 333, 553, 673, 838 }
id <id>IP ID
ip id 22ip id!= 233ip id 33-45ip id!= 33-45ip id { 33, 55, 67, 88 }
frag-off <value>Fragmentation offset
ip frag-off & 0x1fff!= 0 # match fragmentsip frag-off & 0x2000!= 0 # match MF flag ip frag-off & 0x4000!= 0 # match DF flag
ttl <ttl>Time to live
ip ttl 0ip ttl 233ip ttl 33-55ip ttl!= 45-50ip ttl { 43, 53, 45 }ip ttl { 33-55 }
protocol <protocol>Upper layer protocol
ip protocol tcpip protocol 6ip protocol!= tcpip protocol { icmp, esp, ah, comp, udp, udplite, tcp, dccp, sctp }
checksum <checksum>IP header checksum
ip checksum 13172ip checksum 22ip checksum!= 233ip checksum 33-45ip checksum!= 33-45ip checksum { 33, 55, 67, 88 }ip checksum { 33-55 }
saddr <ip source address>Source address
ip saddr 192.168.2.0/24ip saddr!= 192.168.2.0/24ip saddr 192.168.3.1 ip daddr 192.168.3.100ip saddr!= 1.1.1.1ip saddr 1.1.1.1ip saddr & 0xff == 1ip saddr & 0.0.0.255 < 0.0.0.127
daddr <ip destination address>Destination address
ip daddr 192.168.0.1ip daddr!= 192.168.0.1ip daddr 192.168.0.1-192.168.0.250ip daddr 10.0.0.0-10.255.255.255ip daddr 172.16.0.0-172.31.255.255ip daddr 192.168.3.1-192.168.4.250ip daddr!= 192.168.0.1-192.168.0.250ip daddr { 192.168.0.1-192.168.0.250 }ip daddr { 192.168.5.1, 192.168.5.2, 192.168.5.3 }
version <version>Ip Header version
ip version 4
hdrlength <header length>IP header length
ip hdrlength 0ip hdrlength 15

Ip6

ip6 match
dscp <value>
ip6 dscp cs1ip6 dscp!= cs1ip6 dscp 0x38ip6 dscp!= 0x20ip6 dscp { cs0, cs1, cs2, cs3, cs4, cs5, cs6, cs7, af11, af12, af13, af21, af22, af23, af31, af32, af33, af41, af42, af43, ef }
flowlabel <label>Flow label
ip6 flowlabel 22ip6 flowlabel!= 233ip6 flowlabel { 33, 55, 67, 88 }ip6 flowlabel { 33-55 }
length <length>Payload length
ip6 length 232ip6 length!= 233ip6 length 333-435ip6 length!= 333-453ip6 length { 333, 553, 673, 838 }
nexthdr <header>Next header type (Upper layer protocol number)
ip6 nexthdr { esp, udp, ah, comp, udplite, tcp, dccp, sctp, icmpv6 }ip6 nexthdr espip6 nexthdr!= espip6 nexthdr { 33-44 }ip6 nexthdr 33-44ip6 nexthdr!= 33-44
hoplimit <hoplimit>Hop limit
ip6 hoplimit 1ip6 hoplimit!= 233ip6 hoplimit 33-45ip6 hoplimit!= 33-45ip6 hoplimit { 33, 55, 67, 88 }ip6 hoplimit { 33-55 }
saddr <ip source address>Source Address
ip6 saddr 1234:1234:1234:1234:1234:1234:1234:1234ip6 saddr::1234:1234:1234:1234:1234:1234:1234ip6 saddr::/64ip6 saddr::1 ip6 daddr::2
daddr <ip destination address>Destination Address
ip6 daddr 1234:1234:1234:1234:1234:1234:1234:1234ip6 daddr!=::1234:1234:1234:1234:1234:1234:1234-1234:1234::1234:1234:1234:1234:1234
version <version>IP header version
ip6 version 6

Tcp

tcp match
dport <destination port>Destination port
tcp dport 22tcp dport!= 33-45tcp dport { 33-55 }tcp dport { telnet, http, https }tcp dport vmap { 22: accept, 23: drop }tcp dport vmap { 25:accept, 28:drop }
sport < source port>Source port
tcp sport 22tcp sport!= 33-45tcp sport { 33, 55, 67, 88 }tcp sport { 33-55 }tcp sport vmap { 25:accept, 28:drop }tcp sport 1024 tcp dport 22
sequence <value>Sequence number
tcp sequence 22tcp sequence!= 33-45
ackseq <value>Acknowledgement number
tcp ackseq 22tcp ackseq!= 33-45tcp ackseq { 33, 55, 67, 88 }tcp ackseq { 33-55 }
flags <flags>TCP flags
tcp flags { fin, syn, rst, psh, ack, urg, ecn, cwr }tcp flags cwrtcp flags!= cwr
window <value>Window
tcp window 22tcp window!= 33-45tcp window { 33, 55, 67, 88 }tcp window { 33-55 }
checksum <checksum>IP header checksum
tcp checksum 22tcp checksum!= 33-45tcp checksum { 33, 55, 67, 88 }tcp checksum { 33-55 }
urgptr <pointer>Urgent pointer
tcp urgptr 22tcp urgptr!= 33-45tcp urgptr { 33, 55, 67, 88 }
doff <offset>Data offset
tcp doff 8

Udp

udp match
dport <destination port>Destination port
udp dport 22udp dport!= 33-45udp dport { 33-55 }udp dport { telnet, http, https }udp dport vmap { 22: accept, 23: drop }udp dport vmap { 25:accept, 28:drop }
sport < source port>Source port
udp sport 22udp sport!= 33-45udp sport { 33, 55, 67, 88 }udp sport { 33-55 }udp sport vmap { 25:accept, 28:drop }udp sport 1024 udp dport 22
length <length>Total packet length
udp length 6666udp length!= 50-65udp length { 50, 65 }udp length { 35-50 }
checksum <checksum>UDP checksum
udp checksum 22udp checksum!= 33-45udp checksum { 33, 55, 67, 88 }udp checksum { 33-55 }

Udplite

udplite match
dport <destination port>Destination port
udplite dport 22udplite dport!= 33-45udplite dport { 33-55 }udplite dport { telnet, http, https }udplite dport vmap { 22: accept, 23: drop }udplite dport vmap { 25:accept, 28:drop }
sport < source port>Source port
udplite sport 22udplite sport!= 33-45udplite sport { 33, 55, 67, 88 }udplite sport { 33-55 }udplite sport vmap { 25:accept, 28:drop }udplite sport 1024 udplite dport 22
checksum <checksum>Checksum
udplite checksum 22udplite checksum!= 33-45udplite checksum { 33, 55, 67, 88 }udplite checksum { 33-55 }

Sctp

sctp match
dport <destination port>Destination port
sctp dport 22sctp dport!= 33-45sctp dport { 33-55 }sctp dport { telnet, http, https }sctp dport vmap { 22: accept, 23: drop }sctp dport vmap { 25:accept, 28:drop }
sport < source port>Source port
sctp sport 22sctp sport!= 33-45sctp sport { 33, 55, 67, 88 }sctp sport { 33-55 }sctp sport vmap { 25:accept, 28:drop }sctp sport 1024 sctp dport 22
checksum <checksum>Checksum
sctp checksum 22sctp checksum!= 33-45sctp checksum { 33, 55, 67, 88 }sctp checksum { 33-55 }
vtag <tag>Verification tag
sctp vtag 22sctp vtag!= 33-45sctp vtag { 33, 55, 67, 88 }sctp vtag { 33-55 }
chunk <type>Existence of a chunk with given type in packet
sctp chunk init existssctp chunk error missing
chunk <type> <field>A chunk's field value (implies chunk existence)<sourcex lang="bash">

sctp chunk init flags 0x1sctp chunk data tsn 0x23</source>

Dccp

dccp match
dport <destination port>Destination port
dccp dport 22dccp dport!= 33-45dccp dport { 33-55 }dccp dport { telnet, http, https }dccp dport vmap { 22: accept, 23: drop }dccp dport vmap { 25:accept, 28:drop }
sport < source port>Source port
dccp sport 22dccp sport!= 33-45dccp sport { 33, 55, 67, 88 }dccp sport { 33-55 }dccp sport vmap { 25:accept, 28:drop }dccp sport 1024 dccp dport 22
type <type>Type of packet
dccp type { request, response, data, ack, dataack, closereq, close, reset, sync, syncack }dccp type requestdccp type!= request

Ah

ah match
hdrlength <length>AH header length
ah hdrlength 11-23ah hdrlength!= 11-23ah hdrlength { 11, 23, 44 }
reserved <value>
ah reserved 22ah reserved!= 33-45ah reserved { 23, 100 }ah reserved { 33-55 }
spi <value>
ah spi 111ah spi!= 111-222ah spi { 111, 122 }
sequence <sequence>Sequence Number
ah sequence 123ah sequence { 23, 25, 33 }ah sequence!= 23-33

Esp

esp match
spi <value>
esp spi 111esp spi!= 111-222esp spi { 111, 122 }
sequence <sequence>Sequence Number
esp sequence 123esp sequence { 23, 25, 33 }esp sequence!= 23-33

Comp

comp match
nexthdr <protocol>Next header protocol (Upper layer protocol)
comp nexthdr!= espcomp nexthdr { esp, ah, comp, udp, udplite, tcp, tcp, dccp, sctp }
flags <flags>Flags
comp flags 0x0comp flags!= 0x33-0x45comp flags { 0x33, 0x55, 0x67, 0x88 }
cpi <value>Compression Parameter Index
comp cpi 22comp cpi!= 33-45comp cpi { 33, 55, 67, 88 }

Icmp

icmp match
type <type>ICMP packet type
icmp type { echo-reply, destination-unreachable, source-quench, redirect, echo-request, time-exceeded, parameter-problem, timestamp-request, timestamp-reply, info-request, info-reply, address-mask-request, address-mask-reply, router-advertisem*nt, router-solicitation }
code ICMP packet code
icmp code 111icmp code!= 33-55icmp code { 2, 4, 54, 33, 56 }
checksum <value>ICMP packet checksum
icmp checksum 12343icmp checksum!= 11-343icmp checksum { 1111, 222, 343 }
id <value>ICMP packet id
icmp id 12343icmp id!= 11-343icmp id { 1111, 222, 343 }
sequence <value>ICMP packet sequence
icmp sequence 12343icmp sequence!= 11-343icmp sequence { 1111, 222, 343 }
mtu <value>ICMP packet mtu
icmp mtu 12343icmp mtu!= 11-343icmp mtu { 1111, 222, 343 }
gateway <value>ICMP packet gateway
icmp gateway 12343icmp gateway!= 11-343icmp gateway { 1111, 222, 343 }

Icmpv6

icmpv6 match
type <type>ICMPv6 packet type
icmpv6 type { destination-unreachable, packet-too-big, time-exceeded, echo-request, echo-reply, mld-listener-query, mld-listener-report, mld-listener-reduction, nd-router-solicit, nd-router-advert, nd-neighbor-solicit, nd-neighbor-advert, parameter-problem, mld2-listener-report }
code ICMPv6 packet code
icmpv6 code 4icmpv6 code 3-66icmpv6 code { 5, 6, 7 }
checksum <value>ICMPv6 packet checksum
icmpv6 checksum 12343icmpv6 checksum!= 11-343icmpv6 checksum { 1111, 222, 343 }
id <value>ICMPv6 packet id
icmpv6 id 12343icmpv6 id!= 11-343icmpv6 id { 1111, 222, 343 }
sequence <value>ICMPv6 packet sequence
icmpv6 sequence 12343icmpv6 sequence!= 11-343icmpv6 sequence { 1111, 222, 343 }
mtu <value>ICMPv6 packet mtu
icmpv6 mtu 12343icmpv6 mtu!= 11-343icmpv6 mtu { 1111, 222, 343 }
max-delay <value>ICMPv6 packet max delay
icmpv6 max-delay 33-45icmpv6 max-delay!= 33-45icmpv6 max-delay { 33, 55, 67, 88 }

Ether

ether match
saddr <mac address>Source mac address
ether saddr 00:0f:54:0c:11:04
type <type>
ether type vlan

Dst

dst match
nexthdr <proto>Next protocol header
dst nexthdr { udplite, ipcomp, udp, ah, sctp, esp, dccp, tcp, ipv6-icmp }dst nexthdr 22dst nexthdr!= 33-45
hdrlength <length>Header Length
dst hdrlength 22dst hdrlength!= 33-45dst hdrlength { 33, 55, 67, 88 }

Frag

frag match
nexthdr <proto>Next protocol header
frag nexthdr { udplite, comp, udp, ah, sctp, esp, dccp, tcp, ipv6-icmp, icmp }frag nexthdr 6frag nexthdr!= 50-51
reserved <value>
frag reserved 22frag reserved!= 33-45frag reserved { 33, 55, 67, 88 }
frag-off <value>
frag frag-off 22frag frag-off!= 33-45frag frag-off { 33, 55, 67, 88 }
more-fragments <value>
frag more-fragments 0frag more-fragments 0
id <value>
frag id 1frag id 33-45

Hbh

hbh match
nexthdr <proto>Next protocol header
hbh nexthdr { udplite, comp, udp, ah, sctp, esp, dccp, tcp, icmpv6 }hbh nexthdr 22hbh nexthdr!= 33-45
hdrlength <length>Header Length
hbh hdrlength 22hbh hdrlength!= 33-45hbh hdrlength { 33, 55, 67, 88 }

Mh

mh match
nexthdr <proto>Next protocol header
mh nexthdr { udplite, ipcomp, udp, ah, sctp, esp, dccp, tcp, ipv6-icmp }mh nexthdr 22mh nexthdr!= 33-45
hdrlength <length>Header Length
mh hdrlength 22mh hdrlength!= 33-45mh hdrlength { 33, 55, 67, 88 }
type <type>
mh type { binding-refresh-request, home-test-init, careof-test-init, home-test, careof-test, binding-update, binding-acknowledgement, binding-error, fast-binding-update, fast-binding-acknowledgement, fast-binding-advertisem*nt, experimental-mobility-header, home-agent-switch-message }mh type home-agent-switch-messagemh type!= home-agent-switch-message
reserved <value>
mh reserved 22mh reserved!= 33-45mh reserved { 33, 55, 67, 88 }
checksum <value>
mh checksum 22mh checksum!= 33-45mh checksum { 33, 55, 67, 88 }

Rt

rt match
nexthdr <proto>Next protocol header
rt nexthdr { udplite, ipcomp, udp, ah, sctp, esp, dccp, tcp, ipv6-icmp }rt nexthdr 22rt nexthdr!= 33-45
hdrlength <length>Header Length
rt hdrlength 22rt hdrlength!= 33-45rt hdrlength { 33, 55, 67, 88 }
type <type>
rt type 22rt type!= 33-45rt type { 33, 55, 67, 88 }
seg-left <value>
rt seg-left 22rt seg-left!= 33-45rt seg-left { 33, 55, 67, 88 }

Vlan

vlan match
id <value>Vlan tag ID
vlan id 4094vlan id 0
cfi <value>
vlan cfi 0vlan cfi 1
pcp <value>
vlan pcp 7vlan pcp 3

Arp

arp match
ptype <value>Payload type
arp ptype 0x0800
htype <value>Header type
arp htype 1arp htype!= 33-45arp htype { 33, 55, 67, 88 }
hlen <length>Header Length
arp hlen 1arp hlen!= 33-45arp hlen { 33, 55, 67, 88 }
plen <length>Payload length
arp plen 1arp plen!= 33-45arp plen { 33, 55, 67, 88 }
operation <value>
arp operation { nak, inreply, inrequest, rreply, rrequest, reply, request }

Ct

ct match
state <state>State of the connection
ct state { new, established, related, untracked }ct state!= relatedct state establishedct state 8
direction <value>Direction of the packet relative to the connection
ct direction originalct direction!= originalct direction { reply, original }
status <status>Status of the connection
ct status expected(ct status & expected)!= expectedct status { expected, seen-reply, assured, confirmed, snat, dnat, dying }
mark [set] Mark of the connection
ct mark 0ct mark or 0x23 == 0x11ct mark or 0x3!= 0x1ct mark and 0x23 == 0x11ct mark and 0x3!= 0x1ct mark xor 0x23 == 0x11ct mark xor 0x3!= 0x1ct mark 0x00000032ct mark!= 0x00000032ct mark 0x00000032-0x00000045ct mark!= 0x00000032-0x00000045ct mark { 0x32, 0x2222, 0x42de3 }ct mark { 0x32-0x2222, 0x4444-0x42de3 }ct mark set 0x11 xor 0x1331ct mark set 0x11333 and 0x11ct mark set 0x12 or 0x11ct mark set 0x11ct mark set markct mark set mark map { 1: 10, 2: 20, 3: 30 }
expiration Connection expiration time
ct expiration 30ct expiration 30sct expiration!= 233ct expiration!= 3m53sct expiration 33-45ct expiration 33s-45sct expiration!= 33-45ct expiration!= 33s-45sct expiration { 33, 55, 67, 88 }ct expiration { 1m7s, 33s, 55s, 1m28s }
helper "<helper>"Helper associated with the connection
ct helper "ftp"
[original | reply] bytes <value>
ct original bytes > 100000ct bytes > 100000
[original | reply] packets <value>
ct reply packets < 100
[original | reply] ip saddr <ip source address>
ct original ip saddr 192.168.0.1ct reply ip saddr 192.168.0.1ct original ip saddr 192.168.1.0/24ct reply ip saddr 192.168.1.0/24
[original | reply] ip daddr <ip destination address>
ct original ip daddr 192.168.0.1ct reply ip daddr 192.168.0.1ct original ip daddr 192.168.1.0/24ct reply ip daddr 192.168.1.0/24
[original | reply] l3proto <protocol>
ct original l3proto ipv4
[original | reply] protocol <protocol>
ct original protocol 6
[original | reply] proto-dst <port>
ct original proto-dst 22
[original | reply] proto-src <port>
ct reply proto-src 53
count [over] <number of connections>
ct count over 2tcp dport 22 add @ssh_flood { ip saddr ct count over 2 } reject[ which requires an existing ssh_flood set, ie. add set filter ssh_flood { type ipv4_addr; flags dynamic; } ]

Meta

meta matches packet by metainformation.

meta match
iifname <input interface name>Input interface name
meta iifname "eth0"meta iifname!= "eth0"meta iifname { "eth0", "lo" }meta iifname "eth*"
oifname <output interface name>Output interface name
meta oifname "eth0"meta oifname!= "eth0"meta oifname { "eth0", "lo" }meta oifname "eth*"
iif <input interface index>Input interface index
meta iif eth0meta iif!= eth0
oif <output interface index>Output interface index
meta oif lometa oif!= lometa oif { eth0, lo }
iiftype <input interface type>Input interface type
meta iiftype { ether, ppp, ipip, ipip6, loopback, sit, ipgre }meta iiftype!= ethermeta iiftype ether
oiftype <output interface type>Output interface hardware type
meta oiftype { ether, ppp, ipip, ipip6, loopback, sit, ipgre }meta oiftype!= ethermeta oiftype ether
length <length>Length of the packet in bytes
meta length 1000meta length!= 1000meta length > 1000meta length 33-45meta length!= 33-45meta length { 33, 55, 67, 88 }meta length { 33-55, 67-88 }
protocol <protocol>ethertype protocol
meta protocol ipmeta protocol!= ipmeta protocol { ip, arp, ip6, vlan }
nfproto <protocol>
meta nfproto ipv4meta nfproto!= ipv6meta nfproto { ipv4, ipv6 }
l4proto <protocol>
meta l4proto 22meta l4proto!= 233meta l4proto 33-45meta l4proto { 33, 55, 67, 88 }meta l4proto { 33-55 }
mark [set] Packet mark
meta mark 0x4meta mark 0x00000032meta mark and 0x03 == 0x01meta mark and 0x03!= 0x01meta mark!= 0x10meta mark or 0x03 == 0x01meta mark or 0x03!= 0x01meta mark xor 0x03 == 0x01meta mark xor 0x03!= 0x01meta mark set 0xffffffc8 xor 0x16meta mark set 0x16 and 0x16meta mark set 0xffffffe9 or 0x16meta mark set 0xffffffde and 0x16meta mark set 0x32 or 0xfffffmeta mark set 0xfffe xor 0x16
priority [set] <priority>tc class id
meta priority nonemeta priority 0x1:0x1meta priority 0x1:0xffffmeta priority 0xffff:0xffffmeta priority set 0x1:0x1meta priority set 0x1:0xffffmeta priority set 0xffff:0xffff
skuid <user id>UID associated with originating socket
meta skuid { bin, root, daemon }meta skuid rootmeta skuid!= rootmeta skuid lt 3000meta skuid gt 3000meta skuid eq 3000meta skuid 3001-3005meta skuid!= 2001-2005meta skuid { 2001-2005 }
skgid <group id>GID associated with originating socket
meta skgid { bin, root, daemon }meta skgid rootmeta skgid!= rootmeta skgid lt 3000meta skgid gt 3000meta skgid eq 3000meta skgid 3001-3005meta skgid!= 2001-2005meta skgid { 2001-2005 }
rtclassid <class>Routing realm
meta rtclassid cosmos
pkttype <type>Packet type
meta pkttype broadcastmeta pkttype!= broadcastmeta pkttype { broadcast, unicast, multicast }
cpu <cpu index>CPU ID
meta cpu 1meta cpu!= 1meta cpu 1-3meta cpu!= 1-2meta cpu { 2,3 }meta cpu { 2-3, 5-7 }
iifgroup <input group>Input interface group
meta iifgroup 0meta iifgroup!= 0meta iifgroup defaultmeta iifgroup!= defaultmeta iifgroup { default }meta iifgroup { 11,33 }meta iifgroup { 11-33 }
oifgroup <group>Output interface group
meta oifgroup 0meta oifgroup!= 0meta oifgroup defaultmeta oifgroup!= defaultmeta oifgroup { default }meta oifgroup { 11,33 }meta oifgroup { 11-33 }
cgroup <group>
meta cgroup 1048577meta cgroup!= 1048577meta cgroup { 1048577, 1048578 }meta cgroup 1048577-1048578meta cgroup!= 1048577-1048578meta cgroup { 1048577-1048578 }

Statements

statement is the action performed when the packet match the rule. It could be terminal and non-terminal. In a certain rule we can consider several non-terminal statements but only a single terminal statement.

Verdict statements

The verdict statement alters control flow in the ruleset and issues policy decisions for packets. The valid verdict statements are:

  • accept: Accept the packet and stop the remain rules evaluation.
  • drop: Drop the packet and stop the remain rules evaluation.
  • queue: Queue the packet to userspace and stop the remain rules evaluation.
  • continue: Continue the ruleset evaluation with the next rule.
  • return: Return from the current chain and continue at the next rule of the last chain. In a base chain it is equivalent to accept
  • jump <chain>: Continue at the first rule of <chain>. It will continue at the next rule after a return statement is issued
  • goto <chain>: Similar to jump, but after the new chain the evaluation will continue at the last chain instead of the one containing the goto statement

Log

log statement
level [over] <value> <unit> [burst <value> <unit>]Log level
loglog level emerglog level alertlog level critlog level errlog level warnlog level noticelog level infolog level debug
group <value> [queue-threshold <value>] [snaplen <value>] [prefix "<prefix>"]
log prefix aaaaa-aaaaaa group 2 snaplen 33log group 2 queue-threshold 2log group 2 snaplen 33

Reject

The default reject will be the ICMP type port-unreachable. The icmpx is only used for inet family support.

More information on the Rejecting_traffic page.

reject statement
with <protocol> type <type>
rejectreject with icmp type host-unreachablereject with icmp type net-unreachablereject with icmp type prot-unreachablereject with icmp type port-unreachablereject with icmp type net-prohibitedreject with icmp type host-prohibitedreject with icmp type admin-prohibitedreject with icmpv6 type no-routereject with icmpv6 type admin-prohibitedreject with icmpv6 type addr-unreachablereject with icmpv6 type port-unreachablereject with icmpx type host-unreachablereject with icmpx type no-routereject with icmpx type admin-prohibitedreject with icmpx type port-unreachableip protocol tcp reject with tcp reset

Counter

counter statement
packets <packets> bytes <bytes>
countercounter packets 0 bytes 0

Limit

limit statement
rate [over] <value> <unit> [burst <value> <unit>]Rate limit
limit rate 400/minutelimit rate 400/hourlimit rate over 40/daylimit rate over 400/weeklimit rate over 1023/second burst 10 packetslimit rate 1025 kbytes/secondlimit rate 1023000 mbytes/secondlimit rate 1025 bytes/second burst 512 byteslimit rate 1025 kbytes/second burst 1023 kbyteslimit rate 1025 mbytes/second burst 1025 kbyteslimit rate 1025000 mbytes/second burst 1023 mbytes

Nat

nat statement
dnat to <destination address>Destination address translation
dnat to 192.168.3.2dnat to ct mark map { 0x00000014: 1.2.3.4 }
snat to <ip source address>Source address translation
snat to 192.168.3.2snat to 2001:838:35f:1::-2001:838:35f:2:::100
masquerade [<type>] [to:<port>]Masquerade
masquerademasquerade persistent,fully-random,randommasquerade to :1024masquerade to :1024-2048

Queue

queue statement
num <value> <scheduler>
queuequeue num 2queue num 2-3queue num 4-5 fanout bypassqueue num 4-5 fanoutqueue num 4-5 bypass

Extras

Export Configuration

% nft export (xml | json)

Monitor Events

Monitor events from Netlink creating filters.

% nft monitor [new | destroy] [tables | chains | sets | rules | elements] [xml | json]

List ruleset

% nft list ruleset

Flush ruleset

% nft flush ruleset

Load ruleset

Create a command batch file and load it with the nft interpreter,

% echo "flush ruleset" > /etc/nftables.rules% echo "add table filter" >> /etc/nftables.rules% echo "add chain filter input" >> /etc/nftables.rules% echo "add rule filter input meta iifname lo accept" >> /etc/nftables.rules% nft -f /etc/nftables.rules

or create an executable nft script file,

% cat << EOF > /etc/nftables.rules> #!/usr/local/sbin/nft -f> flush ruleset> add table filter> add chain filter input> add rule filter input meta iifname lo accept> EOF% chmod u+x /etc/nftables.rules% /etc/nftables.rules

or create an executable nft script file from an already created ruleset,

% nft list ruleset > /etc/nftables.rules% nft flush ruleset% nft -f /etc/nftables.rules

Simple IP/IPv6 Firewall

flush rulesettable firewall { chain incoming { type filter hook input priority 0; policy drop; # established/related connections ct state established,related accept # loopback interface iifname lo accept # icmp icmp type echo-request accept # open tcp ports: sshd (22), httpd (80) tcp dport { ssh, http } accept }}table ip6 firewall { chain incoming { type filter hook input priority 0; policy drop; # established/related connections ct state established,related accept # invalid connections ct state invalid drop # loopback interface iifname lo accept # icmp # routers may also want: mld-listener-query, nd-router-solicit icmpv6 type { echo-request, nd-neighbor-solicit } accept # open tcp ports: sshd (22), httpd (80) tcp dport { ssh, http } accept }}
Quick reference-nftables in 10 minutes (2024)

FAQs

What is the priority rule in nftables? ›

The nftables wiki has to say: The priority can be used to order the chains or to put them before or after some Netfilter internal operations. For example, a chain on the prerouting hook with the priority -300 will be placed before connection tracking operations.

What is the default table in nftables? ›

Nftables doesn't have any default tables. Without a table being built, nftables will not filter network traffic. Note: adding rules to a running nftables can cause loss of connectivity to the system.

What is the default family in nftables? ›

ip (i.e. IPv4) is the default family and will be used if family is not specified.

How to write nftables rules? ›

Basic Configuration. Initially, nftables starts with a completely empty ruleset; there are no predefined tables, chains, or rules. As a user (or admin), you first create the tables, add chains to the tables that hook into the Linux kernel as netfilter hooks, and then fill the chains with the appropriate rules.

Why is nftables better than iptables? ›

nftables provides a simpler, more efficient alternative to iptables, with unified IPv4/IPv6 handling. Features like rule tracing and multi-action rules in nftables enhance network management. Transitioning to nftables offers better performance and simplicity, supported by tools like iptables-translate.

What priority rule is best overall? ›

Minimum slack rule:

In the minimum slack rule, when two or more tasks compete with each other, one should choose the task with minimum total slack. and the best overall priority rule should be based on the minimum slack rule.

Does nftables use netfilter? ›

nftables utilizes the building blocks of the Netfilter infrastructure, such as the existing hooks into the networking stack, connection tracking system, userspace queueing component, and logging subsystem.

What does nftables stand for? ›

#netfilter (webchat) 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 iptables/ip6tables and framework.

Does fail2ban work with nftables? ›

A table for fail2ban needs to be added to nftables. An ip table is used to support both IPv4 and IPv6. The priority for the table is set high to reject connections before other rules are evaluated.

Where are nftables rules stored? ›

By default, rules are located in /etc/nftables.

The Debian nftables package comes with example rule sets at /usr/share/doc/nftables/examples/ .

How to delete nftables rule? ›

The actual syntax is quite logical:
  1. to remove everything nft flush ruleset.
  2. to empty a table (with ip as family by default if not specified). ...
  3. to delete a table (which also empties it first). ...
  4. to empty a chain (ditto). ...
  5. to delete a chain (ditto). ...
  6. to delete a rule (this can still be done only by the handle reference).
Jan 19, 2020

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.

What is the rule of priority? ›

The principle of priority states simply that the earliest name applied properly to a taxon of animals is the correct scientific name, with the date of publication determined by the stated date on the publication or by other means if that information is not reliable.

What is the priority sequence rule? ›

Priority Sequencing Rules provide the guidance for the order in which the jobs are to be processed at a workstation. The application of different priority rules in job shop scheduling gives different order of scheduling.

What is the criteria of priority rules? ›

PRIORITY SCHEDULING RULES
  • SPT (shortest processing time). The job with the shortest processing time is processed first. ...
  • EDD (earliest due date). This rule is useful when the goal is to reduce job lateness. ...
  • CR (critical ratio). ...
  • FOR (fewest operations remaining). ...
  • ST (slack time). ...
  • NQ (next queue).

What is the priority of Openflow rule? ›

Priority—Matching precedence of the flow entry. When a packet is matched with the flow table, only the highest priority flow entry that matches the packet is selected. Counters—Counts of the packets and bytes that match the flow entry.

Top Articles
Amtrak 174 Status
Today Was A Good Day With Lyrics
Joi Databas
Le Blanc Los Cabos - Los Cabos – Le Blanc Spa Resort Adults-Only All Inclusive
Craigslist Vans
How To Be A Reseller: Heather Hooks Is Hooked On Pickin’ - Seeking Connection: Life Is Like A Crossword Puzzle
35105N Sap 5 50 W Nit
Tx Rrc Drilling Permit Query
Betonnen afdekplaten (schoorsteenplaten) ter voorkoming van lekkage schoorsteen. - HeBlad
Guidewheel lands $9M Series A-1 for SaaS that boosts manufacturing and trims carbon emissions | TechCrunch
Crossword Nexus Solver
Dr Manish Patel Mooresville Nc
What Happened To Anna Citron Lansky
Dr Adj Redist Cadv Prin Amex Charge
Nhl Wikia
Divina Rapsing
Joann Ally Employee Portal
Mccain Agportal
Schedule An Oil Change At Walmart
Tyler Sis University City
The Ultimate Guide to Extras Casting: Everything You Need to Know - MyCastingFile
The BEST Soft and Chewy Sugar Cookie Recipe
Amazing Lash Studio Casa Linda
8005607994
Piedmont Healthstream Sign In
Random Bibleizer
Mikayla Campinos: Unveiling The Truth Behind The Leaked Content
NV Energy issues outage watch for South Carson City, Genoa and Glenbrook
Rs3 Bring Leela To The Tomb
Happy Shuttle Cancun Review
Ezstub Cross Country
The Menu Showtimes Near Amc Classic Pekin 14
Japanese Pokémon Cards vs English Pokémon Cards
Lake Dunson Robertson Funeral Home Lagrange Georgia Obituary
Craigs List Jonesboro Ar
Mydocbill.com/Mr
Shih Tzu dogs for sale in Ireland
Bitchinbubba Face
Craigslist Jobs Brownsville Tx
Search All of Craigslist: A Comprehensive Guide - First Republic Craigslist
About My Father Showtimes Near Amc Rockford 16
Cnp Tx Venmo
Weekly Math Review Q2 7 Answer Key
Guided Practice Activities 5B-1 Answers
Best Conjuration Spell In Skyrim
Iupui Course Search
Craiglist.nj
Dcuo Wiki
Lake County Fl Trash Pickup Schedule
Inloggen bij AH Sam - E-Overheid
All Obituaries | Roberts Funeral Home | Logan OH funeral home and cremation
Craigslist Centre Alabama
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 5637

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.