Building a Raspberry Pi Wireless Router

1. Purchasing Hardware

  • Raspberry Pi (I bought a Raspberry Pi 2 Model B 1GB last year that's been gathering dust for ages. I can't stand it)
  • USB wireless network card (purchase link: http://item.jd.com/509932.html). The new Raspberry Pi 3 comes with a built-in wireless module, so you shouldn't need one.
  • Optional Equipment
  • Ethernet cable (for debugging)

2. Install the official Raspberry Pi operating system, update, and perform basic configuration.

  • Omitted (If you don't know how to configure, find documentation to consult)

3. Install hostapd and udhcpd

apt-get install -y hostapd udhcpd

  • udhcpd Configuration

root@raspberrypi:~# cat /etc/udhcpd.conf start 192.168.42.2 # This is the range of IP addresses that the hostspot will provide to client devices. end 192.168.42.20 interface wlan0 # The device uDHCP listens on. Remaining yes #opt dns 8.8.8.8 4.2.2.2 # The DNS servers client devices will use. opt dns 223.5.5.5 223.6.6.6 # The DNS servers client devices will use. opt subnet 255.255.255.0 opt router 192.168.42.1 # The Pi's IP address on wlan0, which we will set up shortly. opt lease 864000 # 10-day DHCP lease time in seconds

Due to hardware compatibility issues, I silently debugged for 3-4 hours and searched various options (this issue should not exist on the Raspberry Pi 3).

root@raspberrypi:~# lsusb |grep WLAN Bus 001 Device 004: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter

root@raspberrypi:~# lshw -C network *-network:0 description: Wireless interface physical id: 1 bus info: usb@1:1.3 logical name: wlan0 serial: e8:4e:06:2a:5e:73 capabilities: ethernet physical wireless configuration: broadcast=yes driver= rtl8192cu ip=192.168.42.1 multicast=yes wireless=IEEE 802.11bg

hostapd configuration

root@raspberrypi:~# cat /etc/hostapd/hostapd.conf interface=wlan0 driver=rtl871xdrv ssid=Walker Sun hw_mode=g channel=6 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase=haha1234 wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP

4. Kernel iptables Network Configuration Boot Items

  • Kernel

root@raspberrypi:~# grep net.ipv4.ip_forward=1 /etc/sysctl.conf net.ipv4.ip_forward=1

  • iptables

root@raspberrypi:~# cat /etc/iptables.ipv4.nat

Generated by iptables-save v1.4.14 on Sat Apr 30 16:59:41 2016

*filter :INPUT ACCEPT [23:1360] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [21:3136] -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT -A FORWARD -i wlan0 -o eth0 -j ACCEPT COMMIT

Completed on Sat Apr 30 16:59:41 2016

Generated by iptables-save v1.4.14 on Sat Apr 30 16:59:41 2016

*nat :PREROUTING ACCEPT [2:156] :INPUT ACCEPT [2:156] :OUTPUT ACCEPT [1:108] :POSTROUTING ACCEPT [0:0] -A POSTROUTING -o eth0 -j MASQUERADE COMMIT

Completed on Sat Apr 30 16:59:41 2016

  • Network Configuration

root@raspberrypi:~# cat /etc/network/interfaces auto lo iface lo inet loopback iface eth0 inet dhcp

auto eth0 allow-hotplug eth0

auto wlan0 allow-hotplug wlan0 iface wlan0 inet static address 192.168.42.1 netmask 255.255.255.0

pre-up iptables-restore < /etc/iptables.ipv4.nat

  • Startup

root@raspberrypi:~# history | grep enable 103 update-rc.d udhcpd enable 104 update-rc.d hostapd enable

4. Testing the connection between phone and computer: impressive results

The test speed is very fast. Subjectively, I'm confident that my hardware configuration is indeed superior to most routers.

SSID: Xingzhesun Password: haha1234

5. Summary

Hi, GNU/Linux. Companionship is the most enduring confession of love. When I'm financially independent, I'll earn money for you!

  • Eliminating the boredom of a crowded May Day holiday without a girlfriend
  • Owning a Linux router with free control
  • Self-indulgence
  • Making the most of everything
Lastmod: Wednesday, August 6, 2025

Translations: