如果你需要在外场通过Wi-Fi连接树莓派,但又没有路由器的Wi-Fi信号,这种情况可以把树莓派配置成AP,发射一个Wi-Fi热点。
注意:本文不涉及通过Wi-Fi热点上网
1.安装依赖包
1 2 3 | sudo apt-get update sudo apt-get install hostapd sudo apt-get install dnsmasq |
2.配置DHCP服务器dnsmasq,自动分配地址
1 2 3 4 | sudo bash -c 'cat >> /etc/dnsmasq.conf' << EOF interface=wlan0 dhcp-range=192.168.80.11,192.168.80.30,255.255.255.0,24h EOF |
3.配置hostapd,注意下面按照需要修改ssid和password
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 28 | sudo bash -c 'cat > /etc/hostapd/hostapd.conf' << EOF interface=wlan0 hw_mode=a ieee80211d=1 ieee80211n=1 ieee80211ac=1 # 802.11ac support ht_capab=[HT40+][SHORT-GI-20][SHORT-GI-40][DSSS_CCK-40] require_ht=1 basic_rates=60 90 120 180 240 360 480 540 vht_capab=[MAX-MPDU-3895][SHORT-GI-80][SU-BEAMFORMEE] country_code=CN channel=149 wmm_enabled=1 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP ssid=raspberry wpa_passphrase=password EOF sudo bash -c 'cat > /etc/default/hostapd' << EOF DAEMON_CONF="/etc/hostapd/hostapd.conf" EOF |
4.开启AP
由于wpa_supplicant是由dhcpcd管理,并不是单独的service,所以配置nohook即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #配置wlan0静态地址 sudo bash -c 'cat >> /etc/dhcpcd.conf' << EOF interface wlan0 #config-ap static ip_address=192.168.80.1/24 #config-ap nohook wpa_supplicant #config-ap EOF sudo systemctl restart dhcpcd sudo systemctl enable hostapd sudo systemctl enable dnsmasq sudo systemctl restart hostapd sudo systemctl restart dnsmasq |
好了,现在应该可以搜索到一个叫raspberry的Wi-Fi信号了,树莓派的IP地址是:192.168.80.1
5.关闭AP(如果需要还原到连接路由器)
1 2 3 4 5 6 7 8 9 10 | #删除wlan0静态地址 sudo sed -i '/#config-ap/d' /etc/dhcpcd.conf sudo systemctl stop hostapd sudo systemctl stop dnsmasq sudo systemctl disable hostapd sudo systemctl disable dnsmasq sudo systemctl restart dhcpcd |