Linux › Section I · Your network and its configuration › Topic I.11
Linux as a router: private networks and NAT
A router is just a computer with more than one network interface, so any Linux machine can be one. This topic switches on forwarding, joins two private subnets, and shares one public address with them using masquerading in nftables, the same trick that home routers, containers and virtual machines rely on.
A router is a computer that forwards
By default a Linux host accepts datagrams addressed to itself and discards others; it does not pass traffic between its interfaces. Turn on IP forwarding and it will. Take a machine with three interfaces: enp1s0, an uplink to the internet with the public address 203.0.113.9; enp2s0, on the LAN 192.168.10.0/24 with address 192.168.10.1; and enp3s0, on a second LAN, 192.168.20.0/24, with address 192.168.20.1. Once its addresses are set, ip route shows a proto kernel route for each LAN plus a default route through the uplink. That table is everything the machine needs to route.
Switching on forwarding
IP forwarding is a kernel setting. sysctl net.ipv4.ip_forward prints its current value, 0 by default, and cat /proc/sys/net/ipv4/ip_forward shows the same thing. As root, sysctl -w net.ipv4.ip_forward=1 turns it on at once, and the machine starts routing between subnets. To keep it after a reboot, put the line net.ipv4.ip_forward = 1 in a file such as /etc/sysctl.d/90-router.conf and load it with sysctl --system; a separate file in /etc/sysctl.d keeps local settings apart from the distribution's own and is the documented place for them. IPv6 forwarding is separate: net.ipv6.conf.all.forwarding = 1.
Worked example: routing between two LANs
Host A, 192.168.10.25, sends a datagram to Host E, 192.168.20.40; both use the router's address on their own LAN as their default gateway. 1. A checks its routes: 192.168.20.40 is outside 192.168.10.0/24, so it sends the datagram to its gateway, 192.168.10.1. 2. The router receives it on enp2s0, sees the destination is not one of its own addresses and, with forwarding on, looks it up. 3. 192.168.20.40 matches the proto kernel route 192.168.20.0/24 dev enp3s0, so the router sends it out of enp3s0 in a new frame. 4. E replies to its own gateway, 192.168.20.1, and the reply returns the same way. Check: no NAT was needed, because both LANs are behind the same router.
Routes must work both ways
The example worked because both LANs used the router as their gateway. A common mistake is to add a route on one side only. If Host A had a route to 192.168.20.0/24 through some other machine, but hosts on 192.168.20.0/24 did not know how to reach 192.168.10.0/24, every request would arrive and every reply would vanish. Each network must be able to route back. The simplest design gives every host one default gateway and lets the routers, which see all the subnets, do the rest, rather than scattering extra routes over individual hosts.
Private addresses and why NAT is needed
The LAN addresses above come from the private blocks 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16, defined in RFC 1918 and described in the Networking topic DHCP and network address translation. Anyone may use them internally, but internet routers will not deliver datagrams to them. Forwarding alone therefore gets Host A's request out through the uplink, but the server's reply, addressed to 192.168.10.25, could never find its way back. The router must translate: send the request out under its own public address and map the reply back to A. Linux calls its usual form of NAT masquerading.
Masquerading with nftables
nftables, the current Linux packet-filtering framework, does NAT in a table with a chain hooked at postrouting, the last step before a packet leaves. As root: nft add table ip nat; then nft 'add chain ip nat postrouting { type nat hook postrouting priority 100 ; }'; then nft add rule ip nat postrouting oifname "enp1s0" masquerade. The rule rewrites the source address of every packet leaving the uplink to that interface's current address, which suits an uplink whose address comes from the ISP by DHCP. The kernel's connection tracking remembers each translation and reverses it for replies automatically. nft list ruleset shows the result.
Worked example: one public address for a LAN
Host A, 192.168.10.25, opens a connection from port 50500 to a web server at 198.51.100.80, port 443, through the router above. 1. The router forwards the first packet towards enp1s0. 2. At postrouting the masquerade rule rewrites the source to 203.0.113.9; Linux keeps the source port, 50500, if no other mapping is using it, and otherwise picks another. 3. Connection tracking records that 203.0.113.9:50500 belongs to 192.168.10.25:50500. 4. The server replies to 203.0.113.9:50500. 5. The router matches the tracked connection, rewrites the destination back to 192.168.10.25:50500 and forwards the reply onto enp2s0. Check: the server only ever sees 203.0.113.9.
The iptables way, and connection tracking
Older guides, and many scripts still in use, do the same job with iptables: iptables -t nat -A POSTROUTING -o enp1s0 -j MASQUERADE adds a masquerade rule to the POSTROUTING chain of the nat table. On current distributions the iptables command often writes nftables rules underneath, through a compatibility layer, so the two should not be mixed carelessly on one machine. To see active translations, conntrack -L, from the conntrack-tools package, lists the kernel's tracked connections. A NAT router also needs a filter on forwarded traffic, so that outsiders cannot start connections inward; the topic Firewalls builds that FORWARD policy.
Routers that run Linux
Most home routers are small Linux computers: an efficient processor, flash storage and several network ports, running forwarding, NAT, a DHCP server and a firewall, with a web page for settings. In the early 2000s the GPL licence of components in one popular router's firmware obliged its maker to publish the source, and community distributions such as OpenWrt grew from that; OpenWrt now runs on a wide range of hardware and often outlasts the maker's own updates. Such firmware usually uses BusyBox, one small program that provides the shell and many basic commands. Container engines and virtual machine tools set up exactly this kind of NAT on ordinary Linux hosts.
Common mistakes to avoid
First, forgetting that net.ipv4.ip_forward is 0 by default, or setting it with sysctl -w and losing it at reboot. Second, adding a route in one direction only, so replies have no way back. Third, masquerading on the LAN interface instead of the uplink; the oifname must be the outward interface. Fourth, expecting private addresses to be reachable from the internet without NAT. Fifth, mixing hand-written iptables and nft rules without knowing which backend the system uses. Sixth, assuming IPv6 needs NAT; it has enough addresses, so an IPv6 router forwards and firewalls instead.
Key takeaways
A Linux machine with several interfaces routes once net.ipv4.ip_forward is 1, set with sysctl -w and kept in /etc/sysctl.d; IPv6 uses net.ipv6.conf.all.forwarding. Each subnet needs a route back, ideally through the default gateway. Private ranges 10/8, 172.16/12 and 192.168/16 are not routed on the internet, so the router masquerades: an nft rule in a nat-type postrouting chain, oifname uplink masquerade, or iptables -t nat -A POSTROUTING -o uplink -j MASQUERADE. Connection tracking reverses translations for replies; conntrack -L lists them. Home routers, OpenWrt devices and container hosts all do this.
Flashcards
Flashcard
Kernel setting that lets a host pass datagrams between interfaces
Test your understanding
Question 1 of 5
A Linux machine with two LAN interfaces does not pass traffic between them, although both LANs use it as their gateway. What is the most likely cause?
Question 2 of 5
How do you make IPv4 forwarding survive a reboot?
Question 3 of 5
Which nft rule shares the router's public address, on uplink enp1s0, with the LAN behind it?
Question 4 of 5
Host A, 192.168.10.25, reaches a web server through a masquerading Linux router whose uplink is 203.0.113.9. Which source address does the server see?
Question 5 of 5
A host on 192.168.10.0/24 gets a route to 192.168.20.0/24 through a lab machine, but hosts on 192.168.20.0/24 have no route back. What happens?
