Configuring Raspberry Pi as a Wireless Access Point
Turning your Raspberry Pi into a Wireless Access Point (AP) unlocks the potential of this compact computer, enabling you to create your own Wi-Fi network. This setup opens up a world of possibilities for local networking, prototyping, and connectivity. In this guide, we will use the integrated Ethernet adapter to connect the Raspberry Pi to the internet and configure the built-in Wi-Fi adapter to function as an access point.
Steps to configure the raspberry pi as AP
- Install all required packages
- Unblock wlan
- Configure DHCP
- Configure hostapd
- Configure dnsmasq
- Allow access to the internet
Install required packages
Before starting, we must install the necessary packages to ensure the Raspberry Pi can function effectively as a wireless access point:
sudo apt update
sudo apt install -y dnsmasq hostapd
DEBIAN_FRONTEND=noninteractive sudo apt install -y netfilter-persistent iptables-persistent
Unblock wlan
By default, the wireless card on the Raspberry Pi is blocked. To unblock it and start using the integrated wireless card, you can use the following command:
sudo rfkill unblock wlan
Configure a static IP address
A static IP is crucial for the access point configuration. The Dynamic Host Configuration Protocol (DHCP) is an essential network management protocol utilized on IP networks. Its purpose is to automatically allocate IP addresses and configure other communication parameters for devices connected to the network.
Edit the /etc/dhcpcd.conf config file and add at the end of the file:
interface wlan0
static ip_address=172.16.158.1/24
nohook wpa_supplicant
This will set the static IP of the interface wlan0 to 172.16.158.1
To get the current IP address of the eth0 interface you can use the ifconfig command
Configure Hostapd
Hostapd, which stands for host access point daemon, is a user-space daemon software that allows a network interface card to function as an AP. It transforms a wireless network interface into an access point, allowing other devices to connect to it.
Edit the /etc/hostapd/hostapd.conf config file and make sure you have the following configurations:
country_code=BE
interface=wlan0
ssid=WIFI_NAME_HERE
hw_mode=a
channel=36
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=PASSWORD_HERE
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
- country_code: it's used to set the correct frequency band for the wireless card. You can find all codes here . Make sure to replace it with your correct country code.
- interface: is the wireless network interface to be used.
- ssid: the AP name (make sure to change the value)
- hw_mode: this specify if it should be 2.4 Ghz or 5 GHz. You can find the accepted modes here
- channel: the channel will be used to transfer data. You can find all channels here
- wpa_passphrase: the AP password (replace it with a strong password)
Configure Dnsmasq
Dnsmasq is a user-friendly and lightweight DNS forwarder that offers easy configuration. It provides DNS services with the option to include DHCP. Dnsmasq is specifically designed for small-scale networks.
The config file /etc/dnsmasq.conf should looks like:
dhcp-range=wlan0,172.16.158.2,172.16.158.10,255.255.255.0,24h
domain=wlan
server=208.67.220.220
server=208.67.222.222
dns-forward-max=150
no-poll
bogus-priv
domain-needed
cache-size=10000
#ENABLE THE FOLLOWING FOR LOGGING
#log-dhcp
#log-queries
#log-facility=/var/log/dnsmasq.log
- dhcp-range: It's the range of ips that can be assigned to the device when it's connected to the AP including the net mask and the release time.
- server: The remote DNS server to resolve the queries. I am using OpenDns.
Allow access to the internet
By default, access to the internet is not allowed. You have to enable IP forwarding to allow devices connected to your Raspberry Pi access point to connect to the internet. This can be done by adding net.ipv4.ip_forward=1to /etc/sysctl.d/routed-ap.conf
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.d/routed-ap.conf
Enable the masquerade iptables rule:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
and make it persistent so you don't have to configure it again after reboot:
sudo netfilter-persistent save
Enable and start the services
To make sure all configured services are enabled and running:
sudo systemctl unmask hostapd.service
sudo systemctl enable hostapd
sudo systemctl restart hostapd
sudo systemctl enable dnsmasq
sudo systemctl restart dnsmasq
Finally reboot the Pi with:
sudo reboot now
Now you should be able to connect to the new AP which is your Raspberry Pi.
The Raspberry Pi 4 can serve as an excellent access point solution. With its powerful hardware capabilities and versatility, it can be configured to act as a standalone AP, enabling other devices to connect wirelessly. Setting up your Raspberry Pi as a Wi-Fi router allows you to create a robust wireless network, ideal for various projects and applications.
In the next article, we will discuss using Dnsmasq as an ad blocker, enhancing your network's functionality and security. Stay tuned!