搭建樹莓派無線路由器

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

翻譯: