'( now thoughts )

using nat to route internet traffic through a computer with a working internet connection

07 Mar 2020

Another day, another Linux install where I botch the network configuration and I’m marooned with no internet access to install the drivers. Sometimes it’s because I forget wpa-supplicant, sometimes it’s because I forget to install the WiFi drivers or firmware package. Whatever the case may be, it’s useful to be able to route network traffic through my laptop (which has working WiFi) over Ethernet. Ethernet always works, thankfully.

I’ve done this a few times. Every time, I forget the commands and have to piece together the right ones from a few online sources. Well no more! I’m finally documenting it.

On the laptop (machine A) that will forward internet traffic for the box with the lame network card (machine B):

sudo ip addr add dev $ETH_DEV
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward # enable forwarding
sudo iptables -t nat -A POSTROUTING -o $WIRELESS_DEV -j MASQUERADE
sudo iptables -A FORWARD -i $ETH_DEV -o $WIRELESS_DEV -j ACCEPT

On machine B set a different IP address for its Ethernet device:

ip addr add dev eth0

… then make sure we can ping machine A from machine B:


Configure default routing for machine B to route internet traffic through machine A:

sudo route add default gw eth0

… and make sure we can ping stuff on the internet from machine B:

ping # Google's DNS

Finally, set your DNS server (probably your router, but you can use Google’s DNS if you’re unsure):

echo 'nameserver' | sudo tee /etc/resolv.conf

Finally, make sure you can ping stuff on the internet:

ping c.har.li

Man, my titles continue to get more and more boring. I need write something more controversial next time :P