Setting up Pi as Wi-Fi router

Aim: The experiment is about setting up Pi as a router.  This can be acheived by having a direct internet connection to the Raspberry PI through an Ethernet cable.


  1. Ethernet cable
  2. Internet supplying switch
  3. hostapd and dnsmasq packages

How to create a user?

We are now demonstrating on how to create an internet hub and our aim is to protect the device that’s supplying the internet i.e. Raspberry Pi.  So, we would have to change the default ID and passwords to access the Pi.

The below is the command to create a new user.

sudo adduser userID 

The execution of above command prompts to create a password. The password can be given whenever its asked for.

Installing required Packages:

Command to install packages is as below:

sudo apt-get install dnsmasq hostapd

  • hostapd –  uses the built-in WiFi as an access point
  • dnsmasq – This is a combined DHCP and DNS server


deny interfaces wlan0 in the dhcpcd.conf file add

denyinterfaces wlan0

sudo nano /etc/dhcpcd.conf 

Now we need to configure our static IP. To do this open up the interface configuration file with the following command,

sudo nano /etc/network/interfaces


Next, we need to configure hostapd. Create a new configuration file with the following command,

sudo nano /etc/hostapd/hostapd.conf

with the following contents:

# This is the name of the WiFi interface we configured above

# Use the nl80211 driver with the brcmfmac driver

# This is the name of the network

# Use the 2.4GHz band

# Use channel 6

# Enable 802.11n

# Enable WMM

# Enable 40MHz channels with 20ns guard interval

# Accept all MAC addresses

# Use WPA authentication

# Require clients to know the network name

# Use WPA2

# Use a pre-shared key

# The network passphrase

# Use AES, instead of TKIP


we also need to tell hostapd where to look for the config file when it starts up on boot. Open up the default configuration file with

sudo nano /etc/default/hostapd

check for the line starting with #DAEMON_CONF="" and replace it with DAEMON_CONF="/etc/hostapd/hostapd.conf"

Now we need to configure dnsmasq.

set up IPV4 forwarding by using the following command,

sudo nano /etc/sysctl.conf

uncomment line by removing # from the beginning of the line containing


This will enable it on the next reboot, but because we are impatient, activate it immediately with :
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

Now we need to share our Pi’s internet connection to our devices connected over WiFi by the configuring a NAT between our wlan0 interface and our eth0interface. We can do this using the following commands:

$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

However, we need these rules to be applied every time we reboot the Pi, so run

$sudo sh -c "iptables-save > /etc/iptables.ipv4.nat" to save the rules to the file /etc/iptables.ipv4.nat. Just above the line exit 0, add the following line:

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

Now we just need to start our services:

sudo service hostapd start

sudo service dnsmasq start

In order to start routing the following command is executed,

sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s