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.

Requirements:

  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

1

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

2

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
interface=wlan0

# Use the nl80211 driver with the brcmfmac driver
driver=nl80211

# This is the name of the network
ssid=Harsha_PI

# Use the 2.4GHz band
hw_mode=g

# Use channel 6
channel=6

# Enable 802.11n
ieee80211n=1

# Enable WMM
wmm_enabled=1

# Enable 40MHz channels with 20ns guard interval
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]

# Accept all MAC addresses
macaddr_acl=0

# Use WPA authentication
auth_algs=1

# Require clients to know the network name
ignore_broadcast_ssid=0

# Use WPA2
wpa=2

# Use a pre-shared key
wpa_key_mgmt=WPA-PSK

# The network passphrase
wpa_passphrase=raspberry

# Use AES, instead of TKIP
rsn_pairwise=CCMP

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

net.ipv4.ip_forward=1

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

 

Advertisements