English (unofficial) translations of posts at kexue.fm
Source

[Memo] Using Raspberry Pi 3 as a Wireless Router

Translated by DeepSeek V4 Pro. Translations can be inaccurate, please refer to the original post for important stuff.

The Raspberry Pi 3, released in early March, comes with built-in WiFi and Bluetooth. Since it already has an Ethernet port, it is essentially a wireless router. I couldn’t help but get one, intending to use it as a router and NAS. There are already many tutorials on making a router with a Raspberry Pi, mostly based on the Raspberry Pi 2. Versions before the 3 did not have built-in WiFi, so you needed to configure a wireless network card yourself. Since the 3 has built-in WiFi, configuration is much more convenient. I successfully configured it by referring to two foreign tutorials and am recording it here.

Reference tutorials:

Configuring the Wireless Hotspot

The main software used are hostapd and dnsmasq:

sudo apt-get install hostapd dnsmasq

Then add the following to the end of /etc/dnsmasq.conf (modify the IP and network segment yourself; this file already exists and is a very detailed configuration file, but all lines are commented out with #):

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

Then create a new file /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

Next, modify /etc/sysctl.conf and change (if this line exists, just remove the #):

net.ipv4.ip_forward=1

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

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

After restarting, you can see the hotspot. It’s much simpler  The WiFi signal strength of the Raspberry Pi is similar to the once-famous router toy WR703N.

Pitfalls and Solutions

I also configured offline downloading, NAS, automatic cloud synchronization, etc. Since I am not very familiar with Linux, I fell into many traps. I want to remind everyone that many commands on the Raspberry Pi need to start with sudo, and sudo on the Raspberry Pi does not require a password. However, having sudo and not having sudo are two completely different environments (two different users). For example, after sudo screen -S sync, you cannot see it with screen -ls; you must use sudo screen -ls to see it. Additionally, if you add commands to /etc/rc.local to run, they are executed as sudo by default (regardless of whether you added sudo). As a result, I added a screen task here, and after starting, I couldn’t see it with screen -ls no matter what. It turned out I needed sudo screen -ls, I’m dizzy... Also, when running autossh for intranet penetration, you must add a sleep 5 command before autossh, otherwise autossh won’t work even if it runs  

These were all traps I fell into for a whole day.

Please include the address of this article when reposting: https://kexue.fm/archives/3728

For more detailed reposting matters, please refer to: Scientific Space FAQ