nftables Cheat Sheet¶
How to properly configure network filtering, manage firewall rulesets, and set up NAT using the nftables command-line utility.
1. General Ruleset Management¶
nft list ruleset — Display the entire active ruleset:
nft flush ruleset — Delete all tables, chains, and rules completely:
nft -f <file> — Load and apply a ruleset from a configuration file:
nft list ruleset > <file> — Save the current active ruleset to a file:
nft monitor — Monitor netlink events (rule additions/deletions) in real-time:
2. Table Management¶
Tables act as containers for chains, sets, and stateful objects.
nft add table <family> <table> — Create a new table (families: ip, ip6, inet, arp, bridge, netdev):
nft list table <family> <table> — List all chains and rules within a specific table:
nft delete table <family> <table> — Delete a table and all its contents:
nft flush table <family> <table> — Remove all rules from all chains within a table (keeps the table and chains):
3. Chain Management¶
Chains are containers for rules and can be base (hooked into kernel) or regular.
nft add chain <family> <table> <chain> — Create a regular (non-base) chain:
nft add chain <family> <table> <chain> { type <type> hook <hook> priority <prio> ; } — Create a base chain attached to a specific networking hook:
nft list chain <family> <table> <chain> — List all rules within a specific chain:
nft delete chain <family> <table> <chain> — Delete an empty chain:
nft flush chain <family> <table> <chain> — Delete all rules within a specific chain:
4. Rule Management¶
Rules define the match criteria and the action to take.
nft add rule <family> <table> <chain> <matches> <statement> — Append a rule to the end of a chain:
nft insert rule <family> <table> <chain> <matches> <statement> — Insert a rule at the beginning of a chain:
nft list chain -a <family> <table> <chain> — List rules in a chain with their numerical handles:
nft delete rule <family> <table> <chain> handle <handle> — Delete a specific rule by its handle number:
nft replace rule <family> <table> <chain> handle <handle> <new_rule> — Replace an existing rule in-place by its handle:
5. Sets and Maps¶
Sets allow matching multiple values efficiently; Maps map a matched value to a specific verdict or data.
nft add set <family> <table> <set> { type <type>; } — Create a named set (types: ipv4_addr, ipv6_addr, inet_service, etc.):
nft list set <family> <table> <set> — Display the contents of a set:
nft add element <family> <table> <set> { <elements> } — Add elements to an existing set:
nft delete element <family> <table> <set> { <elements> } — Remove specific elements from a set:
nft delete set <family> <table> <set> — Delete a set entirely:
6. Common Rule Examples¶
Port Filtering¶
Allow incoming SSH (port 22):
Allow incoming HTTP & HTTPS:
Drop incoming Telnet (port 23):
IP/Network Filtering¶
Drop specific source IP:
Accept entire subnet:
Match multiple IPs using anonymous set:
Stateful Tracking¶
Allow established/related traffic:
Drop invalid packets:
Network Address Translation (NAT)¶
Setup SNAT (Masquerade for outbound):
Setup DNAT (Port forwarding):