NAT with Linux

Posted by Marcus Folkesson on Thursday, July 27, 2017

NAT with Linux

To share an internet connection may sometimes be very practical when working with embedded devices. The network may have restrictions/authentications that stops you from plug in your device into the network of the big company you are working for.

But what about creating your own network and use your computer as NAT (Network Address Translation)?

It's not that hard to setup, it's actually just a few command lines away.

Host setup

eth0 has ip address and is connected to the company network

eth1 has ip address ans is connected to the target

Target setup

eth0 has ip address ans is connected to host

Enable NAT

First of all, we need to setup a default gateway on our target, do this as you always do - with route.:

Target$ route add default gw eth0

Next, we need to create a post-routing rule in the to the NAT table that masquerades all traffic to the eth0 interface. iptables is your friend:

Host$ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Thats it! Well, allmost. We just need to enable ip-forwarding.:

Host$ echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward