Skip to content

Turn Raspberry Pi into Wifi Router using DHCP, DNS and IP masquerading

Logan R. Crowe edited this page Apr 26, 2021 · 1 revision

Source

Turn Raspberry Pi into Wifi Router using DHCP, DNS and IP masquerading

Introduction:

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.

Before you start, you will need:

  • 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.

Step 1: Update and Install Packages(requires Internet access)

NOTE: This step can be done before any of the others if internet access is an issue while finishing the setup.

Update Linux packages and dependencies

NOTE: Its sometimes beneficial to run these two in order a couple of times for older versions of Linux

  • sudo apt-get update
  • sudo apt-get upgrade

Install hostapd and enable it

  • sudo apt install hostapd
  • sudo systemctl unmask hostapd
  • sudo systemctl enable hostapd

Install dnsmasq

  • sudo apt install dnsmasq

Install netfilter-persistent

  • sudo DEBIAN_FRONTEND=noninteractive apt install -y netfilter-persistent iptables-persistent

Step 2: Set up the network router

Define the wireless interface IP configuration

  • 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

Enable routing and IP masquerading

  • 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 MASQUERADE
  • sudo netfilter-persistent save

Configure the DHCP and DNS services for the wireless network

Rename the default configuration file and edit a new one:

  • sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
  • sudo 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

Step 3: Ensure wireless operation

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

Step 4: Configure the access point software

  • 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.