You may be a business owner who offers free Wi-Fi to all of your customers; Maybe you have a captive audience and plan to sell access to your Wi-Fi network, or maybe you want to remind guests of the label for using your home network? Here you will learn how to turn your Raspberry Pi into a captive portal Wi-Fi hotspot.
Why do I need a captive portal?
If you’ve ever tried to access a seemingly open Wi-Fi network in a cafe, restaurant, hotel or gym, only to be greeted by a login screen that refuses to let you continue before entering information (usually your email address), you already know the captive portals!
A captive portal is a web page that automatically opens in the user’s default browser or loads when they try to visit a web page. The user will generally have to perform an action before being able to leave the captive portal.
Although commonly used by businesses, captive portals can also be a useful addition to your home network. For example, you can create a separate network for your children, with parental controls and a captive portal that gently reminds your children that you trust them to use the web responsibly – just in case they are aware enough to know how a VPN works.
What you will need
To complete this tutorial, you will need:
- Raspberry Pi running Raspberry Pi OS
- Power cable compatible with your Raspberry Pi
- External keyboard and way to fix it to your Raspberry Pi
- HDMI or micro HDMI cable, depending on your Raspberry Pi model
- External monitor
- Ethernet cable. Since you are transforming your Raspberry Pi into a wireless access point, you will have to connect via Ethernet rather than Wi-Fi. This also means that it will not work with Raspberry Pi 2 or Raspberry Pi Zero because they lack an Ethernet port or a wireless card.
Getting started: update your Raspberry Pi
Connect your external keyboard, monitor and Ethernet cable, then connect your Pi to a power source. Once it is started, open a terminal and type the following command to update it:
Restart your Raspberry Pi by running the following command:
Once your Raspberry Pi has restarted, everything will be up to date.
Configure a wireless access point for Raspberry Pi
There are several ways to turn your Raspberry Pi into a fully functional access point, but in this tutorial, a RaspAP is used because it is easy to configure.
To install the RaspAP software, open a terminal window and run the following command:
Then restart your Raspberry Pi using the following command:
Once your Raspberry Pi is operational again, your Wi-Fi access point will be configured with the following parameters:
- IP address: 10.3.141.1
- Username: admin
- Password: secret
- DHCP range: 10.3.141.50 to 10.3.141.255
- SSID: raspi-webgui
- Wi-Fi password: ChangeMe
To test your access point, simply enter any Wi-Fi enabled device and check its network settings. You should see the option to connect to a new “raspi-webgui” network.
Connect to this network and you will be asked for a password. RaspAP’s default password is “ChangeMe”, so enter it in the network configuration box, click “Connect” and you will be connected to your new Raspberry Pi access point!
Secure your Wi-Fi hotspot
Before going any further, update “ChangeMe” to something more secure, via the RaspAP web interface:
1. Launch your web browser, if you have not already done so.
2. In the address bar, enter the following information: 10.3.141.1.
When prompted, enter the username “admin” and the password “secret”. You should now see the main RaspAP web interface.
3. In the left menu, select “Hotspot -> Security”.
4. Find the “PSK” section and enter the password you want to use for your Wi-Fi hotspot – make sure it’s something secure!
5. Click on “Save settings”.
Creation of a captive portal with Nodogsplash
Now that your access point is operational, you are ready to secure it with a captive portal.
The captive portal will be constructed using the Nodogsplash captive portal solution, but you must first install the
libmicrohttpd-dev package, because it contains code that you will use to compile Nodogspash.
On your Raspberry Pi, run the following command:
Once you have
libmicrohttpd-dev package, you can clone the repository that contains all the Nodogsplash code:
Once Raspbian has finished cloning this code, you are ready to compile and install the Nodogsplash software:
Nodogsplash is now installed on your Raspberry Pi.
Configure your captive portal
Then you need to point Nogdogsplash in the direction of the gateway address, which is the router interface connected to the local network. Uses of RaspAP 10.3.141.1 by default, you must therefore modify the Nogdogsplash configuration file so that it listens on this address.
To change the gateway address, open the Nogdogsplash configuration file:
Add the following:
Once these modifications have been made, save your file by pressing Ctrl + O, followed by Ctrl + X.
Start your captive portal by running the following command:
Your captive portal is now online. To test it, try to connect to your Wi-Fi access point.
You should now be greeted by Nodogsplash’s default captive portal.
Make sure your portal is still online
Now that you have verified that the captive portal is working properly, you need to make sure that Nodogsplash starts automatically on startup.
Configure Nodogsplash to launch automatically by modifying your “rc.local” file. In the Raspberry Pi terminal, run the following command:
Find the following line:
Directly above, add the following:
Save your changes by pressing Ctrl + O, followed by Ctrl + X.
How to customize your captive portal
At this point, you have a Wi-Fi access point protected by a captive portal. However, you are still using the default Nodogsplash page. In this last section, let’s see how you can access the code that controls your captive portal page and make some simple changes.
To customize the default Nodogsplash page, you must open the file “splash.html”:
You can now add images and text to your portal and delete existing content. For example, in the image below, the text displayed in the context of the captive portal is being modified.
When you are satisfied with the changes you have made, save your changes by pressing Ctrl + O, followed by Ctrl + X.
Try to reconnect to your Wi-Fi hotspot and you should see your captive portal revamped.
Note that depending on your operating system and web browser, you may need to clear your browser’s cache to see the updated captive portal.
In addition to a Wi-Fi hotspot, you can also turn your Raspberry Pi into an ad blocker or personal web server.
How do you use your new captive portal? To welcome people on your home network, define ground rules or as a tool to help promote your business? Let us know in the comments below!
Is this article useful?