萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Ubuntu 啟用 Soft AP(軟體無線基地台)四步驟完整教學

Soft AP 利用支援 AP 模式的無線網卡,透過 hostapd + dnsmasq 實現熱點分享,成本低、無需額外硬體。本文詳細說明驗證網卡支援、安裝配置 hostapd/dnsmasq、啟動服務、驗證連線的完整流程。

iw list 驗證 AP 模式

前置檢查:無線網卡是否支援 AP 模式

$ iw list | grep -A 10 "Supported interface modes"
* AP
* AP/VLAN
* monitor
...

輸出含 AP 即支援。若無,需更換網卡或驅動(如 ath9k、rtl8812au)。

步驟 1:安裝並配置 hostapd

$ sudo apt-get install hostapd

編輯 /etc/hostapd/hostapd.conf:

interface=wlan0
driver=nl80211
ssid=MySoftAP
hw_mode=g
channel=6
ieee80211n=1
wmm_enabled=1
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=StrongPassword123
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP

指定配置檔:/etc/default/hostapd 設 DAEMON_CONF="/etc/hostapd/hostapd.conf"。

步驟 2:安裝並配置 dnsmasq(DHCP + DNS)

$ sudo apt-get install dnsmasq

備份原配置,編輯 /etc/dnsmasq.conf:

interface=wlan0
dhcp-range=192.168.10.10,192.168.10.100,12h
dhcp-option=3,192.168.10.1   # Gateway
dhcp-option=6,8.8.8.8,1.1.1.1 # DNS

步驟 3:配置網卡靜態 IP 與 NAT 轉發

# /etc/network/interfaces 或 Netplan
auto wlan0
iface wlan0 inet static
    address 192.168.10.1
    netmask 255.255.255.0

# 啟用 IP 轉發
$ echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
$ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
$ sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

步驟 4:啟動服務並驗證

$ sudo systemctl unmask hostapd
$ sudo systemctl enable --now hostapd dnsmasq
$ sudo systemctl status hostapd dnsmasq

hostapd 配置

dnsmasq 配置

dnsmasq 啟動

驗證與除錯

  • 手機/筆電搜尋 MySoftAP 連線,取得 IP、上網正常。
  • journalctl -u hostapd -f 觀察連線日誌。
  • iw dev wlan0 station dump 查看關聯站點。

常見問題

  • 網卡不支援 AP:換驅動或網卡。
  • 頻道衝突:iw dev wlan0 scan | grep channel 選擇乾淨頻道。
  • DNS 不通:檢查 resolv.conf、dnsmasq 日誌、防火牆 UDP 53。

四步驟打造專屬 Wi-Fi 熱點,適合實驗室、會議室、嵌入式裝置測試等場景。

copyright © 萬盛學電腦網 all rights reserved