Build a Safe nftables Host Firewall Baseline
Before you begin
- Applies to:Linux hosts using nftables. Practice applying the example on a disposable VM.
- Where to start:Start at the test VM console. On a managed host, identify the firewall owner and its existing manager before planning changes.
- What you need:Have an approved list of allowed traffic, administrator access, and a tested recovery path before loading rules.
Guide tools
nftables filters network traffic in the Linux kernel. This guide demonstrates how to inspect the current rules and stage a small host-filter policy. The example is a template for an isolated test VM, not a universal production firewall; an incorrect default-drop policy can cut off SSH and other required traffic.
Identify the Firewall Manager and Current Rules
Read FirstCheck whether nftables, firewalld, UFW, Docker, Kubernetes, or a configuration agent owns the active rules. Do not manage the same ruleset independently from multiple tools. Save a protected copy for review and confirm you have console or out-of-band access before firewall changes.
sudo nft list rulesetsystemctl is-active nftables firewalld ufw 2>/dev/nullsnapshot="$HOME/nft-ruleset-before.txt"test ! -e "$snapshot" || { echo 'Snapshot file already exists; choose a new name.' >&2; exit 1; }test ! -L "$snapshot" || { echo 'Snapshot path is a symlink; inspect it before continuing.' >&2; exit 1; }(umask 077; sudo nft list ruleset > "$snapshot") || exit 1β― Illustrative Output (Results Vary)
table inet filter { chain input { type filter hook input priority filter; policy accept; }}Write a Policy for a Dedicated Test Host
Policy DesignDecide what inbound services the host needs, which management networks may reach it, whether IPv6 is enabled, and what ICMP/ICMPv6 traffic operations require. Replace documentation CIDRs and port numbers with the exact approved values. This sample accepts loopback, established connections, ping, and SSH from a documentation-only IPv4 management subnet.
table inet host_filter { chain input { type filter hook input priority filter; policy drop; iifname "lo" accept ct state established,related accept ip protocol icmp accept meta l4proto ipv6-icmp accept ip saddr 192.0.2.0/24 tcp dport 22 ct state new accept }}β― Illustrative Output (Results Vary)
This example allows new SSH connections only from 192.0.2.0/24.It does not include application ports or an IPv6 SSH management source.
Terminal: Inspect the loaded input-chain policy and the explicitly allowed traffic.
Stage and Syntax-Check the Rules
No-Apply ValidationWrite the policy to a separate file and ask nftables to validate it without applying it. Syntax validation cannot prove that the policy permits every service this host needs; review the effective ruleset and test plan as well.
sudo test ! -e /root/host-filter.nft || { echo 'Staging file already exists; inspect it or choose a new path.' >&2; exit 1; }sudo test ! -L /root/host-filter.nft || { echo 'Staging path is a symlink; inspect it before continuing.' >&2; exit 1; }sudo install -o root -g root -m 0600 /dev/null /root/host-filter.nftsudoedit /root/host-filter.nftsudo nft --check --file /root/host-filter.nftβ― Illustrative Output (Results Vary)
No output and a zero exit status indicate the rules parsed successfully.Apply Only with Console Access and a Recovery Plan
Controlled ChangeFor a disposable test VM with console access, apply the staged file and immediately verify SSH, DNS, monitoring, application ports, IPv4, and IPv6 as required. On production, coordinate with the firewall owner, pre-stage an independently verified recovery plan, and follow the hostβs existing firewall manager rather than layering a second policy.
# Test VM only, after confirming this dedicated table does not conflict:sudo nft --file /root/host-filter.nftsudo nft list table inet host_filtersudo nft list rulesetβ― Illustrative Output (Results Vary)
Confirm the expected chain hook, policy, and individual rules are loaded.Plan Persistence for the Installed Distribution
Boot BehaviorRuntime rules may not survive reboot. The nftables service and its configuration path vary by distribution and may conflict with firewalld or other managers. Inspect the local unit before enabling persistence and ensure the startup file contains the complete intended policy.
systemctl cat nftablessystemctl status nftables --no-pagerβ― Illustrative Output (Results Vary)
Read the unit's ExecStart and configuration path before enabling it.Further reading: nftables manual.
Your feedback keeps these production procedures accurate and battle-tested.