跳至主要内容

树莓派3开无线热点变身为智能无线网关(IPv4+IPv6)


无线上六维不是梦,折腾了两天终于终于弄好了~撒花~
这个暑假一直在外实习,回到学校发现实验室已经没有了我的位置。。悲催的被赶到另一个实验楼,这个实验楼主要放的是各种服务器,因此IP比较紧张,以前我自带一个交换机,IP地址随便用,而且都是有线,IPv6默认都可以用,但这边我只分得一根网线,只有一个固定的IPv4的IP,倒是IPv6没有限制(不过后来测试发现还是有些限制)。对于我这么多设备根本不够用嘛。正好看到树莓派3上市了,自带wifi,CPU升级到A53,是服务器级的U了,立刻买了一个回来,当作我的网关。这里我用树莓派搭的其实是一个NAT的路由器,把WAN的ipv4和ipv6都共享给内网LAN,由于是在学校,我的IPv4的地址是固定的,而IPv6的地址动态分配(前缀是固定的),所以不要盲目跟着教程做。这里使用的树莓派是3代树莓派,系统是2016-09-23的raspbian jessie,板载wifi,不需要考虑驱动的问题,如果不是,那么先自行解决驱动问题,这里WAN外网是eth0,LAN内网是无线wlan0,如果接口不一致请自行替换,后面不再做解释。

准备工作

整个过程分为两步,先开IPv4的热点access point,再共享IPv6的热点access point,热点都不是采用bridge方式进行的连接。

固定IP(IPv4)

后面的树莓派默认使用dhcpcd进行ip的配置,因此网上好多关于配置树莓派固定IP的方法都是有点问题(很早的时候是配置/etc/network/interfaces),我们现在配置dhcpcd的配置文件进行固定IP的配置,打开配置文件/etc/dhcpcd.conf

1
sudo vim /etc/dhcpcd.conf

里面内容不少,感兴趣可以查一下,这里直接拖到最下,根据自己的情况加入下面的内容

1
2
3
4
interface eth0
static ip_address=211.187.224.79/24
static routers=211.187.224.16
static domain_name_servers=114.114.114.114

其中ip_address后面接的是CIDR格式的ip地址,/24是指的netmask是255.255.255.0,地址根据自己的情况填一下即可。
重启就可以上网了。ping一下外网看一下是不是已经通了,IPv6是用ping6。

配置IPv4热点access point

这里使用的是NAT转发
先装依赖

1
sudo apt-get install dnsmasq hostapd

之所以使用dnsmasq是因为配置简单,而hostapd是必不可少的虚拟热点的程序,hostapd对ipv4和ipv6都支持。
配置的时候要给我们的wlan0一个固定的内网IP,再次编辑我们的dhcpcd.conf,在最后接上下面的内容

1
2
interface wlan0
static ip_address=10.0.0.1/24

继续配置dnsmasq,在/etc/dnsmasq.conf的最后加上下面一段(此文件内容很多,但都被注释掉了)

1
2
interface=wlan0
dhcp-range=10.0.0.2,10.0.0.5,255.255.255.0,12h

编辑/etc/hostapd/hostapd.conf文件(新文件),加上下面的内容

1
2
3
4
5
6
7
8
9
10
interface=wlan0
hw_mode=g
channel=10
auth_algs=1
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP
wpa_passphrase=密码
ssid=名称

把密码和名称替换成自己想设置的就可以了。接着修改/etc/sysctl.conf文件,找到下面一行去掉#

1
net.ipv4.ip_forward=1

继续,更新下iptables的规则:

1
2
3
4
5
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

最后一句的目的是将我们执行的三句iptable设置保存下来,以后能直接使用,我们保存到了/etc/iptables.ipv4.nat位置。编辑/etc/network/interfaces,最后加上下面这句话,每次开机都会配置好iptables的内容了

1
up iptables-restore < /etc/iptables.ipv4.nat

既然打开这个文件了,可以把上面的几句wlan的interface注释一下,反正这些话也没用了,即变成这样:

1
2
3
4
5
6
7
allow-hotplug wlan0
iface wlan0 inet manual
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
#allow-hotplug wlan1
#iface wlan1 inet manual
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

为了能够每次开机自动运行hostapd,我们还要修改/etc/rc.local文件,在exit 0 前加上下面这句话

1
hostapd -B /etc/hostapd/hostapd.conf & > /dev/null 2>&1

重启,就可以看到热点了。至此IPv4部分完成。
参考资料:
  1. 树莓派3内置WIFI无线路由器AP热点: https://item.congci.com/-/content/shu-mei-pai-3-neizhi-wifi-wuxian-luyouqi-ap-redian
  2. 这个文章还提到了桥接方式进行连接的方法: http://wangye.org/blog/archives/845/

配置IPv6热点access point

前面的配置方法网上一搜一大把,这里其实就是记录一下,真正花了我两天时间的是配置IPv6的无线热点。。类似IPv4的配置方法,这里主流配置有两种,一种是使用桥接把无线的IPv6流量转到有线的IPv6上,这种方法配置简单,但我用上后只能坚持几十秒,整个树莓派就再也不能上网,只能重启(非常怀疑学校里在配置了交换机,不让使用桥接),另一种就是我最后成功的使用radvd+npd6+DHCPv6+hostapd的这种NDP连接。
这里也写一下第一种方法,毕竟曾让我高兴了几十秒,本着记录为目的的话还是要记一下。两种方法只能选一种,强烈建议使用第二种方法

IPv6 桥接

编辑配置文件/etc/sysctl.conf,把下面一行的注释去掉

1
net.ipv6.conf.all.forwarding=1

用ifconfig看一下自己的ipv6的地址,注意Scope:Global的那个地址是你的IPv6外网地址,Scope:Link的那个地址应该是你的上级地址。我们这里只关注Global那一项,安装radvd

1
2
3
4
5
6
7
8
9
10
11
12
sudo apt-get install radvd
vim /etc/radvd.conf
interface wlan0
{
AdvSendAdvert on;
prefix 2001:da8:7001:251::/64
{
AdvOnLink on;
AdvAutonomous on;
AdvRouterAddr on;
};
};

这里的2001:的地址就是我的Global那一项的地址
装bridge-utils和ebtables

1
sudo apt-get install bridge-utils ebtables

在home下写个脚本ipv6_bridge.sh,内容如下

1
2
3
4
5
6
7
8
9
#!/bin/sh
iptables -t nat -A POSTROUTING -s 10.0.0.0/8 -o ppp0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
service radvd start
brctl addbr br0
ifconfig br0 up #启动网桥
brctl addif br0 eth0
brctl addif br0 wlan0 #桥接两块网卡
ebtables -t broute -A BROUTING -p ! ipv6 -j DROP #只允许ipv6包通过网桥

给他加上执行权限,重启后sudo执行就可以了

1
2
3
chmod a+x ipv6_bridge.sh
sudo reboot
sudo ./ipv6_bridge.sh

这时候如果没有问题,那你就完成了IPv4 NAT转发,IPv6桥接的双栈无线热点。
参考资料:
  1. 树莓派扩展 IPv6无线路由配置(二) http://blog.csdn.net/lingjun_love_tech/article/details/41382625
  2. Linux 用作 IPv6 网关 https://bigeagle.me/2011/11/linux-as-ipv6-gateway/index.html
  3. 讨论这个的还是OpenWRT比较多,可以参考一下 http://www.openwrt.org.cn/bbs/thread-7116-1-1.html

IPv6 NDP IPv6

IPv6是没有NAT的,NAT对于我们是很熟悉,IPv6由于地址足够多,没有使用NAT技术,他用的是NDP( Neighbour Discovery Protocol 邻居发现协议), NDP有点类似NAT,在NDP协议中,内网的IPv6地址也是外网地址,可以向外发送请求,但不可再路由,这时就需要让具有外网地址的Router帮忙告诉它的上层Router,这些外网地址在这个Router的内网中。radvd就支持NDP,
先安装依赖

1
sudo apt-get install radvd wide-dhcpv6-server

如果wide-dhcpv6-server提示转发interface,就写wlan0
编译安装npd6

1
2
3
4
git clone https://github.com/npd6/npd6
cd npd6
make
sudo make install

首先我们先配置/etc/sysctl.conf,找到合适的地方加上下面两句

1
2
net.ipv6.conf.all.proxy_ndp=1
net.ipv6.conf.all.forwarding=1

用ifconfig看一下自己的ipv6的地址,注意Scope:Global的那个地址是你的IPv6外网地址,Scope:Link的那个地址应该是你的上级地址。
写一个脚本ndp_ipv6.sh 内容如下:

1
2
3
4
5
6
7
8
9
#!/bin/sh
ip -6 addr del 2001:da8:7001:251:fe54:7ee0:6994:5fb7/64 dev eth0
ip -6 addr add 2001:da8:7001:251:fe54:7ee0:6994:5fb7/126 dev eth0
ip -6 addr add 2001:da8:7001:251:1::/80 dev wlan0
ip -6 route add default via fe80::f7ce:a905:9347:7144 dev eth0 metric 256
ip -6 route add 2001:da8:7001:251:1::/80 dev wlan0
service radvd restart
service npd6 restart

先解释下这个脚本,2001:da8:7001:251:fe54:7ee0:6994:5fb7是我的Global的IPv6地址,这个原样根据自己的地址修改,后面的/64和/126不要动(64可能要改成你查到的perfix长度,126是不能动的,防止错乱),2001:da8:7001:251:1::/80是内网分配的地址前缀,前四段跟Global的地址一样,后面随便加个数就可以,这里直接写1,接下来那个fe80开头的地址换成你的Link的那个地址,加上执行权限,脚本先放在这里。
配置radvd
编辑/etc/radvd.conf(可能是新文件),写入下面的内容:

1
2
3
4
5
6
7
8
9
10
11
interface wlan0
{
AdvSendAdvert on;
AdvManagedFlag on;
AdvOtherConfigFlag on;
prefix 2001:da8:7001:251:1::/80 {
AdvRouterAddr off;
AdvOnLink on;
AdvAutonomous on;
};
};

注意上面的2001开头地址要换成你刚才设定好的内网地址。
配置npd6
npd6是邻居发现代理,编辑/etc/npd6.conf,写入下面的内容:

1
2
3
4
5
6
7
8
9
10
11
prefix=2001:da8:7001:251:1:
interface = eth0
ralogging = off
listtype = none
listlogging = off
collectTargets = 100
linkOption = false
ignoreLocal = true
routerNA = true
maxHops = 255
pollErrorLimit = 20

注意2001开头那个地址还是要填你自己的内网地址。
配置DHCPv6
编辑文件/etc/wide-dhcpv6/dhcp6s.conf,写入下面的内容,

1
2
3
4
5
6
interface wlan0{
address-pool pool1 86400;
};
pool pool1 {
range 2001:da8:7001:251:1::200 to 2001:da8:7001:251:1::300;
};

这是圈定了你的内网范围。好了,重启后sudo执行脚本ndp_ipv6.sh

1
2
chmod a+x ndp_ipv6.sh
sudo ./ndp_ipv6.sh

当然完全可以让他开机自启动,编辑/etc/rc.local,在exit 0前加上

1
/home/pi/ndp_ipv6.sh

参考资料:
  1. 从零开始,把Raspberry Pi打造成双栈11n无线路由器,支持教育网原生IPv6(原网址打不开了) https://hahaschool.net/tag/linux/
  2. Openwrt的一篇资料http://blog.martianz.cn/article/2013-05-27-openwrt-ipv6
  3. http://talk.withme.me/?p=51
至此ipv4+ipv6无线热点配置完成!经过一段时间测试,比较稳定,信号还是可以的,撒花~

Popular posts from 产品随想的博客

产品随想 | 周刊 第130期:集结信徒,而非官僚

On Dyson, techno-centric design and social consumption   https://2earth.github.io/website/20250707.html 如何創造偉大的事物   https://ryolu.notion.site/1610a94b9c108079a95be4362afd4a26 集結信徒,而非官僚 Reflections on OpenAI   https://calv.info/openai-reflections 创业架构 Shui   https://github.com/rock-zhang/Shui 好好喝水 https://www.ghibli.jp/works/ 吉卜力作品的高清图 From Skeuomorphic to Liquid Glass: Apple's Strategic Bet on the Post-Touch Future   https://omc345.substack.com/p/from-skeuomorphic-to-liquid-glass 迄今为止关于苹果Liquid Glass变革的解读,最好的一篇 The Nueva School   https://en.wikipedia.org/wiki/The_Nueva_School?useskin=vector 看起来是很酷的一个学校 The Barbican   https://arslan.io/2025/05/12/barbican-estate/ 史蒂夫·乔布斯希望你阅读的 9 本书   https://www.douban.com/doulist/147158849/ 《禅者的初心》里有句话:“做任何事,其实都是在展示内心的天性,这是我们存在的唯一理由。” Chuck Feeney was one of the greatest philanthropists ever   https://www.gatesnotes.com/Remembering-Chuck-Feeney 慈善家 My new deadline: 20 years to give away virtually all my wealt...

Apple's One-Dollar-a-Year Man, By Steve Jobs, 2000

(FORTUNE Magazine) – Now that Steve Jobs has showed his hand on Apple's Internet and system software strategies and dropped the "interim" from his title, other questions loom. He's always denied it, but isn't it true that his old company, Next, did wind up taking over Apple? Will there ever be an encore to the 15-year-old Macintosh? Short of that, does Apple have any plans to jump into the "Internet appliance" fray? Will Apple ever build computers for business people again? And what, pray tell, does Steve think of all these young Internet zillionaires? Let's ask. Practically every technology that your old company, Next, possessed when Apple acquired it in 1997 is now being used by Apple in some strategic way. This must seem like sweet vindication.  The thing about Next was that we produced something that was truly brilliant for an audience that our heart really wasn't into selling to--namely, the enterprise. I suppose if you were wr...

《Becoming Steve Jobs》Chapter 16 Blind Spots, Grudges, and Sharp Elbows

Steve could be pretty thin-skinned when someone prominent criticized the aesthetics of his products. He took great umbrage that Neil would, as Steve put it, “pop off in public like that without coming to talk to us about his technical concerns first.” From that point on he had rebuffed all of Neil’s attempts to smoke the peace pipe. 有趣 He had blind spots, grating behavioral habits, and a tendency to give in to emotional impulse that persisted his entire life. These characteristics are often used to make the case that Steve was an “asshole” or a “jerk,” or perhaps simply “binary”—that odd adjective often used to convey the sense that he was half asshole/half genius from birth to death. These aren’t useful, interesting, or enlightening descriptions. What’s more illuminating is to take a look at the specific ways in which Steve failed to do an effective job of tempering some of his weaknesses and antisocial traits, and to consider how, when, and why some of them continued to flare up even...

A Sister’s Eulogy for Steve Jobs

I grew up as an only child, with a single mother. Because we were poor and because I knew my father had emigrated from Syria, I imagined he looked like Omar Sharif. I hoped he would be rich and kind and would come into our lives (and our not yet furnished apartment) and help us. Later, after I’d met my father, I tried to believe he’d changed his number and left no forwarding address because he was an idealistic revolutionary, plotting a new world for the Arab people. Even as a feminist, my whole life I’d been waiting for a man to love, who could love me. For decades, I’d thought that man would be my father. When I was 25, I met that man and he was my brother. By then, I lived in New York, where I was trying to write my first novel. I had a job at a small magazine in an office the size of a closet, with three other aspiring writers. When one day a lawyer called me — me, the middle-class girl from California who hassled the boss to buy us health insurance — and said his cl...

产品爱好者周刊 第26期:PRISM, XKeyscore, Trust No One

  Products Gitea - Git with a cup of tea   https://gitea.io/en-us/ A painless self-hosted Git service. 自建Git服务,避免GitHub隐私侵犯 https://github.com/objective-see/LuLu LuLu is the free macOS firewall 监视Mac的出站流量,且阻断 OverSight   https://github.com/objective-see/OverSight OverSight monitors a mac's mic and webcam, alerting the user when the internal mic is activated, or whenever a process accesses the webcam. 监视是否有应用调用Mac的麦克风、摄像头 Mozilla Hubs   https://github.com/mozilla/hubs The client-side code for Mozilla Hubs, an online 3D collaboration platform that works for desktop, mobile, and VR platforms. 开源的多人虚拟空间,Mozilla打造,企业级VR诉求 数字移民   https://shuziyimin.org 关于内容源、工具的推荐,适合刚接入国际的新人 SimpleLogin   https://simplelogin.io/ 匿名邮箱工具,转发用,Michael Bazzell推荐 Telegram 群组、频道、机器人 - 汇总分享   https://congcong0806.github.io/2018/04/24/Telegram/#机器人-bot https://archive.ph/iJMBj 献给那些将来到Telegram的朋友 Design Patrick Wardle   https://www.instagram.com/patrickwardle/?hl=en 他的IG,摄影也精彩,审美...

黑客技术论坛推荐

原文来自知乎, 世界各大黑客技术论坛TOP排行榜 ,表示感谢 这份名单基本上囊括了目前世界上各大最佳黑客技术论坛。

产品随想 | 周刊 第127期:晨光只开一刻钟,但比千年松,并无甚不同

Cherry Studio   https://github.com/CherryHQ/cherry-studio Cherry Studio is a desktop client that supports for multiple LLM providers. Support deepseek-r1 Aalto Repository beta   https://repo.aalto.fi/ Images, sounds and videos from Aalto University 这个系列,价值极高 Nokia Design Archive   https://nokiadesignarchive.aalto.fi/ 芬兰这个国家很了不起 对话影石刘靖康:两代未出现划时代的产品,就会沦为平庸的品牌   https://www.geekpark.net/news/308996 还挺喜欢这个创始人的,有一种海盗的内涵 从哈佛、明星创业者到酷家乐副总裁,苏奇的传奇   https://app.modaiyun.com/mdy/article/3FO4K4W0M259 WHO关于猫狗咬伤、抓伤的处理建议 动物咬伤: https://www.who.int/zh/news-room/fact-sheets/detail/animal-bites 狂犬病: https://www.who.int/zh/news-room/fact-sheets/detail/rabies 关于狂犬病的10个事实: https://www.who.int/zh/news-room/facts-in-pictures/detail/rabies INDIGO 新年直播(2025)   https://www.youtube.com/live/ZIgPvSDGAfY 对2024年AI发展的回顾部分特别好 Artab   https://github.com/get-artab/artab Get Inspired by the World's Greatest Artworks Every Time You Open a New Tab. Extension Available for Chrome, Edge, and...

巴菲特致股东信-1980年

 笔记: 会计中对于下属股权公司的记账方式有3种: 持股50%以上,全部并入 持续20%--50%,则按持股比例并入 持股20%以下,则以实际收到的利润返还,计入报表 这种会计方式,会导致伯克希尔旗下,不少的企业,未能暴露实际的收益情况 对伯克希尔而言,对盈余的认定并非取决于持股比例是100%,50%,20%,5%或是1%,盈余的真正价值在于其将来再投资所能产生的效益 我们宁愿将所赚的盈余继续交由不受我们控制的人好好发挥,也不希望转由我们自己来浪费 高通货膨胀等于是对投入的资本额外课了一次税 翻译: https://xueqiu.com/6217262310/131837878 https://archive.ph/XMX5n  原文: Buffett’s Letters To Berkshire Shareholders 1980 巴菲特致股东的信 1980 年 Operating earnings improved to $41.9 million in 1980 from $36.0 million in 1979, but return on beginning equity capital (with securities valued at cost) fell to 17.8% from 18.6%. We believe the latter yardstick to be the most appropriate measure of single-year managerial economic performance. Informed use of that yardstick, however, requires an understanding of many factors, including accounting policies, historical ca...

写给大家看的中文排版指南

作者:Hindy 原文地址: http:// zhuanlan.zhihu.com/uici rcle/20506092 前言:很遗憾,我们的周围充斥着大量排版丑陋的文章。我国的字体排印与日本、美国等设计强国差距实在太大。我希望能够做些力所能及的小事,让更多人意识到“设计”的价值和其必要性,创造更美好的视觉环境。本文旨在帮助普及、提升大家对文字排版的认识,让大家在平时的学习工作中能有更专业的文字排版素养。 必看人群: 设计师、编辑、作家、撰稿人、教师、学生 目录: 1. 中文排版 1.1 引号 1.2 省略号与破折号 1.3 行首行尾禁则 2. 西文排版基础 2.1 西文撰写基础 2.2 西文标点相关 2.3 斜体的用法 2.4 大小写的区别 3. 中西文混排 3.1 基础原则 3.2 标点相关 1. 中文排版 1.1 引号 我国国家标准要求弯引号,个人建议使用直角引号。 示例:你竟然喜欢“苹果表”? 引号中再用引号使用双直角引号。 示例:我问他,“你竟然喜欢‘苹果表’?” 当引号表示讽刺、反语暗示时,使用弯引号(用法参考“西文排版”部分)。 示例:说真的,我也很 “喜欢”“苹果表”哦。 1.2 省略号(删节号)与破折号 省略号占两个汉字空间,包含六个点。 正确示例:中国设计还有太长路要走…… 错误示例:中国设计还有太长路要走… 破折号占两个汉字空间。 示例:中国设计还有太长路要走──加油罢。 1.3 行首行尾禁则 点号(顿号、逗号、句号等)、结束引号、结束括号等,不能出现在一行的开头。 错误示例: 排版时注意某些 符号不能在行首 ,别弄错了。 正确示例: 排版时注意某些 符号不能在行首, 别弄错了。 开始引号、开始括号、开始双书名号等,不能出现在一行的结尾。 错误示例: 她对我们说:“ 这书太赞了。” 正确示例: 她对我们说: “这书太赞了。” 2. 西文排版基础 2.1 西文撰写基础 句首字母大写。 单词间留空格。 示例:Have a question? 2.2 西文标点相关 点号后加一个空格(如逗号、句号等)。 示例:Hello everyone! Welcome to my blog....

360T7 刷机步骤及固件

https://cmi.hanwckf.top/p/360t7-firmware/   360T7的固件支持由immortalwrt-mt798x项目提供支持,请参考: https://cmi.hanwckf.top/p/immortalwrt-mt798x https://github.com/hanwckf/immortalwrt-mt798x 刷机步骤 参考 此处 的办法开启原厂固件的UART和telnet功能 在以下链接下载360T7测试固件(纯净版,无任何插件) https://wwd.lanzout.com/b0bt9idwd 密码:ezex (此固件已过时,请选择其它更新的固件) 接下来将刷入修改版uboot。修改版uboot的优点有: 固件分区可达108MB,原厂uboot只能使用36M 自带一个简单的webui恢复页面 到以下仓库的Release页面下载uboot,目前暂时仅支持360T7,后续将支持更多mt798x路由器。 推荐使用 mt7981_360t7-fip-fixed-parts.bin , fixed-parts 代表uboot分区表在编译期间固定,不会随着uboot环境变量变化。 https://github.com/hanwckf/bl-mt798x/releases/latest 将 mt7981_360t7-fip-fixed-parts.bin 通过HFS等方式上传到路由器,使用以下命令刷入uboot mtd write mt7981_360t7-fip-fixed-parts.bin fip 确认刷入完毕后,拔掉路由器电源。然后将电脑的IP地址设置为固定的 192.168.1.2 ,接着按住路由器的RESET按钮后通电开机,等待8s后用浏览器进入 192.168.1.1 在uboot恢复页面选择要刷入的固件。immortalwrt-mt798x目前编译两个版本的360T7固件。 建议修改版uboot直接使用 immortalwrt-mediatek-mt7981-mt7981-360-t7-108M-squashfs-factory.bin ,两种固件区别如下: mt7981-360-t7-108M 为108M固件分区,原厂uboot不可启动,需要修改版u...