Hack: How to Turn Your Raspberry Pi Into A Router

If you’ve been doing your Raspberry Pi 3 projects where you need to connect your ESP8266 modules to your Pi 3 for sensor data collection, chances are you might be using a separate access point to route your data. This article will show you how to eliminate the use of a router when accessing your Raspberry Pi by making it as a standalone access point itself. In this way, you can also operate your Raspberry Pi in headless set up using SSH (no more cables for HDMI monitor, mouse and keyboard).

Before starting, make sure your setup is ready:

  • A raspberry pi with keyboard, mouse and monitor
  • A raspberry Pi with internet connection (either via WiFi or Ethernet)

Step 1: Update your OS

To start, we need to update our Raspberry Pi 3. Try opening up the terminal (Ctrl + Alt + T) and type the following commands:

sudo apt-get update
sudo apt-get dist-upgrade

Note: The upgrade may take minutes or hours depending on your device or internet connection.

Step 2: Install Required Packages

Next step is to install the packages. Type the following:

sudo apt-get install dnsmasq hostapd

Note: These packages enable your device to establish its own standalone network (hostapd) and automate IP assignment (dnsmasq) to the devices connected to your Raspberry Pi.

Step 3: Set a Static IP for WLAN0

Configure the /etc/network/interfaces to have a static IP by invoking the following command:

sudo nano /etc/network/interfaces

Next, edit the file to have the following configuration:

allow-hotplug wlan0
iface wlan0 inet static

Out of this, your Pi will have a static IP of Of course, you can select for your own private IP address (i.e. ranging to and to

Type the following command to restart the network service.

sudo service networking restart

Step 4: Configure DNS/DHCP Server

Next step is to configure the dnsmasq. This service is responsible for assigning the IP address of your device when it connects to the Pi. The default configuration contains a lot of information and thus should not be overwritten should we decide to revert back to this configuration in the future. So instead, we can rename this file to:

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig

and create a new configuration file:

sudo nano /etc/dnsmasq.conf

Add the following lines to the file:


This enables your service to listen to your static IP address in your wlan0 interface. The dhcp-range allows your Pi to hand out the range of IPs to your connected devices. The 12h is the lease/reservation time of dwelling an IP address given by the DHCP server to the device connected.  For a full documentation of dnsmasq service, check out this link:


Step 5: Configure Hostapd

Next is to configure your access point by editing the hostapd configuration file.

sudo nano /etc/hostapd/hostapd.conf

Copy and paste the following settings:


You may want to create your own SSID, passphrase and channel for your access point.

A daemon can autorun this setting when the Pi boots by editing the configuration file so that you will never reconfigure your Pi whenever the system restarts. Type these commands:

sudo nano /etc/default/hostapd

Look for the line containing “DAEMON_CONF” and edit this (remove hash if there is) by inserting the following:


Step 6: Enable IPv4 Forwarding

By the end of this activity, you should be able to connect your Pi 3 to a modem via the Ethernet cable to access the internet. Through packet forwarding, your Pi will allow your connected devices to communicate with the Ethernet thus sharing your Pi’s internet connection.

So the next step is to enable IPv4 forwarding by editing the sysctl.conf

sudo nano /etc/sysctl.conf

and uncomment the IPv4 forwarding section. It should look like this:

# Uncomment the next line to enable packet forwarding for IPv4

Step 7: Set the Rules for IPTables

Open the file containing the IPTables by typing:

Sudo nano /etc/iptables.ipv4.nat

The following rules are currently what I am using. You may copy these for your own configuration.

# Generated by iptables-save v1.4.21 on Wed Dec 21 11:28:05 2016
-A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i wlan0 -o eth0 -j ACCEPT
# Completed on Wed Dec 21 11:28:05 2016
# Generated by iptables-save v1.4.21 on Wed Dec 21 11:28:05 2016
# Completed on Wed Dec 21 11:28:05 2016

Also, we need these rules to auto run on each boot. To do this, we will edit the /etc/rc.local

sudo nano /etc/rc.local

and just above the ‘exit 0’, add the following line

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

Step 8: Start the Services

Now, all the configurations are complete! All we need to do is to start the services:

sudo service hostapd start  
sudo service dnsmasq start 

And finally, reboot the system:

sudo reboot

And you’re done! When your Pi has turned on, try to check your Wi-Fi networks and you should be able to connect to your access point.

Let us know if you have successfully turned your Pi 3 into a router through the comments section below. Share this article with your friends who are also working with the same project. Enjoy!

Leave a Reply

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