nftables Firewall Basics on Linux (Replace iptables)

nftables firewall linux linux-nftables.png

RHEL-family distros now default to nftables instead of legacy iptables. This guide builds a minimal host firewall that drops inbound traffic except SSH and web ports.

Step 1: Install and Enable nftables

Install with dnf install nftables and enable with systemctl enable --now nftables.

install enable nftables service linux
Step 1: Install and Enable nftables

Step 2: Write a Basic Ruleset

Create /etc/nftables/main.nft with an inet filter table, input chain, and policy drop.

nftables main.nft input chain linux
Step 2: Write a Basic Ruleset

Step 3: Load Rules and Verify

Run nft -f /etc/nftables/main.nft and check with nft list ruleset. Test SSH from another host before closing your session.

nft list ruleset verify firewall linux
Step 3: Load Rules and Verify

Minimal ruleset

table inet filter {
  chain input {
    type filter hook input priority 0; policy drop;
    ct state established,related accept
    iif lo accept
    tcp dport { 22, 80, 443 } accept
  }
}

Related tutorials

Terminal screenshots are original illustrations created for Gnome IT Solutions (blog.gnomeitsolutions.com).