树莓派共享 Wi-Fi

更新

1
2
3
4
5
6
7
8
9
10
11
gg@RaspberryPi:~ $ sudo apt update
Hit:1 https://mirrors.tuna.tsinghua.edu.cn/debian bullseye InRelease
Get:2 https://mirrors.tuna.tsinghua.edu.cn/debian bullseye-updates InRelease [44.1 kB]
Get:3 https://mirrors.tuna.tsinghua.edu.cn/debian bullseye-backports InRelease [49.0 kB]
Hit:4 https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security InRelease
Hit:5 https://mirrors.tuna.tsinghua.edu.cn/raspberrypi bullseye InRelease
Fetched 93.0 kB in 2s (51.8 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.

安装 hostapd

1
2
3
4
5
6
gg@RaspberryPi:~ $ sudo apt install hostapd
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
hostapd is already the newest version (2:2.9.0-21).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
gg@RaspberryPi:~ $ sudo vim /etc/hostapd/hostapd.conf

# 内容
interface=wlan0
driver=nl80211
ssid=RaspberryPi
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=Wrg2023.Ggdl
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
1
2
3
4
gg@RaspberryPi:~ $ sudo vim /etc/default/hostapd

# 内容
DAEMON_CONF="/etc/hostapd/hostapd.conf"

启动 hostapd 服务

1
2
3
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl start hostapd

安装 dhcpcd

1
2
3
4
5
6
gg@RaspberryPi:~ $ sudo vim /etc/dhcpcd.conf

# 内容
interface wlan0
static ip_address=10.10.3.1/24
nohook wpa_supplicant
1
gg@RaspberryPi:~ $ sudo systemctl restart dhcpcd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
gg@RaspberryPi:~ $ ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::5a5:3df6:70cf:a6b prefixlen 64 scopeid 0x20<link>
ether dc:a6:32:a2:79:f9 txqueuelen 1000 (Ethernet)
RX packets 3818 bytes 662477 (646.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2870 bytes 2069705 (1.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 20 bytes 2657 (2.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 20 bytes 2657 (2.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.10.3.1 netmask 255.255.255.0 broadcast 10.10.3.255
inet6 fe80::9c9a:d4c1:38ec:8fdb prefixlen 64 scopeid 0x20<link>
ether dc:a6:32:a2:79:fa txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 48 bytes 5972 (5.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

安装 dnsmasq

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
gg@RaspberryPi:~ $ sudo apt install dnsmasq
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
dnsmasq
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 32.0 kB of archives.
After this operation, 120 kB of additional disk space will be used.
Get:1 https://mirrors.tuna.tsinghua.edu.cn/debian bullseye/main arm64 dnsmasq all 2.85-1 [32.0 kB]
Fetched 32.0 kB in 0s (85.5 kB/s)
Selecting previously unselected package dnsmasq.
(Reading database ... 40686 files and directories currently installed.)
Preparing to unpack .../dnsmasq_2.85-1_all.deb ...
Unpacking dnsmasq (2.85-1) ...
Setting up dnsmasq (2.85-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/dnsmasq.service → /lib/systemd/system/dnsmasq.service.
gg@RaspberryPi:~ $ sudo systemctl stop dnsmasq
1
2
3
4
5
gg@RaspberryPi:~ $ sudo vim /etc/dnsmasq.conf

# 内容
interface=wlan0
dhcp-range=10.10.3.10,10.10.3.90,255.255.255.0,12h