A lot of persnickety typing follows, so you’ll probably want to use the “copy code” button to get these commands and settings exact.

Lets make sure the Pi’s operating system and utilities are fresh and up-to-date:

sudo apt update
sudo apt -y upgrade

The second line in particular may take a little while; perhaps 10 minutes. Take a moment to stretch or get a drink of water. Then:

sudo apt install -y hostapd dnsmasq
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo DEBIAN_FRONTEND=noninteractive apt install -y netfilter-persistent iptables-persistent
sudo reboot

The official Pi directions omit the reboot step, but it does seem to be necessary before certain commands later. The system will kick you off at this point. Allow a minute or so for the reboot, then log back in for more commands…

sudo nano /etc/dhcpcd.conf

Here we’re using the nano editor, but you can substitute another editor of your choice. That goes for other files being edited later as well.

There’s a few dozen lines of stuff in dhcpcd.conf. Go to the end of the file and add the following at the bottom:

interface wlan0
    static ip_address=192.168.4.1/24
    nohook wpa_supplicant

(The latter two lines are indented by four spaces. This is correct and normal; do not “clean up.”)

Save the changes and exit nano (if that’s what you’re using) by pressing CTRL-O and CTRL-X. Then we’ll edit another:

sudo nano /etc/sysctl.d/routed-ap.conf

This file is likely empty, so you’ll simply be filling it with:

# Enable IPv4 routing
net.ipv4.ip_forward=1

Save and exit, then edit another:

sudo nano /etc/dnsmasq.conf

Everything in this file is commented out by default, so you can insert the following lines anywhere…beginning, end, or delete everything there and replace with just these lines, it’s all good:

interface=wlan0 # Listening interface
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
                # Pool of IP addresses served via DHCP
domain=wlan     # Local wireless DNS domain
address=/gw.wlan/192.168.4.1
                # Alias for this router

Save and exit that, then there’s one more file to edit:

sudo nano /etc/hostapd/hostapd.conf

This one’s likely empty too. Begin with the following:

country_code=US
interface=wlan0
ssid=WirelessNetworkName
hw_mode=a
channel=48
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=WirelessNetworkPassword
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

This is just a starting point, and several lines should be changed:

WirelessNetworkName is the name (or “SSID”) one sees in the WiFi network selection menu on your computer or phone; replace with something descriptive.

WirelessNetworkPassword is the corresponding password for the above.

country_code=US must be edited with the 2-letter country code for your location; the default here is for the United States. 5 GHz WiFi will not work correctly without it! Here is a list of country codes on Wikipedia.

The lines hw_mode=a and channel=48 probably will need to be edited…

hw_mode can be a, b or g, corresponding to different 802.11 WiFi generations. 802.11a is 5 GHz (Pi 3 B+ onwards), the others are 2.4 GHz; for USB WiFi dongles, try g.

channel will depend on hw_mode and your country. If it’s a 2.4 GHz mode (b or g), this is 1–11 (and up to 14 in some places) (List of 2.4 GHz channels). If 5 GHz (hw_mode=a only), the range is much larger and a bit confusing (List of 5 GHz channels — look for ones with a “Yes” in your country/region’s column). 48 worked for me in the US, but you might need to try a few different channels before one sticks, rebooting each time.

If using a non-Adafruit USB WiFi adapter, you might need to add a line: driver=nl80211 or similar, depends on the WiFi chip used within. Check documentation for the item, also there’s no guarantee all such dongles can work with the Pi.

Save and exit, then enter the following commands to finalize everything and restart the system, hopefully bringing up the WiFi network with it.

If outside the United States, change the “US” on the first line here to the 2-letter country code you previously entered in hostapd.conf.

sudo raspi-config nonint do_wifi_country US
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo netfilter-persistent save
sudo reboot

These directions are in a slightly different sequence than the canonical Pi documentation, to minimize going back-and-forth between editing files and entering commands. End result should be the same though.

This guide was first published on Jun 12, 2013. It was last updated on Mar 08, 2024.

This page (Install software) was last updated on Mar 08, 2024.

Text editor powered by tinymce.