Skip to content

Instantly share code, notes, and snippets.

@letssudormrf
Forked from snakevil/router.pi-2.md
Created November 3, 2018 13:26
Show Gist options
  • Select an option

  • Save letssudormrf/b5b8bcb5863a3fa9568d8da883ecb746 to your computer and use it in GitHub Desktop.

Select an option

Save letssudormrf/b5b8bcb5863a3fa9568d8da883ecb746 to your computer and use it in GitHub Desktop.
使用树莓派3B打造超强路由之二:初成

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

新款的树莓派3B功能之丰富,性能之强悍,让我垂涎。考虑到家里的网件 WNDR3700v2 也服役四年有余了。还是败了一个树莓派3B回来打造成新的路由。

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

〇 前文提要

《使用树莓派3B打造超强路由之一:初装》

一 添加网卡

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

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

这张外接网卡的理论上限通信速度受限于树莓派3B的 USB2 口效率,可以达到 480Mbps。比板载有线网卡的 100Mbps 还是能强上不少的。

ifconfig 指令截屏

二 调整网络

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

三 开启 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