DEV Community

Andrea Mancuso
Andrea Mancuso

Posted on

Setting Up a Wi-Fi Access Point on Raspberry Pi with 5 GHz and Client Mode

If you want to use your Raspberry Pi as a Wi-Fi access point (AP) that connects to an existing shared Wi-Fi network and acts as a hotspot for other devices, here's how you can set it up.

This setup leverages hostapd, dnsmasq, and iptables to enable a seamless Wi-Fi AP, allowing for a faster and more reliable connection.


Prerequisites

  • A Raspberry Pi (with a Wi-Fi 6 USB dongle or built-in support)
  • Wi-Fi router or AP
  • A stable shared Wi-Fi network to connect to
  • Basic understanding of the terminal and Raspberry Pi networking
  • A text editor (e.g., nano or vim)

Step 1: Install Required Software

First, ensure your Raspberry Pi is up to date and install the necessary software packages:

sudo apt update sudo apt upgrade -y sudo apt install hostapd dnsmasq iptables-persistent 
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure hostapd for 5 GHz Wi-Fi

Create the configuration file for hostapd:

sudo nano /etc/hostapd/hostapd.conf 
Enter fullscreen mode Exit fullscreen mode

Insert the following configuration:

interface=wlan1 driver=nl80211 ssid=PiRouter hw_mode=a channel=36 ieee80211d=1 ieee80211n=1 ieee80211ac=1 wmm_enabled=1 country_code=country_code_2_letter_ISO macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase=your_secure_password wpa_key_mgmt=WPA-PSK rsn_pairwise=CCMP 
Enter fullscreen mode Exit fullscreen mode
  • Replace country_code_2_letter_ISO with your country code, i.e. US.
  • Replace your_secure_password with your desired WPA2 passphrase.
  • hw_mode=a sets it to 5 GHz.
  • ieee80211ac=1 enables 802.11ac for Wi-Fi 6 speeds.
  • channel=36 is a good 5 GHz channel to start with.

Step 3: Configure DHCP and DNS with dnsmasq

Edit the dnsmasq configuration file:

sudo nano /etc/dnsmasq.conf 
Enter fullscreen mode Exit fullscreen mode

Add the following:

interface=wlan1 dhcp-range=192.168.50.10,192.168.50.50,12h 
Enter fullscreen mode Exit fullscreen mode

This sets up the DHCP range for devices that will connect to the Pi's Wi-Fi.


Step 4: Enable IP Forwarding and NAT

Enable IP forwarding by uncommenting the appropriate line in the sysctl configuration file:

sudo nano /etc/sysctl.conf 
Enter fullscreen mode Exit fullscreen mode

Uncomment the line:

net.ipv4.ip_forward=1 
Enter fullscreen mode Exit fullscreen mode

Apply the changes:

sudo sysctl -p 
Enter fullscreen mode Exit fullscreen mode

Set up Network Address Translation (NAT) to route traffic from your connected devices to the internet:

sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE sudo iptables -A FORWARD -i wlan1 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT sudo iptables -A FORWARD -i wlan0 -o wlan1 -j ACCEPT 
Enter fullscreen mode Exit fullscreen mode

To make these iptables rules persist after reboot, run:

sudo netfilter-persistent save 
Enter fullscreen mode Exit fullscreen mode

Step 5: Start Services

Enable and start the hostapd and dnsmasq services:

sudo systemctl enable hostapd sudo systemctl start hostapd sudo systemctl enable dnsmasq sudo systemctl start dnsmasq 
Enter fullscreen mode Exit fullscreen mode

Step 6: Reboot and Test

Finally, reboot the Raspberry Pi:

sudo reboot 
Enter fullscreen mode Exit fullscreen mode

After rebooting, your Raspberry Pi should now be broadcasting the PiRouter SSID. Devices should be able to connect to it, and DHCP should assign IPs in the 192.168.50.x range.


Troubleshooting

  • Slow speeds? Make sure you’ve configured WMM (wmm_enabled=1), 802.11ac (ieee80211ac=1), and check for interference in the 5 GHz band.
  • Devices not connecting? Ensure that your device supports 802.11ac or ax for higher speeds.
  • No IP address? Double-check the dnsmasq config for correct DHCP range and ensure wlan1 is properly assigned an IP.

Conclusion

With just a few steps, you can turn your Raspberry Pi into a powerful Wi-Fi access point. This setup will improve your network’s performance and allow you to share your internet connection over 5 GHz Wi-Fi, with high-speed access and support for multiple devices.

Feel free to tweak the configuration to fit your needs, and let me know if you have any questions!


Let me know if you’d like me to tweak anything or add more details!

Top comments (0)