A persistent nftables set
Rules added with nft add live until a reboot. The persistent set is the
file /etc/nftables.conf, loaded by the nftables service
(ExecStart=/usr/sbin/nft -f /etc/nftables.conf). On the lab machine the
service is disabled, and the file from the package lets everything
through: its chains have no rules and no policy.
A basic "drop whatever is not allowed" set:
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority filter; policy drop;
iif lo accept
ct state established,related accept
meta l4proto { icmp, ipv6-icmp } accept
tcp dport { 22, 80 } accept
counter
}
}
| Line |
Why |
policy drop |
everything not allowed below is dropped |
iif lo accept |
the machine talks to itself: the database and admin panel on 127.0.0.1 keep working |
ct state established,related accept |
replies to connections the machine opened itself |
meta l4proto { icmp, ipv6-icmp } accept |
ping and ICMP service messages |
tcp dport { 22, 80 } accept |
the ports open to the outside, as a list |
counter |
how many packets reached the drop policy |
sudo nft -c -f /etc/nftables.conf
sudo systemctl enable --now nftables
sudo nft list ruleset
nft -c prints the error with its position in the line and exits with 1:
/tmp/bad.nft:1:54-54: Error: syntax error, unexpected '}'
table inet filter { chain input { tcp dport 22 acept } }
^
From outside — another network namespace or another machine — closed
ports show up as filtered: the packet is dropped with no answer (see
nmap):
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
5432/tcp filtered postgresql
8080/tcp filtered http-proxy
flush ruleset at the top of the file deletes every table, other
people's too: a table created by hand was gone after systemctl enable
--now nftables. The fail2ban and Docker tables come back after restart
nftables only when those services restart.