It is always useful to know how to use your Raspberry Pi in pet projects that are actually useful around the house. Especially when you aren’t building something new – using your dormant Raspberry Pi to build useful devices around the house is a fun idea. If you are new to Raspberry Pi I recommend you first take a look at our articles on setup and what it is before undertaking this project as I have assumed that some setup and idea of the user interface is already obtained. If you have all that, then hi! In this article, we’ll build a WiFi extender. At the end of this article you should be able to connect your device to your Raspberry Pi just like you would to any other WiFi network. You can even build this at home and use it as your private WiFi hotspot!
Things you need:
- Raspberry Pi 3 or 4 – wont work for Zero or 2 [since they are missing either Ethernet port or the on-board WiFi Adapter]
- Power cable
- Keyboard
- Mouse
- HDMI cable
- Monitor
- Ethernet cable
For this, we’ll be using Hostapd (Host Access Point Daemon) to transform network interface cards into access points. What that means is that it allows a host device, in this case our Raspberry Pi, to become a WiFi access point (AP) that other clients (any other device) can connect to and use like one would a modem.
We also use, DNSMASQ to provide domain name system caching and dynamic host configuration protocol (DHCP) for smaller networks. Dynamic Host Configuration Protocol (DHCP) allows us to dynamically assign IP addresses to devices that connect to the AP. Domain Name System (DNS) maps host names to IP number.
In addition to this, we use firewall plugins. To save and load these, we install the net filter persistent and the IP tables persistent plugins.
Setup:
Step 1 – setup your Pi:
Connect all the peripherals to the Raspberry Pi, including the Ethernet cable. Then boot your Raspberry Pi to check if you are running the latest version of Raspbian. To do that, open the terminal and type,
sudo apt-get update
Press enter and wait for the command to execute.
sudo apt-get upgrade
Again wait for the command to execute, after which you can reboot. To do this, type
reboot
Step 2 – Install the pre-requisites:
We need to install Hostapd, DNSMasq and Firewall Plugins.
For Hostapd:
To do this, open your terminal and run the following command,
sudo apt install hostapd
After this, you want to enable the wireless access point and set it to launch automatically on start of the system. To do this, enter
sudo systemctl unmask hostapd
then,
sudo systemctl enable hostapd
For DNSMASQ:
In your terminal, run the following command,
sudo apt install dnsmasq
For Firewall Plugins:
On your terminal,
sudo DEBIAN_FRONTEND=noninteractive apt install -y netfilter-persistent iptables-persistent
Step 3 – Assign a static IP address:
The DHCP, as we discussed earlier needs a static IP address that we will now learn to configure. In the terminal, run
sudo nano /etc/dhcpcd.conf
You will then see the configuration file open up like below
At the bottom of these, add the following lines:
interface wlan0 static ip_address=192.168.4.1/24 nohook wpa_supplicant
Save this by pressing ctrl+O, then enter and exit by hitting ctrl+X.
Step 4 – Enable routing
At this point your Pi already is a stand alone wireless network, but in order to actually use this, you need to enable other devices to connect to your Pi. To do this, we have to enable routing by creating a routed ap file, by typing the command
sudo nano /etc/sysctl.d/routed-ap.conf
this command creates the file and opens it for editing in the Nano. In the Nano text editor that opens, type the following commands.
net.ipv4.ip_forward = 1
Save this and exit, as before by pressing ctrl+O, then enter and exit by hitting ctrl+X.
Next we add the firewall plugin by typing
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
To ensure that this loads at start up, type
sudo netfilter-persistent save
Step 5 – Configure DHCP and DNS services:
Why we do this is, since we don’t really require all the options included in the default configuration file provided by DNS. So, to do this we rename the default configuration file, replace it with an empty file and edit the new empty file to suit our requirement.
In the terminal, type
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo nano /etc/dnsmasq.conf
This will then open the Nano text editor. Then write the following in this new file.
interface=wlan0 dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h domain=wlan address=/gw.wlan/192.168.4.1
Once again, save and exit, by pressing ctrl+O, then enter and exit by hitting ctrl+X.
Step 6 – Create network name and password for your WiFi:
To do this we have to edit the Hostapd configuration file. To open this file type,
sudo nano /etc/hostapd/hostapd.conf
Now in the Nano editor you can add information about your wireless access point including name and password. Please note that your password should be 8 characters or more and have a mix of letters, numbers and special characters.
interface=wlan0 ssid= <enter a Network Name> hw_mode=g channel=7 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase= <enter any password> wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP
Remember the name and password, then save and exit, by pressing ctrl+O, then enter and exit Nano by hitting ctrl+X.
Step 7 – Celebrate with your WiFi
But before you do that, first connect any device to your network to see if it works! Restart your Raspberry Pi first. Then, like any other Wi-Fi network – just search for the available networks on your device and find your Network name. I used the name ‘hi’, and there it is!
Connect to it after entering the password, and there you go! Time to celebrate 😊!