2022.6 本文对于现在的Raspberry Pi OS已经不适用,移步官方文档
之前尝试了树莓派用作无限AP的教程,大多都只能获得ipv4地址,本文参考一篇教程,可以让树莓派给接入设备分配ipv6地址
原文链接:How to use your Raspberry Pi as a wireless access point
环境:原生Raspbian
安装和更新Raspbian
因为hostapd
和dnsmasq
对依赖的版本要求很高,需要先更新系统:
sudo apt-get update |
更新后可能依然提示依赖版本过低,所以最好先
sudo reboot
安装dnsmasq和hostapd
之后就可以开始安装dnsmasq
和hostapd
:
sudo apt-get install hostapd dnsmasq |
hostapd
是用来创建无线热点,dnsmasq
则是用来配置DNS和DHCP服务器,先将两个服务关闭:sudo systemctl stop hostapd |
给wlan0配置静态ip
之后修改菜配置:sudo vim /etc/dhcpcd.conf
,在文件末尾加入以下行:
interface wlan0 |
其中192.168.0.10是配置的静态ip,/24说明子网掩码是255.255.255.0
配置DHCP服务器
先备份原先配置:sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
,然后修改:sudo vim /etc/dnsmasq.conf
interface=wlan0 |
上述配置为wlan0的dhcp区间
配置hostapd
sudo vim /etc/hostapd/hostapd.conf
,修改内容呢类似下面
interface=wlan0 |
其中:NETWORK是SSID的名称,wpa_passphrase是密码,加密为WPA-PSK
然后sudo vim /etc/default/hostapd
,将DAEMON_CONF=""
取消注释,改为:DAEMON_CONF="/etc/hostapd/hostapd.conf"
配置数据转发和iptable
执行sudo vim /etc/sysctl.conf
,取消net.ipv4.ip_forward=1
的注释
配置转发规则:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE |
在启动项/etc/rc.local
中的exit 0
之前加入一行:iptables-restore < /etc/iptables.ipv4.nat
开启网络连接
现在树梅派已经可以作为热点但不能接入网络,我们需要在eth0和wlan0之间建一个网桥接口
首先安装:sudo apt-get install bridge-utils
,然后建立一个网桥,名为br0:sudo brctl addbr br0
,将eth0连到网桥上:sudo brctl addif br0 eth0
,配置接口信息:sudo nano /etc/network/interfaces
,在末尾加上下面几行:
auto br0 |
最后,设置开机自启动:
sudo systemctl enable dnsmasq |
最后重启:
sudo reboot
,连接热点,输入ifconfig
已经有ipv6地址了。