Raspberry Pi Zero WiFi Access Point Tutorial

Firstly, start with updating the Raspberry Pi with the following commands:

sudo apt-get update
sudo apt-get upgrade

Then we need to download two packages, DNSMASQ and HOSTAPD and we can do that by running:

sudo apt-get install hostapd dnsmasq

Then we need to find the MAC address of the wlan0 interface. This interface is the WiFi connection to your router. Run the follwoing command and find the MAC address of the wlan0 interface:

ip addr
Find your MAC address underlined in red

Copy and this MAC address and store it for later! Next we need to create another interface, this time for the access point and we do this by creating a udev rule. Create and edit the following file using:

sudo nano /etc/udev/rules.d/70-persistent-net.rules

And add the following, replacing MAC_ADD with the MAC address you copied previously:

SUBSYSTEM=="ieee80211", ACTION=="add|change", ATTR{macaddress}=="MAC_ADD", KERNEL=="phy0", \
  RUN+="/sbin/iw phy phy0 interface add ap0 type __ap", \
  RUN+="/bin/ip link set ap0 address MAC_ADD"

Close the file using ctrl-x then Y to save and enter to save with the same name.

Then we need to edit the following file:

sudo nano /etc/dnsmasq.conf

Add the following to the bottom of the file that opens:

interface=lo,ap0
no-dhcp-interface=lo,wlan0
bind-interfaces
server=8.8.8.8
domain-needed
bogus-priv
dhcp-range=192.168.10.100,192.168.10.200,12h

The above code configures the DHCP server to give out IP addresses on the “.10” subnet between “.100” and “.200”. Adjust the “server” value if you want to use a different DNS server, if you have your own for example. 8.8.8.8 is Google’s DNS server. Save and close this file.

Next we edit another file, this time the hostapd configuration file:

sudo nano /etc/hostapd/hostapd.conf

Add the following text whilst adjusting the AP_SSID and AP_PASS to your desired access point SSID and password.

ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
interface=ap0
driver=nl80211
ssid=AP_SSID
hw_mode=g
channel=11
wmm_enabled=0
macaddr_acl=0
auth_algs=1
wpa=2
wpa_passphrase=AP_PASS
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
rsn_pairwise=CCMP

Then save and close this file.

Next we modify:

sudo nano /etc/default/hostapd

And add the following line of code to the bottom of the file:

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

This will tell hostapd to use the config file we just created.

Next we need to modify:

sudo nano /etc/network/interfaces

With the following. Note: If you changed the subnet of the access point earlier, then make sure to alter the static IP of ap0 here.

source-directory /etc/network/interfaces.d

auto lo
auto ap0
auto wlan0
iface lo inet loopback

allow-hotplug ap0
iface ap0 inet static
address 192.168.10.1
netmask 255.255.255.0
hostapd /etc/hostapd/hostapd.conf

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

The next step is credited to Albert Chaharbakhshi of blog.thewalr.us who created this automated start-up script.

We need to create a script file in our user area:

nano ./start-ap-managed-wifi.sh

With the following contents:

#!/bin/bash
sleep 30
sudo ifdown --force wlan0 && sudo ifdown --force ap0 && sudo ifup ap0 && sudo ifup wlan0
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -t nat -A POSTROUTING -s 192.168.10.0/24 ! -d 192.168.10.0/24 -j MASQUERADE
sudo systemctl restart dnsmasq

Save and exit this file, and make it executable:

chmod +x /home/pi/start-ap-managed-wifi.sh

Then run:

sudo crontab -e

And add the following line:

@reboot /home/pi/start-ap-managed-wifi.sh

Save and exit this file and reboot! You should now have your AP up and running!

Leave a comment

Your email address will not be published. Required fields are marked *