【Memo】Setting Up a Raspberry Pi 3 as a Wireless Router

The Raspberry Pi 3, released in early March, comes with built-in WiFi and Bluetooth, and combined with its existing ethernet port, it's basically a wireless router already. I couldn't resist getting one myself, planning to use it as a router and NAS. There are already plenty of tutorials on turning a Raspberry Pi into a router, though of course almost all of them are based on the Raspberry Pi 2 — versions before the 3 didn't have built-in WiFi, so you had to configure your own wireless network card. Since the 3 comes with one built in, the configuration is much more convenient. I referred to two foreign-language tutorials and got it working successfully, so I'm noting it down here.

Reference tutorials:

https://frillip.com/using-your-raspberry-pi-3-as-a-wifi-access-point-with-hostapd/

https://gist.github.com/Lewiscowles1986/fecd4de0b45b2029c390#file-rpi3-ap-setup-shmore

Setting Up the Wireless Hotspot

The main software packages used are hostapd and dnsmasq:

sudo apt-get install hostapd dnsmasq

Then, at the end of /etc/dnsmasq.conf, add the following (adjust the IP and subnet yourself; this file already exists and is a very detailed config file, but every line in it is commented out with a # by default):

interface=wlan0
dhcp-range=10.0.0.2,10.0.0.5,255.255.255.0,12h

Next, create /etc/hostapd/hostapd.conf and add:

interface=wlan0
hw_mode=g
channel=10
auth_algs=1
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP
wpa_passphrase=your wifi password
ssid=your wifi name

Then modify /etc/sysctl.conf, changing (if this line already exists, just remove the # in front):

net.ipv4.ip_forward=1

Finally, add the following script to /etc/rc.local, before the exit 0 line:

ifconfig wlan0 down
ifconfig wlan0 10.0.0.1 netmask 255.255.255.0 up
iwconfig wlan0 power off
service dnsmasq restart
hostapd -B /etc/hostapd/hostapd.conf & > /dev/null 2>&1
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE  
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT  
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

Reboot, and you should see the hotspot — much simpler than expected. The Raspberry Pi's WiFi signal strength is about on par with that once-famous little router toy, the wr703n.

Pitfalls Encountered and Fixed

While I was at it, I also set up offline downloading, NAS, and automatic cloud sync, among other things. Since I'm not very familiar with Linux, I ran into quite a few pitfalls. A reminder for everyone: many commands on the Raspberry Pi need to be prefixed with sudo, and sudo on the Raspberry Pi doesn't require a password. However, running something with sudo versus without sudo is essentially like being in two completely different environments (two different users). For instance, after running sudo screen -S sync, running screen -ls (without sudo) won't show it — you have to use sudo screen -ls to see it. Also, if you add a command to /etc/rc.local to run at startup, it will be executed as sudo by default (regardless of whether you added sudo or not yourself). As a result, I added a screen task there, and after booting, no matter how much I tried screen -ls, I couldn't see it — turns out I needed sudo screen -ls. I was completely baffled... Also, when running autossh for reverse tunneling through NAT, you must add a sleep 5 command before autossh, otherwise autossh runs for nothing.

I spent a whole day falling into and climbing out of these pitfalls.

English translation of a post from 科学空间 | Scientific Spaces by 苏剑林. Original: https://kexue.fm/archives/3728
Translated automatically with claude-sonnet-5; all equations are reproduced verbatim from the source. Copyright remains with the original author.