How to Turn a Raspberry Pi into a Wi-Fi Access Point

Configuring a Raspberry Pi to act as a wireless access point is an excellent way to extend your home network, create a standalone local network, or build a dedicated IoT gateway. This setup leverages the Raspberry Pi’s built-in Wi-Fi module to broadcast a network SSID and route traffic, transforming the compact computer into a fully functional wireless router. By utilizing standard Linux networking tools, you can easily share an existing wired internet connection or host a completely isolated private network.

Prerequisites and Initial Preparation

Before diving into the configuration, ensure you have a Raspberry Pi 3, 4, 5, or Zero 2 W, as these models include onboard Wi-Fi. You will need Raspberry Pi OS (Lite or Desktop) installed and updated. For the initial setup, connect your Pi to your main router using an Ethernet cable to ensure uninterrupted internet access while downloading the necessary software packages. Open a terminal or SSH into your Pi to begin the process.

Installing the Required Software

The transformation relies on two primary software packages: hostapd (Host Access Point Daemon), which handles the wireless access point creation, and dnsmasq, which provides DHCP ip address assignment and DNS services to connected devices. Install them by executing the following commands in your terminal:

sudo apt update
sudo apt install hostapd dnsmasq

Once installed, stop the services temporarily so you can configure them without interference:

sudo systemctl stop hostapd
sudo systemctl stop dnsmasq

Configuring the Static IP

The Raspberry Pi needs a permanent, static IP address on its wireless interface (wlan0) to act as the gateway for your new network. Edit the DHCP client configuration file using a text editor like Nano:

sudo nano /etc/dhcpcd.conf

Go to the bottom of the file and append the following lines to define the static configuration for wlan0:

interface wlan0
    static ip_address=192.168.4.1/24
    nohook wpa_supplicant

Save and exit the file, then restart the DHCP daemon to apply the changes:

sudo systemctl restart dhcpcd

Setting Up the DHCP Server

Next, configure dnsmasq to distribute IP addresses to devices that connect to your Pi’s Wi-Fi. It is best to back up the default configuration file and create a fresh one:

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo nano /etc/dnsmasq.conf

Add the following configuration to define the IP range, subnet mask, and lease duration for connected clients:

interface=wlan0
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h

Configuring the Wireless Network Details

Now you must configure hostapd to define your network name (SSID) and password. Create a new configuration file:

sudo nano /etc/hostapd/hostapd.conf

Populate the file with your network preferences. Ensure you choose a secure password for the wpa_passphrase field:

interface=wlan0
driver=nl80211
ssid=Pi_Access_Point
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=YourSecurePassword
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Point the system daemon to this new configuration file by editing the hostapd default environment file:

sudo nano /etc/default/hostapd

Find the line #DAEMON_CONF="", uncomment it by removing the #, and change it to:

DAEMON_CONF="/etc/hostapd/hostapd.conf"

Enabling Routing and Internet Sharing

If you want devices connected to your Raspberry Pi to access the internet via the Pi’s Ethernet port (eth0), you must enable IP forwarding. Edit the system control configuration file:

sudo nano /etc/sysctl.conf

Uncomment the following line to enable packet forwarding:

net.ipv4.ip_forward=1

To bridge the traffic from the wireless network to your wired internet connection immediately, configure a masquerade firewall rule using iptables:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

To ensure this firewall rule persists after a reboot, install the iptables-persistent utility:

sudo apt install iptables-persistent

Starting the Access Point

With all configurations in place, unmask, start, and enable the network services so they launch automatically every time your Raspberry Pi boots up:

sudo systemctl unmask hostapd
sudo systemctl start hostapd
sudo systemctl start dnsmasq
sudo systemctl enable hostapd
sudo systemctl enable dnsmasq

Your Raspberry Pi is now broadcasting its new Wi-Fi network. You can search for “Pi_Access_Point” on your smartphone or laptop, enter the password you specified, and enjoy your custom-built wireless access point.