Repository navigation
Turn Raspberry Pi into Wifi Router using DHCP, DNS and IP masquerading
A Raspberry Pi within an Ethernet network can be used as a wireless access point, creating a secondary network. The resulting new wireless network is entirely managed by the Raspberry Pi. A routed wireless access point can be created using the inbuilt wireless features of the Raspberry Pi 4.
- Raspberry Pi(we use a 4 but a 3 or Zero W will suffice)
- Admin access
- Internet access via ethernet OR a completely up-to-date OS including the hostapd, dnsmasq and netfilter-persistent packages(see Step 1)
- Knowledge of the IP configuration from the LAN/Internet, specifically the subnet used, so that a different subnet can be used for the Wifi network we are building
- Example: If the ethernet network provides a 192.168.1.0/24 subnet, we could a 10.20.1.0/24 with no issues
- Something to test the Wifi network once established: PC, phone, etc.
NOTE: This step can be done before any of the others if internet access is an issue while finishing the setup.
NOTE: Its sometimes beneficial to run these two in order a couple of times for older versions of Linux
sudo apt-get updatesudo apt-get upgrade
sudo apt install hostapdsudo systemctl unmask hostapdsudo systemctl enable hostapd
sudo apt install dnsmasq
sudo DEBIAN_FRONTEND=noninteractive apt install -y netfilter-persistent iptables-persistent
sudo nano /etc/dhcpcd.conf
Go to the end of the file and add the following:
interface wlan0 static ip_address=10.20.1.1/24 nohook wpa_supplicant
sudo nano /etc/sysctl.d/routed-ap.conf
File contents:
# https://www.raspberrypi.org/documentation/configuration/wireless/access-point-routed.md # Enable IPv4 routing net.ipv4.ip_forward=1
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADEsudo netfilter-persistent save
Rename the default configuration file and edit a new one:
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.origsudo nano /etc/dnsmasq.conf
Add the following to the file and save it:
interface=wlan0 # Listening interface dhcp-range=10.20.1.2,10.20.1.20,255.255.255.0,24h # Pool of IP addresses served via DHCP domain=wlan # Local wireless DNS domain address=/gw.wlan/10.20.1.1 # Alias for this router
Countries around the world regulate the use of telecommunication radio frequency bands to ensure interference-free operation.
The Linux OS helps users comply with these rules by allowing applications to be configured with a two-letter "WiFi country code", e.g. US for a computer used in the United States.
In the Raspberry Pi OS, 5 GHz wireless networking is disabled until a WiFi country code has been configured by the user, usually as part of the initial installation process (see wireless configuration pages in this section for details.)
To ensure WiFi radio is not blocked on your Raspberry Pi, execute the following command:
sudo rfkill unblock wlan
sudo nano /etc/hostapd/hostapd.conf
This configuration assumes we are in the US, using channel 36, with a network name of aspc, and a password teamroot.
country_code=US
interface=wlan0
ssid=aspc
hw_mode=a
channel=36
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=teamroot
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
Possible values for hw_mode are:
- a = IEEE 802.11a (5 GHz) (Raspberry Pi 3B+ onwards)
- b = IEEE 802.11b (2.4 GHz)
- g = IEEE 802.11g (2.4 GHz)
Note that when changing the hw_mode, you may need to also change the channel - see Wikipedia for a list of allowed combinations.