我是基于CHatGPT实现的AI助手,在此网站上负责整理和概括文章
文章介绍了如何将树莓派设置成Access Point。首先需要准备好储存卡、读卡器、树莓派和网卡等材料,并安装相关驱动。然后安装并配置hostapd、dnsmasq和dhcpcd等软件。接着配置dhcpcd.conf和dnsmasq,并检查安装是否成功。随后配置hostapd,根据不同协议和频率选择对应的配置文件。最后介绍了如何魔改hostapd以实现功能。若树莓派出现网络连接问题,可以重新配置接口或调整IP地址。需要注意的是,若wlan0未在ifconfig中显示,可以使用sudo ifconfig wlan0 up命令启用。
以下文章是使用 hostapd 将树莓派变成 AP 的。
# 准备事项:
准备一张好的储存卡,内容最好大于等于 32GB,利用树莓派自身拷贝系统前一定要清空储存卡。
准备读卡器。并且在官方网站下载树莓派烧录工具,并且在烧录系统时为了稳定性选择 Debian Bullseye 版本。
树莓派(毋庸置疑)
如果不使用板载网卡接口 eth0,就需要自行准备网卡,本文以 TP-Link AC1900 Archer T9UH
为例进行配置。
由于适配性能的问题存在,无线网卡插在 USB2.0 与 USB3.0 的表现不同。
# 安装驱动
TP-Link AC1900 Archer T9UH 版本:
TP-Link AC1300 Archer T4U 版本:
gnab/rtl8812au: Realtek 802.11n WLAN Adapter Linux driver (github.com)
# 更新库
sudo apt-get upgrade |
# 安装 hostapd,dnsmasq, dhcpcd
sudo apt-get install hostapd | |
sudo apt-get install dnsmasq | |
sudo apt-get install dhcpcd |
如果是通过 apt
指令安装 hostapd
,那么 hostapd 的指令会在 /etc/network/hostapd
中。
如果自行下载安装包, tar
指令安装的情况下, hostpad
的指令在 /usr/local/bin
中。
# 配置 dhcpcd.conf
interface wlan1 | |
static ip_address=192.168.11.1/24 //任意IP地址都可以,但是不要重复! | |
# denyinterfaces eth0 | |
# denyinterfaces wlan0 (可选) |
# 配置 dnsmasq
将 dnsmasq
配置文件重命名并编写一个新配置文件
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig | |
sudo nano /etc/dnsmasq.conf | |
# mv: 是 "move"(移动)的缩写,用于移动或重命名文件。 | |
#/etc/dnsmasq.conf: 是要移动或重命名的目标文件的路径。 | |
#/etc/dnsmasq.conf.orig: 是目标文件的新路径和名称。 |
新的 dnsmasq.config
文件中添加如下
interface=wlan1 #这里表示 dnsmasq 要监听的接口,我们使用的是 wlan1 接口 | |
dhcp-range=192.168.11.10,192.168.11.100,255.255.255.0,24h | |
# 这里定义了分配的 IP 地址范围 | |
# 192:常见的家庭网络标识号 | |
# 168.11:子网号 | |
# 10:具体的主机 | |
# 要保持分配的 IP 地址的子网号与 wlan1 的相同,同时不能包含 wlan1 的主机号,避免冲突 |
好的,现在让我们来检查 hostapd
, dnsmasq
, dhcpcd
有没有安装成功。
hostapd --version | |
dnsmasq --version | |
dhcpcd --version |
# 配置 hostapd
首先创建一个新的 .config
文件。
802.11n 协议下的 2.4G20Mhz 配置(无信道绑定)
ssid=RaspberryN_20Mhz | |
wpa_passphrase=raspberry | |
country_code=JP | |
interface=wlan1 | |
driver=nl80211 | |
wpa=2 | |
wpa_key_mgmt=WPA-PSK | |
rsn_pairwise=CCMP | |
#macaddr_acl=0 | |
logger_syslog=0 | |
logger_syslog_level=4 | |
logger_stdout=-1 | |
logger_stdout_level=0 | |
hw_mode=g | |
wmm_enabled=1 | |
# N | |
ieee80211n=1 | |
#require_ht=1 | |
#ht_capab=[MAX-AMSDU-3839][HT40+][SHORT-GI-20][SHORT-GI-40][DSSS_CCK-40] | |
#ht_capab=[SHORT-GI-40][HT40+][HT40-][DSSS_CCK-40] | |
# AC | |
#ieee80211ac=1 | |
#require_vht=1 | |
#ieee80211d=0 | |
#ieee80211h=0 | |
#vht_capab=[MAX-AMSDU-3839][SHORT-GI-80] | |
#vht_oper_chwidth=1 | |
channel=1 | |
#vht_oper_centr_freq_seg0_idx=42 |
802.11n 协议下的 2.4G40Mhz 配置(信道绑定)
ssid=RaspberryN_40Mhz | |
wpa_passphrase=raspberry | |
country_code=JP | |
interface=wlan1 | |
driver=nl80211 | |
wpa=2 | |
wpa_key_mgmt=WPA-PSK | |
rsn_pairwise=CCMP | |
#macaddr_acl=0 | |
logger_syslog=0 | |
logger_syslog_level=4 | |
logger_stdout=-1 | |
logger_stdout_level=0 | |
hw_mode=g | |
wmm_enabled=1 | |
# N | |
ieee80211n=1 | |
require_ht=1 | |
ht_capab=[MAX-AMSDU-3839][HT40+][SHORT-GI-20][SHORT-GI-40][DSSS_CCK-40] | |
#ht_capab=[SHORT-GI-40][HT40+][HT40-][DSSS_CCK-40] | |
# AC | |
#ieee80211ac=1 | |
#require_vht=1 | |
#ieee80211d=0 | |
#ieee80211h=0 | |
#vht_capab=[MAX-AMSDU-3839][SHORT-GI-80] | |
#vht_oper_chwidth=1 | |
channel=1 | |
#vht_oper_centr_freq_seg0_idx=42 |
802.11ac 协议下的 5G20Mhz 配置(无信道绑定)
ssid=RaspberryAC_20Mhz | |
wpa_passphrase=raspberry | |
country_code=JP | |
interface=wlan1 | |
driver=nl80211 | |
wpa=2 | |
wpa_key_mgmt=WPA-PSK | |
rsn_pairwise=CCMP | |
macaddr_acl=0 | |
logger_syslog=0 | |
logger_syslog_level=4 | |
logger_stdout=-1 | |
logger_stdout_level=0 | |
hw_mode=a | |
wmm_enabled=1 | |
# N | |
ieee80211n=1 | |
#require_ht=1 | |
#ht_capab=[MAX-AMSDU-3839][HT40+][SHORT-GI-20][SHORT-GI-40][DSSS_CCK-40] | |
# AC | |
ieee80211ac=1 | |
#require_vht=1 | |
ieee80211d=0 | |
ieee80211h=0 | |
#vht_capab=[MAX-AMSDU-3839][SHORT-GI-80] | |
#vht_oper_chwidth=1 | |
channel=36 | |
#vht_oper_centr_freq_seg0_idx=42 |
802.11ac 协议下的 5G40Mhz 配置(信道绑定)
ssid=RaspberryAC_40Mhz | |
wpa_passphrase=raspberry | |
country_code=JP | |
interface=wlan1 | |
driver=nl80211 | |
wpa=2 | |
wpa_key_mgmt=WPA-PSK | |
rsn_pairwise=CCMP | |
macaddr_acl=0 | |
logger_syslog=0 | |
logger_syslog_level=4 | |
logger_stdout=-1 | |
logger_stdout_level=0 | |
hw_mode=a | |
wmm_enabled=1 | |
# N | |
ieee80211n=1 | |
require_ht=1 | |
ht_capab=[MAX-AMSDU-3839][HT40+][SHORT-GI-20][SHORT-GI-40][DSSS_CCK-40] | |
# AC | |
ieee80211ac=1 | |
#require_vht=1 | |
ieee80211d=0 | |
ieee80211h=0 | |
#vht_capab=[MAX-AMSDU-3839][SHORT-GI-80] | |
#vht_oper_chwidth=1 | |
channel=36 | |
#vht_oper_centr_freq_seg0_idx=42 |
但是,hostapd 配置完成后不要忙着高兴,是不是一开启 AP 发现,2.4G 怎么只有 20Mhz,说好的 Channel Bonding 呢,GPT 快救一下啊!
别着急,这是因为 80211n 协议有一个非常重要的东西叫 Good Nieghbor wifi,它会在你开启 80211n 协议下的 AP 时,自动扫描周围的路由器,一旦发现有 overlapping 的情况,就会自动退回 20Mhz。
毕竟 2.4G 频段有限,一个人占了 40Mhz 频段,其他人就会受到非常大的影响。
# 魔改 hostapd
首先需要卸载已经安装的 hostapd 软件。
sudo apt autoremove hostapd |
在这篇文章中提到了如何修改 hostapd-2.8 版本,而且我也已经亲自实验过了,结合 hostapd/patch/300-noscan.patch at master・igorpecovnik/hostapd (github.com) 的 patch,在拥挤的环境中依然可以在 2.4G 频段开启 40Mhz。
适用于 hostapd2.8 版本的补丁:
diff -Naur hostapd-2.8.orig/src/ap/hw_features.c hostapd-2.8/src/ap/hw_features.c | |
--- hostapd-2.8.orig/src/ap/hw_features.c 2019-04-21 09:10:22.000000000 +0200 | |
+++ hostapd-2.8/src/ap/hw_features.c 2019-06-25 08:32:00.555631536 +0200 | |
@@ -316,7 +316,7 @@ | |
iface->conf->channel, | |
iface->conf->channel + | |
iface->conf->secondary_channel * 4); | |
- iface->conf->secondary_channel = 0; | |
+// iface->conf->secondary_channel = 0; | |
if (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX) { | |
/* | |
* TODO: Could consider scheduling another scan to check | |
diff -Naur hostapd-2.8.orig/src/ap/ieee802_11_ht.c hostapd-2.8/src/ap/ieee802_11_ht.c | |
--- hostapd-2.8.orig/src/ap/ieee802_11_ht.c 2019-04-21 09:10:22.000000000 +0200 | |
+++ hostapd-2.8/src/ap/ieee802_11_ht.c 2019-06-25 08:38:51.760302846 +0200 | |
@@ -349,7 +349,7 @@ | |
wpa_printf(MSG_DEBUG, "is_ht40_allowed=%d num_sta_ht40_intolerant=%d", | |
is_ht40_allowed, iface->num_sta_ht40_intolerant); | |
- if (!is_ht40_allowed && | |
+/* if (!is_ht40_allowed && | |
(iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX)) { | |
if (iface->conf->secondary_channel) { | |
hostapd_logger(hapd, mgmt->sa, | |
@@ -372,7 +372,7 @@ | |
"Reschedule HT 20/40 timeout to occur in %u seconds", | |
delay_time); | |
} | |
- } | |
+ } */ | |
} |
Noscanpatch:
diff --git a/hostapd/config_file.c b/hostapd/config_file.c | |
index 5079f69e3..5ddccc216 100644 | |
--- a/hostapd/config_file.c | |
+++ b/hostapd/config_file.c | |
@@ -2863,6 +2863,10 @@ static int hostapd_config_fill(struct hostapd_config *conf, | |
} | |
#endif /* CONFIG_IEEE80211W */ | |
#ifdef CONFIG_IEEE80211N | |
+ } else if (os_strcmp(buf, "noscan") == 0) { | |
+ conf->noscan = atoi(pos); | |
+ } else if (os_strcmp(buf, "ht_coex") == 0) { | |
+ conf->no_ht_coex = !atoi(pos); | |
} else if (os_strcmp(buf, "ieee80211n") == 0) { | |
conf->ieee80211n = atoi(pos); | |
} else if (os_strcmp(buf, "ht_capab") == 0) { | |
diff --git a/src/ap/ap_config.h b/src/ap/ap_config.h | |
index 8c8f7e286..3eae4ef56 100644 | |
--- a/src/ap/ap_config.h | |
+++ b/src/ap/ap_config.h | |
@@ -664,6 +664,8 @@ struct hostapd_config { | |
int ht_op_mode_fixed; | |
u16 ht_capab; | |
+ int noscan; | |
+ int no_ht_coex; | |
int ieee80211n; | |
int secondary_channel; | |
int no_pri_sec_switch; | |
diff --git a/src/ap/hw_features.c b/src/ap/hw_features.c | |
index 16887acdf..75012df30 100644 | |
--- a/src/ap/hw_features.c | |
+++ b/src/ap/hw_features.c | |
@@ -474,6 +474,6 @@ static int ieee80211n_check_40mhz(struct hostapd_iface *iface) | |
int ret; | |
- if (!iface->conf->secondary_channel) | |
+ if (!iface->conf->secondary_channel || iface->conf->noscan) | |
return 0; /* HT40 not used */ | |
hostapd_set_state(iface, HAPD_IFACE_HT_SCAN); | |
diff --git a/src/ap/ieee802_11_ht.c b/src/ap/ieee802_11_ht.c | |
index 5eb1060a2..552bcc9bc 100644 | |
--- a/src/ap/ieee802_11_ht.c | |
+++ b/src/ap/ieee802_11_ht.c | |
@@ -244,6 +244,9 @@ void hostapd_2040_coex_action(struct hostapd_data *hapd, | |
if (!(iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) | |
return; | |
+ if (iface->conf->noscan || iface->conf->no_ht_coex) | |
+ return; | |
+ | |
if (len < IEEE80211_HDRLEN + 2 + sizeof(*bc_ie)) | |
return; | |
@@ -368,6 +371,9 @@ void ht40_intolerant_add(struct hostapd_iface *iface, struct sta_info *sta) | |
if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G) | |
return; | |
+ if (iface->conf->noscan || iface->conf->no_ht_coex) | |
+ return; | |
+ | |
wpa_printf(MSG_INFO, "HT: Forty MHz Intolerant is set by STA " MACSTR | |
" in Association Request", MAC2STR(sta->addr)); |
# 写在后面
哦对了,不要忘记在你的 config 文件中,写入 noscan=1
用来禁用 Good Nieghbor wifi 技术。
Do a me a favor for your neighbor don't open 40Mhz in 2.4G.
由于树莓派的 USB 口适配性不同,如果发现 bonding 不起作用的话,使用 - d 指令查看是否已经启用 bonding,但是由于接口问题回退至 20Mhz,对于树莓派 4B 来说,下面的 USB2.0 更容易开启 bonding。
最好不要用延长线。
# iw 指令:
iw 是一种新的基于 nl80211 的用于无线设备的 CLI 配置实用程序。它支持最近已添加到内核所有新的驱动程序。
采用无线扩展接口的旧工具 iwconfig 已被废弃,强烈建议切换到 iw 和 nl80211。
# iw help # 帮助 | |
# iw list # 获得所有设备的功能,如带宽信息(2.4GHz,和 5GHz),和 802.11n 的信息 | |
# iw dev wlan0 scan # 扫描 | |
# iw event # 监听事件 | |
# iw dev wlan0 link # 获得链路状态 | |
# iw wlan0 connect foo # 连接到已禁用加密的 AP,这里它的 SSID 是 foo | |
# iw wlan0 connect foo 2432 # 假设你有两个 AP SSID 都是 foo ,你知道你要连接的是在 2432 频道 | |
# iw wlan0 connect foo keys 0:abcde d:1:0011223344 # 连接到使用 WEP 的 AP | |
# iw dev wlan1 station dump # 获取 station 的统计信息 | |
# iw dev wlan1 station get # 获得 station 对应的 peer 统计信息 | |
# iw wlan0 set bitrates legacy-2.4 12 18 24 # 修改传输比特率 | |
# iw dev wlan0 set bitrates mcs-5 4 # 修改 tx HT MCS 的比特率 | |
# iw dev wlan0 set bitrates mcs-2.4 10 | |
# iw dev wlan0 set bitrates mcs-5 # 清除所有 tx 比特率和设置的东西来恢复正常 | |
# iw dev set txpower [] #设置传输功率 | |
# iw phy set txpower [] #设置传输功率 | |
# iw dev wlan0 set power_save on #设置省电模式 | |
# iw dev wlan0 get power_save #查询当前的节电设定 | |
# iw phy phy0 interface add moni0 type monitor #添加一个 monitor 接口 |
# 如何让树莓派重新联网
如果发现树莓派显示 wifi 模块关闭,有可能是将接口关闭了。
或者能够找到附近的网络,但是无法连接,检查 dhcpcd 是不是分配了一个固定 IP。
sudo nano /etc/network/interfaces | |
auto wlan0 | |
iface wlan inet dhcp | |
wpa-ssid Wifiname | |
wpa-psk Wifipassword | |
sudo nano /etc/dhcpcd.conf | |
#开启接口 | |
sudo ifup wlan0 | |
#关闭接口 | |
sudo ifdown wlan0 |
# wlan0 没有在 ifconfig 里显示出来:
sudo ifconfig wlan0 up |