Setting up a raspberry pi 3 as a router

Below I am going to show you how to share your wi-fi internet with an ethernet device using a raspberry pi. This is a simple task to get an ethernet-only device online or in my case a host computer for a mesh network.

 

Prerequisites

If you don’t have your raspberry pi SD card configured to run Raspian click here

sudo apt-get -y update; sudo apt-get -y upgrade; sudo reboot

What does it do: update your software to the latest saying Yes to all prompts. (most are just it is this size are you sure) then reboot

Once your system is back up and running lets configured the DHCP host for the ethernet port.

Let’s install the DNS server

sudo apt-get install dnsmasq -y

Let’s deny the ethernet for requesting DHCP
sudo nano /etc/dhcp/dhcpd.cfg
and add

denyinterfaces eth0

Next edit

sudo nano /etc/network/interfaces

Under auto eth0 I used the following settings

allow-hotplug eth0
iface eth0 inet static
address 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255

Now configure the DHCP service for eth0

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak

now create a new file and add the following code

sudo nano /etc/dnsmasq.conf
interface=eth0
listen-address=192.168.1.1
dhcp-range=192.168.1.100, 192.168.1.200, 12h
server=8.8.8.8
bind-interfaces
domain-needed
bogus-priv

to set up port forwarding run the following command

sudo nano /etc/sysctl.conf 

Un-comment out the following line

net.ipv4.ip_forward=1

This should be the last line on your terminal when you open the file but if you cant find it just place it at the bottom of the page.
Protip: Ctrl + W the alt + W (represented as ^W) to rerun the search

Next step is setting up the communication between the Ethernet and the Wireless chip. They are called eth0 and wlan0 respectively.


sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
sudo iptables -A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT

Almost there last few steps
Run this command to back up the configuration.

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

Then add the following to the end of the file /etc/rc.local to ensure the IPtables are always set on boot:

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

Finalize everything with a

sudo reboot n 

Check to make sure your client can get online when connected to the raspberry pi and you are good to go.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.