Setting up a Captive Portal with hostapd, Apache, ISC-DHCP-Server, and iptables on Debian and Manjaro
In this article, we will go through the process of setting up a captive portal on a Debian or Manjaro system using hostapd, Apache, ISC-DHCP-Server, and iptables. This setup is ideal for access points and allows for user redirection to a captive portal upon connection.
Prerequisites
Before we begin, it is assumed that you have a working knowledge of the Linux command line and have a system with Debian or Manjaro installed. Additionally, you should have hostapd, Apache, ISC-DHCP-Server, and iptables installed on your system.
Setting up the Access Point with hostapd
The first step in setting up a captive portal is to configure the access point using hostapd. This can be done by editing the hostapd configuration file, typically located at /etc/hostapd/hostapd.conf.
interface=wlan0
driver=nl80211
ssid=MyAccessPoint
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=MySecretPassphrase
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
Once the configuration file has been edited, hostapd can be started with the following command:
sudo systemctl start hostapd
Setting up the DHCP Server with ISC-DHCP-Server
The next step is to configure the DHCP server using ISC-DHCP-Server. This can be done by editing the dhcpd.conf configuration file, typically located at /etc/dhcp/dhcpd.conf.
ddns-update-style none;
option domain-name "example.com";
option domain-name-servers ns1.example.com, ns2.example.com;
default-lease-time 600;
max-lease-time 7200;
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.100 10.0.0.200;
option routers 10.0.0.1;
option broadcast-address 10.0.0.255;
option domain-name-servers 8.8.8.8, 8.8.4.4;
}
Once the configuration file has been edited, ISC-DHCP-Server can be started with the following command:
sudo systemctl start isc-dhcp-server
Setting up the Captive Portal with Apache
The final step is to configure the captive portal using Apache. This can be done by creating a new virtual host in the Apache configuration file, typically located at /etc/apache2/sites-available/000-default.conf.
ServerName captive.example.com
DocumentRoot /var/www/captive
Order allow,deny
Allow from all
ErrorLog ${APACHE\_LOG\_DIR}/error.log
CustomLog ${APACHE\_LOG\_DIR}/access.log combined
Once the virtual host has been created, the captive portal page can be created in the /var/www/captive directory. Apache can then be restarted with the following command:
sudo systemctl restart apache2
Redirecting Users to the Captive Portal with iptables
The final step is to redirect users to the captive portal upon connection. This can be done using iptables with the following command:
sudo iptables -t nat -A PREROUTING -i wlan0 -p tcp --dport 80 -j DNAT --to-destination 10.0.0.1
This command redirects all incoming TCP traffic on port 80 to the IP address of the Apache server. Users will now be redirected to the captive portal upon connection.
- Configure the access point using hostapd
- Configure the DHCP server using ISC-DHCP-Server
- Configure the captive portal using Apache
- Redirect users to the captive portal using iptables
References
--endarticle--