Skip to content

21 NAT gateway ​

A Linux gateway gives a private LAN DHCP, DNS and NAT to the internet, like a home router.

What you practice ​

  • Running your own DHCP and DNS (dnsmasq) on a switch with dhcp: false
  • Masquerade and a forward policy in nftables
  • Following one connection through NAT with conntrack and tcpdump

Topology ​

NodeImageCPUs / memoryAddressesRole
gwdebian-131 / 1GiBmgmt 10.121.0.11; lan 10.121.1.254/24DHCP, DNS and NAT for the LAN
c1debian-131 / 1GiBlan DHCPClient, address from gw
c2debian-131 / 1GiBlan DHCPClient, address from gw

Before you start ​

bash
vz image pull disk debian-13

Run it ​

bash
cd examples/21-nat-gateway
vz validate
vz up
vz node ls
ssh lab@10.121.0.11        # password: lab

First boot installs packages; give it a minute or two after vz up returns.

Try it ​

bash
ssh lab@10.121.0.11 'cat /var/lib/misc/dnsmasq.leases'   # the clients' addresses
vz node console c1
#   ip route; resolvectl status; curl -sI https://deb.debian.org | head -1
ssh lab@10.121.0.11 'sudo nft list ruleset; sudo tcpdump -ni mgmt -c 5'

Notes ​

The Mac is a member of every vmnet switch, so it also has an address on lan (10.121.1.1). It does not route; the clients' default route is the gateway. The clients have no mgmt NIC: their only way out is through gw.

Clean up ​

bash
vz down
vz destroy --yes   # deletes the overlays and state of this lab