Guia

Um firewall nftables correto, incluindo IPv6

Intermediário15 min de leituraAtualizado 22 de abril de 2026
Resposta curta

O nftables substitui o iptables com um conjunto de regras unificado que cobre IPv4 e IPv6 juntos através da família inet. Uma política base correta bloqueia tráfego de entrada por predefinição, aceita ligações estabelecidas, permite loopback e ICMP, e abre apenas as portas que serve.

01 Porque a família inet importa

The most common firewall mistake on a modern VPS is a v4-only ruleset. Every OnionVPS instance has a routed IPv6 /64, so services remain fully reachable over v6 unless the ruleset covers both. The inet family covers both in one place.

02 Um conjunto de regras base completo

Write it to /etc/nftables.conf and enable the service. Keep a second session open while testing.

#!/usr/sbin/nft -f
flush ruleset

table inet filter {
  chain input {
    type filter hook input priority 0; policy drop;

    ct state established,related accept
    ct state invalid drop
    iif lo accept

    ip protocol icmp accept
    ip6 nexthdr icmpv6 accept

    tcp dport { 22, 80, 443 } accept
    udp dport 51820 accept          # WireGuard

    limit rate 5/minute burst 10 packets log prefix "nft-drop: "
  }
  chain forward { type filter hook forward priority 0; policy drop; }
  chain output  { type filter hook output  priority 0; policy accept; }
}

03 Aplique-o com segurança

Schedule a flush before applying, so a mistake resolves itself rather than requiring the console.

echo 'nft flush ruleset' | at now + 10 minutes
nft -f /etc/nftables.conf
# verify you are still connected, then:
atrm $(atq | cut -f1)
systemctl enable nftables

04 Nunca bloqueie ICMPv6

IPv6 depends on ICMPv6 for neighbour discovery and path MTU discovery. Blocking it wholesale, which people do out of IPv4 habit, produces intermittent failures that are extremely hard to diagnose.

Perguntas frequentes

Deveria usar nftables ou ufw?

O ufw é um front end mais amigável e lida com ambas as famílias de endereços corretamente por predefinição. Use nftables diretamente quando precisar de regras que o ufw não consegue expressar, como rate limiting ou NAT.

Como vejo o que está a ser bloqueado?

A regra de registo acima escreve no registo do kernel com um prefixo "nft-drop"; leia-a com journalctl -k -g nft-drop.