Configure nftables NAT and port forwarding for home lab environments

Intermediate 25 min Apr 05, 2026 145 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Learn how to configure nftables for NAT and port forwarding in home lab environments. This tutorial covers basic NAT masquerading, port forwarding rules, and advanced networking scenarios for virtual machines and containers.

Prerequisites

  • Root or sudo access
  • Basic understanding of networking concepts
  • Multiple network interfaces or VLANs configured

What this solves

Home labs often need Network Address Translation (NAT) and port forwarding to allow virtual machines, containers, and services to communicate with external networks while maintaining security. This tutorial shows you how to configure nftables to provide internet access to your lab infrastructure and expose specific services through port forwarding rules.

Step-by-step installation

Update system and install nftables

Start by updating your system packages and installing nftables along with its documentation.

sudo apt update && sudo apt upgrade -y
sudo apt install -y nftables
sudo dnf update -y
sudo dnf install -y nftables

Stop and disable conflicting firewalls

Disable other firewall services that might conflict with nftables configuration. This ensures nftables has full control over packet filtering and NAT rules.

sudo systemctl stop ufw
sudo systemctl disable ufw
sudo systemctl stop iptables
sudo systemctl disable iptables
sudo systemctl stop firewalld
sudo systemctl disable firewalld
sudo systemctl stop iptables
sudo systemctl disable iptables

Enable IP forwarding

Configure the kernel to allow packet forwarding between network interfaces, which is essential for NAT functionality.

echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv6.conf.all.forwarding = 1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Create basic nftables configuration

Create a comprehensive nftables configuration file that includes NAT, port forwarding, and basic security rules for home lab environments.

#!/usr/sbin/nft -f

Clear existing rules

flush ruleset

Define variables for common interfaces and networks

define WAN_IF = "eth0" # Replace with your WAN interface define LAN_IF = "eth1" # Replace with your LAN interface define LAN_NET = "192.168.1.0/24" # Replace with your LAN network define HOMELAB_NET = "10.0.0.0/24" # Replace with your lab network

Main filter table for firewall rules

table inet filter { chain input { type filter hook input priority filter policy drop # Allow loopback iif lo accept # Allow established and related connections ct state established,related accept # Allow ICMP/ICMPv6 ip protocol icmp accept ip6 nexthdr icmpv6 accept # Allow SSH from LAN and homelab networks iifname { $LAN_IF } ip saddr { $LAN_NET, $HOMELAB_NET } tcp dport 22 accept # Allow DNS from local networks iifname { $LAN_IF } ip saddr { $LAN_NET, $HOMELAB_NET } udp dport 53 accept iifname { $LAN_IF } ip saddr { $LAN_NET, $HOMELAB_NET } tcp dport 53 accept # Drop everything else counter drop } chain forward { type filter hook forward priority filter policy drop # Allow established and related connections ct state established,related accept # Allow forwarding from LAN to WAN iifname $LAN_IF oifname $WAN_IF ip saddr $LAN_NET accept # Allow forwarding from homelab to WAN iifname $LAN_IF oifname $WAN_IF ip saddr $HOMELAB_NET accept # Allow forwarding between lab networks iifname $LAN_IF oifname $LAN_IF ip saddr { $LAN_NET, $HOMELAB_NET } ip daddr { $LAN_NET, $HOMELAB_NET } accept # Drop everything else counter drop } chain output { type filter hook output priority filter policy accept } }

NAT table for address translation

table inet nat { chain prerouting { type nat hook prerouting priority dstnat # Port forwarding rules for home lab services # Web server on 10.0.0.10:80 -> external port 8080 iifname $WAN_IF tcp dport 8080 dnat to 10.0.0.10:80 # SSH to lab server on 10.0.0.20:22 -> external port 2222 iifname $WAN_IF tcp dport 2222 dnat to 10.0.0.20:22 # HTTPS service on 10.0.0.10:443 -> external port 8443 iifname $WAN_IF tcp dport 8443 dnat to 10.0.0.10:443 } chain postrouting { type nat hook postrouting priority srcnat # Masquerade traffic from LAN networks to WAN oifname $WAN_IF ip saddr { $LAN_NET, $HOMELAB_NET } masquerade } }

Identify your network interfaces

Determine the correct interface names for your system to update the configuration variables.

ip link show
ip route show default

Update interface variables

Edit the nftables configuration to match your actual network interface names and IP ranges.

sudo nano /etc/nftables.conf
Note: Replace eth0 with your WAN interface (usually the one with internet access), eth1 with your LAN interface, and update the network ranges to match your actual subnets.

Load and test the configuration

Load the nftables rules and verify they are working correctly before making them permanent.

sudo nft -f /etc/nftables.conf
sudo nft list ruleset

Enable nftables service

Enable the nftables service to automatically load rules on boot and start the service.

sudo systemctl enable nftables
sudo systemctl start nftables
sudo systemctl status nftables

Add additional port forwarding rules

Add more port forwarding rules for common home lab services like databases, monitoring tools, and development servers.

sudo nft add rule inet nat prerouting iifname "eth0" tcp dport 3306 dnat to 10.0.0.30:3306  # MySQL
sudo nft add rule inet nat prerouting iifname "eth0" tcp dport 5432 dnat to 10.0.0.30:5432  # PostgreSQL
sudo nft add rule inet nat prerouting iifname "eth0" tcp dport 9090 dnat to 10.0.0.40:9090  # Prometheus
sudo nft add rule inet nat prerouting iifname "eth0" tcp dport 3000 dnat to 10.0.0.40:3000  # Grafana

Save the updated configuration

Export the current ruleset to the configuration file to make the new rules persistent across reboots.

sudo nft list ruleset > /tmp/nftables_backup.conf
sudo cp /tmp/nftables_backup.conf /etc/nftables.conf

Configure advanced home lab scenarios

Create isolated lab network segment

Configure rules for an isolated lab segment that has limited internet access and strict inter-VLAN communication controls.

sudo nft add table inet lab_isolation
sudo nft add chain inet lab_isolation forward { type filter hook forward priority 0 \; }
sudo nft add rule inet lab_isolation forward iifname "lab0" oifname "lab1" drop
sudo nft add rule inet lab_isolation forward iifname "lab0" oifname "eth0" ip daddr { 8.8.8.8, 1.1.1.1 } accept
sudo nft add rule inet lab_isolation forward iifname "lab0" oifname "eth0" tcp dport { 80, 443 } accept

Configure load balancing for multiple lab servers

Set up simple round-robin load balancing for web services across multiple lab servers.

sudo nft add map inet nat lb_web { type mark : ipv4_addr \; }
sudo nft add element inet nat lb_web { 1 : 10.0.0.10, 2 : 10.0.0.11, 3 : 10.0.0.12 }
sudo nft add rule inet nat prerouting iifname "eth0" tcp dport 80 mark set numgen random mod 3 map { 0 : 1, 1 : 2, 2 : 3 }
sudo nft add rule inet nat prerouting iifname "eth0" tcp dport 80 dnat to mark map @lb_web

Set up bandwidth limiting

Configure basic traffic shaping to limit bandwidth for specific lab networks or services.

sudo nft add table inet qos
sudo nft add chain inet qos postrouting { type filter hook postrouting priority 0 \; }
sudo nft add rule inet qos postrouting oifname "eth0" ip saddr 10.0.0.0/24 limit rate 50 mbytes/second accept

Verify your setup

Test that NAT, port forwarding, and security rules are working correctly in your home lab environment.

# Check if rules are loaded
sudo nft list ruleset

Test NAT functionality from a lab machine

ping 8.8.8.8 curl -I http://example.com

Check if port forwarding is working

sudo ss -tlnp | grep :8080 nmap -p 8080 YOUR_WAN_IP

Verify IP forwarding is enabled

sysctl net.ipv4.ip_forward sysctl net.ipv6.conf.all.forwarding

Monitor packet flow

sudo nft monitor

Check connection tracking

cat /proc/net/nf_conntrack | head -10

Common issues

SymptomCauseFix
Lab VMs can't reach internetIP forwarding disabled or NAT masquerade not workingCheck sysctl net.ipv4.ip_forward and verify masquerade rule in postrouting chain
Port forwarding not workingIncorrect interface names or missing DNAT rulesVerify interface names with ip link and check prerouting DNAT rules
Connection timeouts from external clientsFirewall blocking forwarded connectionsAdd accept rules in forward chain for destination ports and IPs
nftables rules not persistent after rebootService not enabled or configuration not savedRun sudo systemctl enable nftables and save ruleset to /etc/nftables.conf
Inter-VLAN communication blockedForward chain dropping packets between lab networksAdd specific forward rules for allowed inter-network communication
DNS resolution fails in labDNS packets blocked by firewall rulesAllow UDP/TCP port 53 in input and forward chains for DNS servers

Next steps

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle private cloud infrastructure for businesses that depend on uptime. From initial setup to ongoing operations.