概述
有两种设置方法:
一 树莓派作路由,AP形成一个子网,下游设备通过AP转发,访问外网。
二 树莓派作网桥, 扩展AP所在的网络的下游设备,下游设备处在与上游设备相同的网段.
实现工具可以用:
a) dhcpcd + dnsmasq, 或
b) systemd-networkd
dhcpcd 的作用是对树莓派的无线网卡分配IP地址,dnsmasq是用于对连接到子网中的设备分配IP地址。
两者混用也可以,需要特别注意相互干扰
两个工具都需要hostapd作为AP软件。
一:路由
1. 用dhcpcd + dnsmasq实现
使用DHCPCD作为DHCP客户端, dnsmasq作为DHCP 服务器
1.1 安装软件包
sudo apt-get install dhcpcd dnsmasq
1.2 设置DHCP客户端 不为WIFI网卡获取IP
1.2.1将Wifi 网卡设置为固定IP
以下中的192.169.4.1和wlan0需按实际情况更改子网地址与树莓派的无线网卡名字。
修改dhcpcd.conf:
pi@3B:/etc $ sudo vi /etc/dhcpcd.conf
在文件末加入以下 内容:
interface wlan0
static ip_address=192.169.4.1/24
nohook wpa_supplicant
1.2.2 测试
pi@3B:/etc $ sudo ip link set wlan0 down
pi@3B:/etc $ sudo systemctl restart dhcpcd
pi@3B:/etc $ sudo ip link set wlan0 up
pi@3B:/etc $ sudo ip addr
wlan0可以获取固定IP即成功
1.3 设 置Hostapd将wifi设置为AP
1.3.1 安装hostapd软件
运行:
sudo apt-get install hostapd
1.3.2 编辑设置文件
vi /etc/hostapd/hostapd.conf
文件内容为:
interface=wlan0
driver=nl80211
ssid=your-ssid
hw_mode=g
channel=6
ieee80211n=1
wmm_enabled=1
ht_capab=[HT40][SHORT-GT-20][DSSS_CCK-40]
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_passphrase=12345678
wpa_pairwise=TKIP
rsn_pairwise=CCMP
上文中的your-said, 密码12345678需要按实际情况更换
1.3.2 测试
sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf
此时能找到设定的SSID网络“your-said"就为成功
Ctrl + C退出测试
1.3设置启动dhcpcd, hostapd
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl enable dhcpcd
1.4 处理random错误
因/dev/random的效率问题,改用urandom
rm /dev/random
ln /dev/urandom /dev/random
1.5 设置DHCP服务器
1.5.1 配置文件
sudo vi /etc/dnsmasq.conf
文件内容为:
interface=wlan0 # Listening interface
dhcp-range=192.169.4.2,192.169.4.20,255.255.255.0,24h
# Pool of IP addresses served via DHCP
domain=wlan # Local wireless DNS domain
address=/gw.wlan/192.169.4.1
# Alias for this router
1.5.2测试
wifi 连接后,可以自动获取IP,并可以Ping 192.169.4.1.
1.6 设置IP 转发
sudo vi /etc/sysctl.conf
去掉net.ipv4.ip_forward=1前面的“#”号。
1.7 设置NAT
1.7.1 iptable转发
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
注意eth0要按系统网卡名称更换。
1.7.2 保存
运行以下命令将上面的iptables转发保存
sudo netfilter-persistent save
目前的系统上面的命令无效,可转向开机时自动运行:
sudo vi /etc/rc.local
在最后添加以下内容:
sudo iptables -t nat -A POSTROUTING -o enxb827ebee27e3 -j MASQUERADE
enxb827ebee27e3需按实际网卡名称替换
1.7.3 测试
可以连接外网了
1.7.4 NAT 表删除子项
通常不需操作NAT表,特殊情况如需删除转发表子项,可参考以下命令:
pi@3T:~/log $ sudo iptables -t nat -L --line-numbers
Warning: iptables-legacy tables present, use iptables-legacy to see them
Chain PREROUTING (policy ACCEPT)
num target prot opt source destination
Chain INPUT (policy ACCEPT)
num target prot opt source destination
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
num target prot opt source destination
1 MASQUERADE all – anywhere anywhere
2 MASQUERADE all – anywhere anywhere
删除
pi@3T:~/log $ sudo iptables -t nat -D POSTROUTING 1
2 用systemd-networkd实现
对于启动使用systemd 的基于Debian的raspberry pi 来说可以用修改systemd.network的配置文件来实现:
3 固定网卡为固定名称
sudo ln -s /lib/udev/rules.d/80-net-setup-link.rules /etc/udev/rules.d/80-net-setup-link.rules
sudo nano /etc/udev/rules.d/80-net-setup-link.rules
将下面内容加入末尾,用实际网卡MAC替换
待补充…
二:网桥
将树莓派的有线网卡与无线网卡用网桥联接起来,设备通过无线与树莓派连接,树莓派通过有线网卡为设备分配IP。 此方法相当于将树莓派设为AP,通过无线连接的设备与树莓派有线网卡处于相同网段。
2.1 通过systemd-networkd实现
此方法不需要dhcpcd,也不需要dnsmasq,更不需要ifupdown这些工具,没避免干扰,需要确认这些工具已停用
2.1.1 interfaces 的设定
移除interfaces的原有配置文件:
sudo mv /etc/network/interfaces.d/* ~
iface enxb827ebee27e3 inet manual
iface wlx000f540c5716 inet manual
allow-hotplug wlx000f540c5716
auto br0
iface br0 inet dhcp
hostapd -B /etc/hostapd/hostapd-wlx000f540c5716.conf
bridge_ports enxb827ebee27e3 wlx000f540c5716
2.1. 2 dnsmasq的设置
sudo systemctl stop dnsmasq
sudo systemctl disable dnsmasq
或者:
修改dnsmasq.conf文件,确保不对网桥的设备分配IP。
2.1.3 wpa_supplicant的设置
sudo systemctl stop wpa_supplicant
sudo systemctl disable wpa_supplicant
可在interfaces文件中用no-scripts确保不会对网桥的网卡加载wpa_supplicant
auto wlan0
allow-hotplug wlan0
no-scripts wlan0
iface wlan0 inet static
address 192.168.5.1/24
hostapd /etc/hostapd/hostapd.conf
2.1.4 安装hostapd
sudo apt-get install hostapd
配置文件hostapd.conf为:
pi@3B:/etc/hostapd $ cat hostapd.conf
country_code=CN
interface=wlan0
driver=nl80211
ssid=3B
hw_mode=g
channel=8
wmm_enabled=1
#ht_capab=[HT40][SHORT-GT-20][DSSS_CCK-40]
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_passphrase=12345678
wpa_pairwise=TKIP
rsn_pairwise=CCMP
其中wlan0, 需要按实现无线网卡的名称更换,wifi密码为12345678
还需要将hostapd.service文件内容中的
After=network.target
替换为
Before=network.target
Before=systemd-networkd.service
pi@3B:/etc/hostapd $ cd /lib/systemd/system
pi@3B:/lib/systemd/system $ cat hostapd.service
[Unit]
Description=Access point and authentication server for Wi-Fi and Ethernet
Documentation=man:hostapd(8)
#After=network.target
Before=network.target
Before=systemd-networkd.service
[Service]
Type=forking
PIDFile=/run/hostapd.pid
Restart=on-failure
RestartSec=2
Environment=DAEMON_CONF=/etc/hostapd/hostapd.conf
EnvironmentFile=-/etc/default/hostapd
ExecStart=/usr/sbin/hostapd -B -P /run/hostapd.pid -B $DAEMON_OPTS ${DAEMON_CONF}
[Install]
WantedBy=multi-user.target
2.1.5 编写systemd-networkd的配置文件
建立网桥br0:
pi@3B:/lib/systemd/system $ cd /etc/systemd/network
pi@3B:/etc/systemd/network $ cat br0.netdev
[NetDev]
Name=br0
Kind=bridge
建立br0的网络配置文件
pi@3B:/etc/systemd/network $ cat 45-br0.network
[Match]
Name=br0
[Network]
DHCP=yes
建立网 卡的网络配置文件
pi@3B:/etc/systemd/network $ cat 55-eth0.network
[Match]
Name=enxb827ebee27e3
[Network]
Bridge=br0
pi@3B:/etc/systemd/network $ cat 56-wlan0.network
[Match]
Name=wlan0
[Network]
Bridge=br0
2.1.6 安装网桥管理工具
sudo apt-get install bridge-utils
2.1.7 关闭转发
sudo sysctl -w net.ipv4.ip_forward=0
sudo sysctl -w net.ipv6.ip_forward=0
检查是否networking和systemd-networkd已运行
pi@3B:/etc/systemd/network $ systemctl status networking systemd-networkd
● networking.service - Raise network interfaces
Loaded: loaded (/lib/systemd/system/networking.service; enabled; vendor preset: >
Active: active (exited) since Sat 2023-10-14 23:34:46 CST; 1h 11min ago
Docs: man:interfaces(5)
Main PID: 272 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 1596)
CPU: 0
CGroup: /system.slice/networking.service
Oct 14 23:34:45 3B systemd[1]: Starting Raise network interfaces…
Oct 14 23:34:46 3B systemd[1]: Finished Raise network interfaces.
● systemd-networkd.service - Network Service
Loaded: loaded (/lib/systemd/system/systemd-networkd.service; enabled; vendor pr>
Active: active (running) since Sat 2023-10-14 23:34:59 CST; 1h 10min ago
TriggeredBy: ● systemd-networkd.socket
Docs: man:systemd-networkd.service(8)
Main PID: 472 (systemd-network)
Status: “Processing requests…”
Tasks: 1 (limit: 1596)
CPU: 555ms
CGroup: /system.slice/systemd-networkd.service
└─472 /lib/systemd/systemd-networkd
2.1.8成功后,设备连接wifi可取得ipv4与ipv6的地址
pi@3T:~ $ brctl show
bridge name bridge id STP enabled interfaces
br0 8000.b827eb9b6795 no eth0
wlan0
2.2 要点
2.4.1 hostapd
可在iface 下,直接用hostapd /etc/hostapd/hostapd.conf, 保证hostapd被正确地加载,hostapd.conf内的网卡也需指向网桥的无线网卡
2.4.2 wpa_supplicant
在interfaces文件中用no-scripts wlan0,确保网桥内的无线网卡不会被wpa_supplicant管理
2.4.3 brctl
可查看网桥是否含有正确的设备
pi@3B:~/interfaces-backup $ brctl show
bridge name bridge id STP enabled interfaces
br0 8000.92a1f63dadf3 no enxb827ebee27e3
pi@3B:~/interfaces-backup $ sudo brctl addif br0 wlx000f540c5716
pi@3B:~/interfaces-backup $ brctl show
bridge name bridge id STP enabled interfaces
br0 8000.92a1f63dadf3 no enxb827ebee27e3
wlx000f540c5716
三:其它
3.1 interfaces 配置无线网卡
在interfaces文件中加一以下wpa_conf 可以将wpa_supplicant正确加载
allow-hotplug wlxb827eb9b6795
iface wlxb827eb9b6795 inet dhcp
wpa_conf /etc/wpa_supplicant/wpa_supplicant.conf