Skip to content

Instantly share code, notes, and snippets.

@snakevil
Last active September 27, 2023 02:25
Show Gist options
  • Save snakevil/7d7af1d8ca2c739e3fedc5b15eb8e4aa to your computer and use it in GitHub Desktop.
Save snakevil/7d7af1d8ca2c739e3fedc5b15eb8e4aa to your computer and use it in GitHub Desktop.

Revisions

  1. snakevil revised this gist Aug 5, 2016. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions router.pi-2.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    使用树莓派3B打造超强路由之二:初成
    ===

    [树莓派3B]: https://www.raspberrypi.org/products/raspberry-pi-3-model-b/
    [之一:初装]: https://gist.github.com/snakevil/bcc719a63979141056c4c17e9ce94ff1

    通过第一篇[《使用树莓派3B打造超强路由之一:初装》][之一:初装]的努力,[树莓派3B][]已经可以作为一台超低能耗、随身携带的开发用服务器来使用了。但这对于目标——打造超强路由而言,才刚刚开始。接下来,我们需要将其打磨成一台基本的无线路由器。

    **`WARNING` 本文所有指令均仅供参考,切勿无脑复制粘贴!**
    @@ -13,6 +16,8 @@
    一 添加网卡
    ---

    [网络桥接]: http://baike.baidu.com/view/3379470.htm

    > 一台能正常工作的无线路由器,至少需要两张有线网卡,和一张无线网卡。其中一张有线网卡作为 WAN 口负责与上级网络地数据交换。另一张有线网卡作为 LAN 口,与无线网卡 WLAN [桥接][网络桥接]组成局域网络,负责与内网数据交换。
    [树莓派3B][]板载地是一张 10/100M 有线网卡 `eth0`,因此我又特意再败了一张[免驱动的 USB3 10/100/1000M 有线网卡](http://item.jd.com/10244682386.html) `eth1` 来做 LAN 口。网卡插上后,在系统中立马可以看到(黄色高亮部分)。
    @@ -174,7 +179,3 @@ mtr -c10 -r 223.6.6.6 # 检查到阿里 DNS 的线路
    ```
    ![mtr 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/13.mtr.png/720)
    [树莓派3B]: https://www.raspberrypi.org/products/raspberry-pi-3-model-b/
    [之一:初装]: https://gist.github.com/snakevil/bcc719a63979141056c4c17e9ce94ff1
    [网络桥接]: http://baike.baidu.com/view/3379470.htm
  2. snakevil revised this gist Aug 5, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions router.pi-2.md
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@

    > 这张外接网卡的理论上限通信速度受限于[树莓派3B][]的 USB2 口效率,可以达到 `480Mbps`。虽然会和之后外接移动硬盘抢带宽,但大部分情况下,比板载有线网卡的 `100Mbps` 还是能强上不少的。
    ![ifconfig 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/08.ifconfig.png/720)
    ![ifconfig 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/08.pi.ifconfig.png/720)

    二 调整网络
    ---
    @@ -37,7 +37,7 @@ reboot # 重启生效

    (红色高亮部分是添加内容。)

    ![/etc/network/interfaces 内容编辑截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/09.vi-_etc_network_interfaces.png/720)
    ![/etc/network/interfaces 内容编辑截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/09.pi.vi-_etc_network_interfaces.png/720)

    > 体贴地提供可复制版本,注意保持格式:
    >
    @@ -100,7 +100,7 @@ brctl show br0 # 查看 LAN 网桥状态
    如果实际显示内容与下图黄色高亮部分一致,说明 AP 开始正常工作了。
    ![systemctl status 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/10.systemctl-status.png/720)
    ![systemctl status 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/10.pi.systemctl-status.png/720)
    这个时候我们就可以尝试用其它设备来找名为 `pi` 的 SSID 了。
    @@ -158,7 +158,7 @@ systemctl restart dnsmasq # 重启 DHCP 服务
    systemctl status dnsmasq # 查看 DHCP 服务状态
    ```
    ![systemctl status 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/12.systemctl-status.png/720)
    ![systemctl status 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/12.pi.systemctl-status.png/720)
    **注意** *不要在意这张截图里的时间,本来没打算截这张图,所以是后补的。*
  3. snakevil revised this gist Jul 21, 2016. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions router.pi-2.md
    Original file line number Diff line number Diff line change
    @@ -13,17 +13,19 @@
    一 添加网卡
    ---

    > 一台能正常工作的无线路由器,至少需要两张有线网卡,和一张无线网卡。其中一张有线网卡作为 `WAN` 口负责与上级网络地数据交换。另一张有线网卡作为 `LAN` 口,与无线网卡 `WLAN` 桥接组成局域网络,负责与内网数据交换。
    > 一台能正常工作的无线路由器,至少需要两张有线网卡,和一张无线网卡。其中一张有线网卡作为 WAN 口负责与上级网络地数据交换。另一张有线网卡作为 LAN 口,与无线网卡 WLAN [桥接][网络桥接]组成局域网络,负责与内网数据交换。
    [树莓派3B][]板载地是一张 `10/100M` 有线网卡,因此我又特意再败了一张[免驱动的 USB3 10/100/1000M 有线网卡](http://item.jd.com/10244682386.html)来做 `LAN` 口。网卡插上后,在系统中立马可以看到(黄色高亮部分)。
    [树莓派3B][]板载地是一张 10/100M 有线网卡 `eth0`,因此我又特意再败了一张[免驱动的 USB3 10/100/1000M 有线网卡](http://item.jd.com/10244682386.html) `eth1` 来做 LAN 口。网卡插上后,在系统中立马可以看到(黄色高亮部分)。

    > 这张外接网卡的理论上限通信速度受限于[树莓派3B][]的 USB2 口效率,可以达到 `480Mbps`。比板载有线网卡的 `100Mbps` 还是能强上不少的。
    > 这张外接网卡的理论上限通信速度受限于[树莓派3B][]的 USB2 口效率,可以达到 `480Mbps`虽然会和之后外接移动硬盘抢带宽,但大部分情况下,比板载有线网卡的 `100Mbps` 还是能强上不少的。
    ![ifconfig 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/08.ifconfig.png/720)

    二 调整网络
    ---

    > 使用[网络桥接][]技术,将 LAN 口有线网卡 `eth1` 和 WLAN 无线网卡 `wlan0` 组成单一[网桥][网络桥接]设备 `br0`,使有线连接地设备与无线连接地设备能处于同一局域网络内。
    ```
    sudo -s # 提权至 root
    echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf # 允许 IPv4 转发
    @@ -175,3 +177,4 @@ mtr -c10 -r 223.6.6.6 # 检查到阿里 DNS 的线路
    [树莓派3B]: https://www.raspberrypi.org/products/raspberry-pi-3-model-b/
    [之一:初装]: https://gist.github.com/snakevil/bcc719a63979141056c4c17e9ce94ff1
    [网络桥接]: http://baike.baidu.com/view/3379470.htm
  4. snakevil revised this gist Jul 21, 2016. 1 changed file with 17 additions and 2 deletions.
    19 changes: 17 additions & 2 deletions router.pi-2.md
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@

    > 一台能正常工作的无线路由器,至少需要两张有线网卡,和一张无线网卡。其中一张有线网卡作为 `WAN` 口负责与上级网络地数据交换。另一张有线网卡作为 `LAN` 口,与无线网卡 `WLAN` 桥接组成局域网络,负责与内网数据交换。
    [树莓派3B][]板载地是一张 `10/100M` 有线网卡,因此我又特意再败了一张[免驱动的 USB3 10/100/1000M 有线网卡](http://item.jd.com/10244682386.html) 来做 `LAN` 口。网卡插上后,在系统中立马可以看到(黄色高亮部分)。
    [树莓派3B][]板载地是一张 `10/100M` 有线网卡,因此我又特意再败了一张[免驱动的 USB3 10/100/1000M 有线网卡](http://item.jd.com/10244682386.html)来做 `LAN` 口。网卡插上后,在系统中立马可以看到(黄色高亮部分)。

    > 这张外接网卡的理论上限通信速度受限于[树莓派3B][]的 USB2 口效率,可以达到 `480Mbps`。比板载有线网卡的 `100Mbps` 还是能强上不少的。
    @@ -37,6 +37,21 @@ reboot # 重启生效

    ![/etc/network/interfaces 内容编辑截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/09.vi-_etc_network_interfaces.png/720)

    > 体贴地提供可复制版本,注意保持格式:
    >
    > ```
    > auto eth1
    > iface eth1 inet manual
    >
    > auto br0
    > iface br0 inet static
    > bridge_ports eth1
    > address 10.7.4.1
    > netmask 255.255.255.0
    > broadcast 10.7.4.255
    > up /sbin/iptables-restore < /etc/iptables
    > ```
    三 开启 AP
    ---
    @@ -159,4 +174,4 @@ mtr -c10 -r 223.6.6.6 # 检查到阿里 DNS 的线路
    ![mtr 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/13.mtr.png/720)
    [树莓派3B]: https://www.raspberrypi.org/products/raspberry-pi-3-model-b/
    [之一:初装]: https://gist.github.com/snakevil/bcc719a63979141056c4c17e9ce94ff1
    [之一:初装]: https://gist.github.com/snakevil/bcc719a63979141056c4c17e9ce94ff1
  5. snakevil revised this gist Jul 21, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions router.pi-2.md
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,14 @@
    使用树莓派3B打造超强路由之二:初成
    ===

    新款的[树莓派3B][]功能之丰富,性能之强悍,让我垂涎。考虑到家里的[网件 WNDR3700v2](http://baike.baidu.com/view/8814776.htm) 也服役四年有余了。还是败了一个[树莓派3B][]回来打造成新的路由
    通过第一篇[《使用树莓派3B打造超强路由之一:初装》][之一:初装]的努力,[树莓派3B][]已经可以作为一台超低能耗、随身携带的开发用服务器来使用了。但这对于目标——打造超强路由而言,才刚刚开始。接下来,我们需要将其打磨成一台基本的无线路由器

    **`WARNING` 本文所有指令均仅供参考,切勿无脑复制粘贴!**

    〇 前文提要
    ---

    [《使用树莓派3B打造超强路由之一:初装》](https://gist.github.com/snakevil/bcc719a63979141056c4c17e9ce94ff1)
    1. [初装》][之一:初装]

    一 添加网卡
    ---
    @@ -159,3 +159,4 @@ mtr -c10 -r 223.6.6.6 # 检查到阿里 DNS 的线路
    ![mtr 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/13.mtr.png/720)

    [树莓派3B]: https://www.raspberrypi.org/products/raspberry-pi-3-model-b/
    [之一:初装]: https://gist.github.com/snakevil/bcc719a63979141056c4c17e9ce94ff1
  6. snakevil revised this gist Jul 21, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions router.pi-2.md
    Original file line number Diff line number Diff line change
    @@ -127,6 +127,8 @@ DHCP 服务配置文件内容如下:
    ```
    no-dhcp-interface=eth0
    dhcp-range=lan,10.7.4.240,10.7.4.249
    dhcp-option=tag:lan,option:router,10.7.4.1
    dhcp-option=tag:lan,option:dns-server,10.7.4.1
    dhcp-broadcast=tag:needs-broadcast
    dhcp-authoritative
    dhcp-leasefile=/var/run/dnsmasq/dhcp.lease
  7. snakevil created this gist Jul 20, 2016.
    159 changes: 159 additions & 0 deletions router.pi-2.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,159 @@
    使用树莓派3B打造超强路由之二:初成
    ===

    新款的[树莓派3B][]功能之丰富,性能之强悍,让我垂涎。考虑到家里的[网件 WNDR3700v2](http://baike.baidu.com/view/8814776.htm) 也服役四年有余了。还是败了一个[树莓派3B][]回来打造成新的路由。

    **`WARNING` 本文所有指令均仅供参考,切勿无脑复制粘贴!**

    〇 前文提要
    ---

    [《使用树莓派3B打造超强路由之一:初装》](https://gist.github.com/snakevil/bcc719a63979141056c4c17e9ce94ff1)

    一 添加网卡
    ---

    > 一台能正常工作的无线路由器,至少需要两张有线网卡,和一张无线网卡。其中一张有线网卡作为 `WAN` 口负责与上级网络地数据交换。另一张有线网卡作为 `LAN` 口,与无线网卡 `WLAN` 桥接组成局域网络,负责与内网数据交换。
    [树莓派3B][]板载地是一张 `10/100M` 有线网卡,因此我又特意再败了一张[免驱动的 USB3 10/100/1000M 有线网卡](http://item.jd.com/10244682386.html) 来做 `LAN` 口。网卡插上后,在系统中立马可以看到(黄色高亮部分)。

    > 这张外接网卡的理论上限通信速度受限于[树莓派3B][]的 USB2 口效率,可以达到 `480Mbps`。比板载有线网卡的 `100Mbps` 还是能强上不少的。
    ![ifconfig 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/08.ifconfig.png/720)

    二 调整网络
    ---

    ```
    sudo -s # 提权至 root
    echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf # 允许 IPv4 转发
    iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE # 开启 IPv4 转发
    iptables-save > /etc/iptables # 持久保存转发配置
    vi /etc/network/interfaces # 调整网络
    reboot # 重启生效
    ```

    (红色高亮部分是添加内容。)

    ![/etc/network/interfaces 内容编辑截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/09.vi-_etc_network_interfaces.png/720)

    三 开启 AP
    ---

    ```
    sudo -s # 提权至 root
    apt-get purge -y wpasupplicant # 卸载无线客户端程序
    echo 'DAEMON_CONF="/etc/hostapd/hostapd.conf"' >> /etc/default/hostapd # 指定 AP 配置文件
    vi /etc/hostapd/hostapd.conf # 编辑 AP 配置文件
    ```

    AP 配置文件内容如下(SSID 为 `pi`,初始密钥为 `raspberry`):

    ```
    interface=wlan0
    bridge=br0
    driver=nl80211
    logger_syslog=-1
    logger_syslog_level=2
    logger_stdout=-1
    logger_stdout_level=2
    ctrl_interface=/var/run/hostapd
    ssid=pi
    utf8_ssid=1
    country_code=CN
    hw_mode=g
    channel=11
    macaddr_acl=0
    auth_algs=1
    ignore_broadcast_ssid=0
    wmm_enabled=1
    ieee80211n=1
    ht_capab=[HT40][SMPS-STATIC][SHORT-GI-20][DSSS_CCK-40]
    wpa=2
    wpa_passphrase=raspberry
    wpa_key_mgmt=WPA-PSK
    rsn_pairwise=CCMP
    ```

    ```
    systemctl restart hostapd # 重启 AP
    systemctl status hostapd # 查看 AP 状态
    brctl show br0 # 查看 LAN 网桥状态
    ```

    如果实际显示内容与下图黄色高亮部分一致,说明 AP 开始正常工作了。

    ![systemctl status 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/10.systemctl-status.png/720)

    这个时候我们就可以尝试用其它设备来找名为 `pi` 的 SSID 了。

    ![OS X 无线网络扫描结果](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/11.ssids.png/720)

    四 开启 DHCP
    ---

    > [Raspbian Jessie](http://raspbian.org/) 的 dnsmasq 包当下版本是 `2.72-3`,支持 ipset !所以,你懂地~
    配置本地 DNS 服务,

    ```
    vi /etc/dnsmasq.d/dns # 配置 DNS 服务
    ```

    DNS 服务配置文件内容如下:

    ```
    expand-hosts
    neg-ttl=60
    max-ttl=3600
    max-cache-ttl=3600
    localise-queries
    bogus-priv
    stop-dns-rebind
    rebind-localhost-ok
    domain-needed
    cache-size=4096
    domain=local,10.7.4.0/24,local
    ```

    配置本地 DHCP 服务,

    ```
    vi /etc/dnsmasq.d/dhcp # 配置 DHCP 服务
    ```

    DHCP 服务配置文件内容如下:

    ```
    no-dhcp-interface=eth0
    dhcp-range=lan,10.7.4.240,10.7.4.249
    dhcp-broadcast=tag:needs-broadcast
    dhcp-authoritative
    dhcp-leasefile=/var/run/dnsmasq/dhcp.lease
    ```

    应用配置使服务生效。

    ```
    systemctl restart dnsmasq # 重启 DHCP 服务
    systemctl status dnsmasq # 查看 DHCP 服务状态
    ```

    ![systemctl status 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/12.systemctl-status.png/720)

    **注意** *不要在意这张截图里的时间,本来没打算截这张图,所以是后补的。*

    五 验证
    ---

    > 其实此时此刻在整理这篇文档的时候,我就是通过无线连接到[树莓派][树莓派3B]的网络。
    使用另外一台设备连接到[树莓派][树莓派3B]的网络,检查通信是否正常:

    ```
    mtr -c10 -r 223.6.6.6 # 检查到阿里 DNS 的线路
    ```

    ![mtr 指令截屏](http://oalpzrx5i.bkt.clouddn.com/@snakevil/2016-07/router.pi/13.mtr.png/720)

    [树莓派3B]: https://www.raspberrypi.org/products/raspberry-pi-3-model-b/