Linux之手动创建WIFI热点

背景

之前介绍了Linux下使用无线网卡作为STA手动连接WIFI:Linux手动连接配置wifi

今天介绍下Linux下怎么手动建立AP热点。主要分为2大步骤:

  • hostapd建立AP热点
  • DHCP服务分配IP

前提:首先要无线网卡是否支持AP模式
使用如下命令,查看网卡属性:

iw list

如果Supported interface modes选项包含 AP 的关键字,则表示支持 AP 模式。如:

Supported interface modes:
		 * IBSS
		 * managed
		 * AP
		 * P2P-client
		 * P2P-GO
		 * P2P-device

hostapd

hostapdwpa_supplicant是兄弟,都是由 w1.fi项目共同维护。

hostapd: IEEE 802.11 AP, IEEE 802.1X/WPA/WPA2/WPA3/EAP/RADIUS Authenticator

官网:https://w1.fi/hostapd/

hostapd is a user space daemon for access point and authentication servers. It implements IEEE 802.11 access point management, IEEE 802.1X/WPA/WPA2/WPA3/EAP Authenticators, RADIUS client, EAP server, and RADIUS authentication server. The current version supports Linux (Host AP, madwifi, mac80211-based drivers) and FreeBSD (net80211).

hostapd is designed to be a “daemon” program that runs in the background and acts as the backend component controlling authentication. hostapd supports separate frontend programs and an example text-based frontend, hostapd_cli, is included with hostapd.

Supported WPA/IEEE 802.11i/EAP/IEEE 802.1X features:

  • WPA-PSK (“WPA-Personal”)
  • WPA with EAP (with integrated EAP server or an external RADIUS backend authentication server) (“WPA-Enterprise”)
  • key management for CCMP, TKIP, WEP104, WEP40
  • WPA and full IEEE 802.11i/RSN/WPA2/WPA3
  • RSN: PMKSA caching, pre-authentication
  • IEEE 802.11r
  • IEEE 802.11w
  • RADIUS accounting
  • RADIUS authentication server with EAP
  • Wi-Fi Protected Setup (WPS)

Supported wireless cards/drivers:

  • Linux mac80211 drivers
  • Linux drivers that support nl80211/cfg80211 in AP mode
  • BSD net80211 layer (e.g., Atheros driver) (FreeBSD 6-CURRENT)

配置文件

官方配置文件 hostapd.conf: https://w1.fi/cgit/hostap/plain/hostapd/hostapd.conf

下面是一个简洁版本:

#使用的网卡
interface=wlan0
#驱动,现一般使用nl80211,wext已经淘汰
driver=nl80211
#AP名
ssid=test
# 802.11g
hw_mode=g
#信道
channel=6
#加密方式:WAP2
wpa=2
#密码
wpa_passphrase=12345678
#加密方式
wpa_key_mgmt=WPA-PSK
#加密算法
wpa_pairwise=TKIP
rsn_pairwise=CCMP

简单使用

# -B将程序放到后台运行
hostapd hostapd.conf -B

DHCP服务器

以上操作只是建立了一个无线AP,我们可以连接到此AP但还无法获取到IP地址,所以还需要搭建DHCP服务器来动态分配IP地址。

DHCP服务器一般常用的有2个:dnsmasqudhcpd

dnsmasq

dnsmasq 是一个轻量级且功能多样的网络服务软件,主要用于提供 DNS 缓存和 DHCP 服务。

Dnsmasq provides network infrastructure for small networks: DNS, DHCP, router advertisement and network boot. It is designed to be lightweight and have a small footprint, suitable for resource constrained routers and firewalls.

官网:https://dnsmasq.org/doc.html

配置文件

官方配置文件 dnsmasq.conf: https://thekelleys.org.uk/gitweb/?p=dnsmasq.git;a=blob_plain;f=dnsmasq.conf.example

下面是一个用于dhcp的极简版本:

interface=wlan0
dhcp-range=192.168.0.2,192.168.0.20,255.255.255.0,24h

简单使用

dnsmasq -C dnsmasq.conf

udhcpd

udhcpd是busybox自带的一个轻量dhcp服务器,常用于嵌入式。

官网:https://udhcp.busybox.net/

配置文件

官方配置文件 udhcpd.conf: https://udhcp.busybox.net/udhcpd.conf

下面是一个简洁版本:

start 192.168.175.2
end 192.168.175.254
interface ap0
max_leases 234
opt router 192.168.175.1

简单使用

udhcpd /etc/udhcpd/udhcpd.conf &

参考