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.
- Ethernet cable
- Internet supplying switch
- 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
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 interface=wlan0 # Use the nl80211 driver with the brcmfmac driver driver=nl80211 # This is the name of the network ssid=Lavanya_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
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