The experiment is about setting up Pi as a router. In order to do that a physical internet connection is given to raspberry pi through an ethernet cable.


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

Creating a user:

Firstly, when we are creating an internet hub our primary goal is to protect the device supplying the internet i.e. Raspberry Pi. So, we need to change the default ID and passwords to access the Pi.

-> $sudo adduser userID # this command is used to create a new user

The execution of above command prompts to create a password. The password that is desired can be given when requested.

Installing required Packages:

Command to install packages is

->$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