前言
树莓派作为Wifi热点
设置wlan0网卡的IP地址范围
- 在配置文件
/etc/dhcpcd.conf末尾追加配置
/etc/dhcpcd.conf1 2 3
| interface wlan0 static ip_address=192.168.1.1/24 nohook wpa_supplicant
|
部署hostapd
- Access point and authentication server for Wi-Fi and Ethernet
下载依赖
创建配置文件
- 创建配置文件
/etc/hostapd/hostapd.conf
<ssid>:热点名
<password>:密码
/etc/hostapd/hostapd.conf1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| interface=wlan0 driver=nl80211
hw_mode=g channel=6 ieee80211n=1 wmm_enabled=0 macaddr_acl=0 ignore_broadcast_ssid=0
auth_algs=1 wpa=2 wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP
ssid=<ssid> wpa_passphrase=<password>
|
为守护进程指定配置文件
- 为hostapd守护进程指定配置文件,修改
/etc/default/hostapd文件第13行,设置为刚刚创建的配置文件
/etc/default/hostapd1
| DAEMON_CONF="/etc/hostapd/hostapd.conf"
|
重启服务
1 2 3
| sudo systemctl unmask hostapd sudo systemctl enable hostapd sudo systemctl restart hostapd
|
部署DNS服务
A lightweight DHCP and caching DNS server
启用IPv4地址转发
- 为了能让连接Wifi的设备通过树莓派的其他网卡通讯从而正常联网
修改配置
/etc/sysctl.conf
立即应用配置
1
| echo 1 > /proc/sys/net/ipv4/ip_forward
|
设置转发规则
1 2 3
| 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
|
设置转发规则开机自动加载
保存转发规则到文件
1
| iptables-save > /etc/iptables.ipv4.nat
|
开机自动加载转发规则
- 在
/etc/rc.local配置文件的exit 0之前加载转发规则
/etc/rc.local1
| iptables-restore < /etc/iptables.ipv4.nat
|
完成
参考文献
知乎——很咸的咸鱼
LabNo3的博客
CSDN——afeiqiang