Skip to content

Instantly share code, notes, and snippets.

@tjnh05
Forked from snakevil/router.pi-2.md
Created January 27, 2020 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjnh05/473045b1b6a53805f59410b9a9335dcd to your computer and use it in GitHub Desktop.
Save tjnh05/473045b1b6a53805f59410b9a9335dcd to your computer and use it in GitHub Desktop.
使用树莓派3B打造超强路由之二:初成

使用树莓派3B打造超强路由之二:初成

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

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

〇 前文提要

  1. 《初装》

一 添加网卡

一台能正常工作的无线路由器,至少需要两张有线网卡,和一张无线网卡。其中一张有线网卡作为 WAN 口负责与上级网络地数据交换。另一张有线网卡作为 LAN 口,与无线网卡 WLAN 桥接组成局域网络,负责与内网数据交换。

树莓派3B板载地是一张 10/100M 有线网卡 eth0,因此我又特意再败了一张免驱动的 USB3 10/100/1000M 有线网卡 eth1 来做 LAN 口。网卡插上后,在系统中立马可以看到(黄色高亮部分)。

这张外接网卡的理论上限通信速度受限于树莓派3B的 USB2 口效率,可以达到 480Mbps。虽然会和之后外接移动硬盘抢带宽,但大部分情况下,比板载有线网卡的 100Mbps 还是能强上不少的。

ifconfig 指令截屏

二 调整网络

使用网络桥接技术,将 LAN 口有线网卡 eth1 和 WLAN 无线网卡 wlan0 组成单一网桥设备 br0,使有线连接地设备与无线连接地设备能处于同一局域网络内。

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 内容编辑截屏

体贴地提供可复制版本,注意保持格式:

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

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 指令截屏

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

OS X 无线网络扫描结果

四 开启 DHCP

Raspbian Jessie 的 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-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

应用配置使服务生效。

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

systemctl status 指令截屏

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

五 验证

其实此时此刻在整理这篇文档的时候,我就是通过无线连接到树莓派的网络。

使用另外一台设备连接到树莓派的网络,检查通信是否正常:

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

mtr 指令截屏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment