搭建树莓派无线路由器

1. 采购硬件

  • 树莓派(去年买的Raspberry Pi 2 Model B 1GB 一直吃灰,不能忍)
  • USB无线网卡(购买链接:http://item.jd.com/509932.html),新版树莓派3自带无线模块,应该无需购买
  • 选用设备
    • 网线(方便调试)

2. 装树莓派官方系统,更新,基础配置

  • 略(不会配置,找个文档看看)

3. 安装 hostapd + udhcpd

apt-get install -y hostapd udhcpd
  • udhcpd 配置

      root@raspberrypi:~# cat /etc/udhcpd.conf
      start 192.168.42.2 # This is the range of IPs that the hostspot will give 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
    
  • hostapd 配置

    • 注意driver=rtl871xdrv 不是nl80211,因为奶茶东的usb网卡硬件不一样(rtl8192cu)
    • 替换/usr/sbin/hostapd这个二进制文件(外国友人链接:https://dl.dropboxusercontent.com/u/1663660/hostapd/hostapd)

    因为硬件的适配性问题,默默调试了3-4h,各种搜(树莓派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配置

	root@raspberrypi:~# cat /etc/hostapd/hostapd.conf
	interface=wlan0
	driver=rtl871xdrv
	ssid=行者孙
	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. 内核 iptable 网络 配置 启动项

  • 内核

      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
    
  • 网络配置

      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
    
  • 启动项

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

4. 手机电脑连接测试,效果感人

测试速度很快。主观自我肯定,客观硬件配置的确秒杀绝大部分路由器

ssid:行者孙
password: haha1234

5. 总结

Hi, GNU/linux 陪伴是最长情的告白,等我财务自由了我赚钱给你!

  • 消灭没有女朋友的无聊的人山人海的五一假期
  • 拥有一个自由控制的linux router
  • 自我陶醉
  • 物尽所用
最后修改于: Monday, August 28, 2023
欢迎关注微信公众号,留言交流。

翻译: