Raspberry PI NAT (for WiFi Router)

Configure Network Address Translation

Setting up NAT will allow multiple clients to connect to the WiFi and have all the data ‘tunneled’ through the single Ethernet IP. (But you should do it even if only one client is going to connect)

Run

sudo vi /etc/sysctl.conf

uncomment this line (or scroll to the bottom and add if it is not there!)

net.ipv4.ip_forward=1

Save the file. This will start IP forwarding on boot up.

To reload the file without rebooting .. 
sudo sysctl -p

Run the following commands to create the network translation between the ethernet port eth0 and the wifi port wlan0

sudo iptables t nat A POSTROUTING o eth0 j MASQUERADE

sudo iptables A FORWARD i eth0 o wlan0 m state state RELATED,ESTABLISHED j ACCEPT

sudo iptables A FORWARD i wlan0 o eth0 j ACCEPT

You can check to see whats in the tables with

sudo iptables -t nat -S
sudo iptables -S

To make this happen on reboot (so you don’t have to type it every time) run

sudo sh -c “iptables-save > /etc/iptables.ipv4.nat”

run

sudo vi /etc/network/interfaces

and add

up iptables-restore < /etc/iptables.ipv4.nat

to the very end

3 thoughts on “Raspberry PI NAT (for WiFi Router)

  1. For USB tether sharing .. add

    sudo iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE
    sudo iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE
    sudo iptables -A FORWARD -i wlan0 -o usb0 -j ACCEPT

    1. rather !
      iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE
      iptables -A FORWARD -m conntrack –ctstate RELATED,ESTABLISHED -j ACCEPT
      iptables -A FORWARD -i eth0 -o usb0 -j ACCEPT

Leave a Reply