Networking Security Homelab
From a pile of iptables rules to one nftables file
My Proxmox host had grown a DNAT script, post-up hooks and six copies of the same masquerade rule. Part 1 of moving three bridges and a VPN to one declarative nftables file, and an honest list of what is left.
- nftables
- iptables
- Proxmox
- NAT
Every new service in my homelab added a port forward, and every port forward went into whatever mechanism was closest at hand: a shell script, a post-up line, a one-off command. When NexiCloud added a third bridge and the VPN needed to reach it, I could no longer say for sure what my own firewall did. Time for one declarative file.
Where I started
The host's NAT came from three places:
- A script,
apply-dnat.sh, reading a small config file ofexternal_port:ip:internal_portlines and creating DNAT rules. post-uplines onvmbr1in/etc/network/interfacesadding masquerade and FORWARD rules.- Commands typed once and saved with
netfilter-persistent.
CONF="/etc/dnat-rules.conf"
WAN="vmbr0"
iptables -t nat -F PREROUTING
while IFS=: read -r EXT_PORT CT_IP INT_PORT; do
[[ "$EXT_PORT" =~ ^#.*$ || -z "$EXT_PORT" ]] && continue
iptables -t nat -A PREROUTING -i "$WAN" -p tcp --dport "$EXT_PORT" -j DNAT --to-destination "${CT_IP}:${INT_PORT}"
iptables -t nat -A PREROUTING -i "$WAN" -p udp --dport "$EXT_PORT" -j DNAT --to-destination "${CT_IP}:${INT_PORT}"
done < "$CONF"It was a good idea at the time: adding a service was one line in a text file. But looking at the live rules showed the cost.
-A PREROUTING -i vmbr0 -p tcp --dport 81 -j DNAT --to-destination 192.168.1.12:81
-A PREROUTING -i vmbr0 -p udp --dport 81 -j DNAT --to-destination 192.168.1.12:81
-A PREROUTING -i vmbr0 -p tcp --dport 5380 -j DNAT --to-destination 192.168.1.11:5380
-A PREROUTING -i vmbr0 -p udp --dport 5380 -j DNAT --to-destination 192.168.1.11:5380
…
-A POSTROUTING -s 192.168.1.0/24 -o vmbr0 -j MASQUERADE
-A POSTROUTING -s 192.168.1.0/24 -o vmbr0 -j MASQUERADE
-A POSTROUTING -s 192.168.1.0/24 -o vmbr0 -j MASQUERADE
-A POSTROUTING -s 192.168.1.0/24 -o vmbr0 -j MASQUERADE
-A POSTROUTING -s 192.168.1.0/24 -o vmbr0 -j MASQUERADE
-A POSTROUTING -s 192.168.1.0/24 -o vmbr0 -j MASQUERADEpost-up appends, every time
post-up iptables -A … runs on every ifreload, not just at boot, and -A appends. Six reloads, six identical masquerade rules. Harmless here, but a sign that nothing was idempotent.
Every port forwarded twice
The script created a UDP rule for every TCP service: UDP 81, UDP 5380, UDP 8096… none of those services listen on UDP. Rules that do nothing are rules nobody reviews.
Admin UIs on the LAN
NPM's admin port (81) and Technitium's (5380) were forwarded to the whole LAN, when both are already reachable over HTTPS through the reverse proxy. Convenience had quietly widened the attack surface.
No filter at all
The FORWARD chain accepted everything. DNAT decided where packets went, but nothing decided whether they should.
What I wanted
- One file, versioned, that describes NAT and filtering for the three bridges.
- Atomic: it loads entirely or not at all. No half-applied state after a typo.
- Default deny from the LAN into the internal bridges, with explicit exceptions.
- Support for the VPN reaching NexiCloud: traffic arriving on vmbr1 from WireGuard and going to vmbr4.
The new ruleset
#!/usr/sbin/nft -f
flush ruleset
table ip nat {
chain prerouting {
type nat hook prerouting priority dstnat; policy accept;
# WireGuard
iifname "vmbr0" udp dport 51820 dnat to 192.168.1.10:51820
# NexiCloud, reachable from the LAN and from the VPN (which lands on vmbr1)
iifname { "vmbr0", "vmbr1" } tcp dport 8080 dnat to 172.16.1.14:80 # HAProxy
iifname { "vmbr0", "vmbr1" } tcp dport 8443 dnat to 172.16.1.14:443
iifname { "vmbr0", "vmbr1" } tcp dport 3333 dnat to 172.16.1.12:3000 # Gitea
iifname { "vmbr0", "vmbr1" } tcp dport 3300 dnat to 172.16.1.25:3000 # Grafana
iifname { "vmbr0", "vmbr1" } tcp dport 8404 dnat to 172.16.1.14:8404 # HAProxy stats
}
chain postrouting {
type nat hook postrouting priority srcnat; policy accept;
# internet access for both internal bridges
ip saddr 192.168.1.0/24 oifname "vmbr0" masquerade
ip saddr 172.16.1.0/24 oifname "vmbr0" masquerade
# hairpin: a container reaching WireGuard through the public address
ip saddr 192.168.1.0/24 ip daddr 192.168.1.10 udp dport 51820 masquerade
# VPN clients (10.0.0.0/24) going to NexiCloud: hide them behind the host,
# so containers on vmbr4 reply to 172.16.1.1 instead of an unknown subnet
ip saddr 10.0.0.0/24 oifname "vmbr4" masquerade
}
}The last masquerade rule took the longest to understand. VPN clients have addresses in 10.0.0.0/24, and the NexiCloud containers have no route back to that subnet: their gateway is the host, which forwards replies to vmbr0. The connection opened and then hung. Masquerading VPN traffic as the host's vmbr4 address makes replies come straight back the way they came.
table ip filter {
chain input {
type filter hook input priority filter; policy accept;
ct state established,related accept
ct state invalid drop
iifname "lo" accept
iifname { "vmbr1", "vmbr4" } accept
tcp dport 8006 accept # Proxmox UI
}
chain forward {
type filter hook forward priority filter; policy accept;
ct state established,related accept
ct state invalid drop
# internal bridges may go out
iifname { "vmbr1", "vmbr4" } oifname "vmbr0" accept
# anything that was explicitly DNATed may come in
iifname "vmbr0" oifname { "vmbr1", "vmbr4" } ct status dnat accept
# VPN → NexiCloud
iifname "vmbr1" oifname "vmbr4" ct status dnat accept
# Samba domain controller for the Windows lab, and nothing else
iifname "vmbr0" oifname "vmbr1" ip saddr 192.168.2.0/24 ip daddr 192.168.1.105 udp dport { 137, 138 } accept
iifname "vmbr0" oifname "vmbr1" ip saddr 192.168.2.0/24 ip daddr 192.168.1.105 tcp dport { 139, 445 } accept
# everything else from the LAN into the homelab bridge
iifname "vmbr0" oifname "vmbr1" drop
}
}ct status dnat accept is the rule I like most: instead of repeating each forwarded port in the filter, it accepts any packet whose destination was rewritten by a DNAT rule. The NAT table becomes the single list of what is exposed.
Applying it without locking myself out
nft -c -f /etc/nftables.conf # syntax check, applies nothing
cp /etc/nftables.conf /etc/nftables.conf.bak-$(date +%F)
nft -f /etc/nftables.conf # atomic: all or nothing
systemctl enable nftables
nft list ruleset | less
sysctl net.ipv4.ip_forward # must be 1
I did it from the Proxmox console, not over SSH, and kept the previous file next to the new one. Each change since then (the Samba rules, for instance) got its own dated backup first.
Honest status: halfway there
Writing this article meant re-reading the live state, and the migration is not finished:
- The old script is still wired in.
apply-dnat.shis still called frompost-upon vmbr1, so the homelab's own forwards (DNS to Technitium, HTTP/HTTPS to NPM, Jellyfin, Forgejo) still come from iptables rules running next to the nftables file. Both work, because modern iptables writes into nftables under the hood, but there are two sources of truth again. - The input chain still has
policy accept, so its rules document intent rather than enforce it.
The plan for part 2:
- Move the remaining forwardsPort 53 (TCP and UDP) to gamma, 80/443 to delta, and only those, into the nat table. Drop 81 and 5380: the admin UIs stay behind the proxy.
- Unhook the legacy piecesRemove the
post-upiptables lines and the script call from/etc/network/interfaces, theniptables -t nat -F. - Close the input chainSwitch to
policy dropwith SSH and 8006 from the LAN and VPN only, tested with a timed rollback. - Version itPut the file in Git next to the Ansible code from NexiCloud, so every change has a diff and a reason.
Lessons learned
A file you load is a file you can read, diff and roll back. A sequence of commands is history.
nft list ruleset and iptables-save tell you what is true. Your memory tells you what you intended.
Anything that can run twice will run twice. Use flush + full reload, never append.
Every forwarded port should have an owner and a reason. Two of mine had neither.