May 27, 2019

How to set up a Raspberry Pi as an access point

The Wi-Fi in our house is very weak. So weak that the signal won't reach any far-away rooms. After getting annoyed at my Mi Box dropping the Wi-Fi connection a couple of times, I decided to extend the range of the signal with a Wi-Fi access point.

Most of the tutorials online, however, only mention how to make a Wi-Fi repeater with a Raspberry Pi. Now, for the unaware, a Wi-Fi repeater is terrible because you're essentially cutting the bandwidth in half, and causing more congestion for the network in your area. An AP (access point), on the other hand, is significantly better because the communication between your Raspberry Pi and your router happens through the Ethernet jack.

So let's get started! This guide assumes you have the latest Raspbian Stretch Lite flashed on a SD card with balenaEtcher inside the Raspberry Pi.

  1. (optional) Create an empty file called ssh or ssh.txt inside the /boot partition of the USB. Windows will automatically open the drive if you plug it in. Do NOT format the other drive if Windows asks you. Too late, and you've already pressed format? Start over with balenaEtcher.
    This asks the Pi to enable SSH on first boot. Useful if you don't have a keyboard, mouse, and a monitor handy to configure the Pi.
  2. Make sure the Pi is connected to an Ethernet network.
  3. Start up the Pi and connect to it using SSH. If you don't know the Pi's IP address, download Fing on your phone (Android/iOS) and do a scan on your network. If that doesn't show anything, try running arp -a on any terminal. If that doesn't work, use nmap. As a last resort (and potentially the first place you might check if your router is decent), connect to the router's administration page and check the DHCP leases. My router is capable of this but it never showed any devices in the list except the one I was connecting from, so the feature was absolutely useless for me.
  4. The default username and password is pi and raspberry. We're going to change the password in a bit.
  5. Change the password using passwd.
  6. sudo apt update to fetch latest update manifests.
  7. sudo apt upgrade to update the packages.
  8. sudo rpi-update to update firmware.
  9. sudo reboot to reboot and apply the new firmware.
  10. sudo apt install hostapd to get Wi-Fi hosting tools.
  11. sudo systemctl stop hostapd to stop hostapd. We don't need it for now and we need it disabled to edit the configuration.
  12. sudo apt install bridge-utils to install bridging utilities.
  13. sudo brctl addbr br0 to create a bridge interface. This is what the Wi-Fi clients will use while connecting.
  14. sudo nano /etc/hostapd/hostapd.conf to create configuration for hostapd:
# driver=nl80211
hw_mode=g # Available options are a for 5GHz, and b and g for 2.4GHz

You must comment out driver or else it will fail. Once finished, type Ctrl-O, and then Enter to save. Type Ctrl-X to exit.

15. sudo nano /etc/default/hostapd to apply new configuration. Find the line that says


And uncomment it. Edit like so:


Save and exit.

16. We need to bridge the two connections. Run sudo brctl addif br0 eth0.

17. Add these lines to sudo nano /etc/network/interfaces:

allow-hotplug wlan0
iface wlan0 inet manual

auto br0
iface br0 inet dhcp
bridge_ports eth0 wlan0

18. We need to prevent the Pi from assigning eth0 and wlan0 to new network interfaces. Add the following lines to sudo nano /etc/dhcpcd.conf:

denyinterfaces wlan0
denyinterfaces eth0

19. Enable hostapd

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

19. Finally, run sudo reboot to start your new AP! When rebooting, there might be an error: "Failed to start dhcpcd on all interfaces." This is normal and should be ignored.

So great! Now that you have it set up, you should be good to go, right?

Well, not quite. If it works for you, don't bother reading the rest of the post. If you're curious, read on.

In my testing, the Pi-AP was absolutely great. DHCP requests were all handled by my main router, and Internet access worked great. However, I wondered what the network throughput would be.

To test this, I set up an iperf3 server and then ran tests between my laptop (a ThinkPad X250) and the server, going through the Pi-AP or directly. The results were disappointing. The direct connection gave me 100Mbps of throughput - very well, seeing as this is Cat.5 cable, but the Pi-AP gave about 20Mbps. What the hell?

I tried using my original, crappy Chinese AP, and it gave me a consistent throughput of 70Mbps. About what I was expecting. I have two theories as to what is going on. Theory number one is that the Pi is underpowered, and therefore suffers from Ethernet/Wi-Fi lag. Theory number two is that the packets first go to the router (or default gateway) for inspection, come back to the Pi and then go to the intended recipient. Then the cycle repeats as the packets make their way back to the client.

I'm betting on the latter, but it could easily be the former. Not too sure what's going on. If you have an idea as to why, please send me an email!