|
Linux 系统将 WiFi 配置为 AP 模式
--- hostapd 和 udhcpd 的使用说明
hostapd
一、功能说明
hostapd 是 Linux 系统上的一个带加密功能的无线接入点 (access point : AP) 程序。
hostapd 能够使得无线网卡切换为 master 模式,模拟 AP(路由器)功能,作为 AP 的认证服务器,负责控制管理 stations 的接入和认证。
hostapd 是用于接入点和身份验证服务器的用户空间守护进程。它实现了IEEE 802.11 接入点管理, 当前版本支持 Linux(Host AP、madwifi、mac80211-based 驱动)和 FreeBSD(net80211)。
hostapd 被设计为一个 “守护进程” 程序,在后台运行并充当控制身份验证的后端组件。hostapd 支持单独的前端程序,hostapd 中包含一个基于文本的使用工具 hostapd_cli。(wpa_supplicant 对应的是对 station 模式的管理,前端程序为 wpa_cli)
由于 hostapd 的良好特性,现在已经被广泛使用,可以说是通用的 AP 管理工具了。这里我们只探讨该工具如何使用,不讨论其实现原理。
官网源码地址: hostapd
二、配置文件 hostapd.conf
启动 hostapd 前需要我们写好 hostapd 的配置文件,因为 hostapd 起来前会解析这个文件来进行相关的设定。
官方文档在源码目录中,由于内容太多,不做粘贴,这里给出我们会常用到的配置说明。
hostapd.conf 官方地址:hostapd.conf
- # 选择的网口名称,我这里是 ap0。具体可以 ifconfig 看下当前设备下有那些网口
- interface=ap0
- # 线驱动,一般有两种:wext/nl80211,wext 版本较旧,目前一般使用 nl80211
- driver=nl80211
- # iEEE802.11n
- ieee80211n=1
- # 802.11g, 一般三个模式: a,b,g。a->5GHZ,g->2.4GHZ
- hw_mode=g
- # wifi工作的信道,2.4GHZ(1~14)
- channel=6
- # AP的名称,类似于我们的路由器名称
- ssid=smartlife123456
- # 选择加密方式为 WPA2, 常用加解密方法有 WEP、WPA、WPA2、WPA3
- wpa=2
- # 密码
- wpa_passphrase=12345678
- # 加密方式
- wpa_key_mgmt=WPA-PSK
- # 加密算法
- rsn_pairwise=CCMP TKIP
- wpa_pairwise=TKIP CCMP
复制代码
上面给出了常用的一些配置项的说明,下面直接给出两个可用的配置。(copy 进 Linux 的时候注意下格式,否则 hostapd 会解析出错)
1. 配置 WiFi 热点为无密码模式
配置 AP 为无密码模式,即 open system,此时 AP 不会进行任何加密验证,无需密码即可连接。
interface=ap0
driver=nl80211
ieee80211n=1
hw_mode=g
channel=6
ssid=smartlife123456
2. 配置 WiFi 热点为加密模式
interface=ap0
driver=nl80211
ieee80211n=1
hw_mode=g
channel=6
ssid=rufan123456
wpa=2
wpa_passphrase=12345678
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP TKIP
wpa_pairwise=TKIP CCMP
三、hostapd 的启动
# hostapd /etc/hostapd/hostapd.conf -B
这里直接指定使用我们上面写好的配置文件,具体的路径依据实际使用来定,-B 将程序放到后台运行
hostapd 启动中常用到的参数说明
-h 显示帮助信息
-d 显示更多的 debug 信息 (-dd 获取更多)
-B 将 hostapd 程序运行在后台
-g 全局控制接口路径,这个供 hostapd_cli 使用,一般为 /var/run/hostapd
-G 控制接口组
-P PID 文件
-K 调试信息中包含关键数据
-t 调试信息中包含时间戳
-v 显示 hostapd 的版本
成功启动后显示:
- # ./hostapd /mnt/hi3881/hostapd.conf -B
- Configuration file: /mnt/hi3881/hostapd.conf
- rfkill: Cannot open RFKILL control device
- ap0: Could not connect to kernel driver
- Using interface ap0 with hwaddr 3a:13:24:33:05:08 and ssid "rufan123456"
- random: Only 19/20 bytes of strong random data available from /dev/random
- random: Not enough entropy pool available for secure operations
- WPA: Not enough entropy in random pool for secure operations - update keys later when the first station connects
- ap0: interface state UNINITIALIZED->ENABLED
- ap0: AP-ENABLED
复制代码
可以看到此时我们设备的 AP 模式已经开启成功了,但是我们的工作还没有结束。因为此时我们连接到此 AP 的设备还无法获取到 IP 地址。要进行 IP 地址的分配我们还得继续下面的工作。
udhcpd
一、功能说明
我们常用到 udhcpc, 对 udhcpd 并不熟悉,其实 udhcpd 是工作在 server 端的 DHCP 服务,udhcpc 则是工作在 client 端的 DHCP 服务。
DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)。是一个局域网的网络协议,使用 UDP 协议工作。
这里对 DHCP 原理不做过多介绍,对 DHCP 协议感兴趣的话可以看下我的另一篇博客:DHCP原理及IP地址获取过程
udhcpc 是用来获取 IP 地址的,而 udhcpd 则是用来为设备分配 IP 地址的。如果使用的是静态 IP 则不需 DHCP 服务的。
官方源码地址:udhcp
二、配置文件 udhcpd.conf 的使用
# Sample udhcpd configuration file (/etc/udhcpd.conf)
# The start and end of the IP lease block
start 192.168.0.150 #default: 192.168.0.20
end 192.168.0.240 #default: 192.168.0.254
# The interface that udhcpd will use
interface wlan0 #default: eth0
# The maximum number of leases, includes addresses reserved
# by OFFER's, DECLINE's, and ARP conflicts
max_leases 21 #default: 254
# If remaining is true (default), udhcpd will store the time
# remaining for each lease in the udhcpd leases file. This is
# for embedded systems that cannot keep time between reboots.
# If you set remaining to no, the absolute time that the lease
# expires at will be stored in the dhcpd.leases file.
#remaining yes #default: yes
# The time period at which udhcpd will write out a dhcpd.leases
# file. If this is 0, udhcpd will never automatically write a
# lease file. (specified in seconds)
#auto_time 7200 #default: 7200 (2 hours)
# The amount of time that an IP will be reserved (leased) for if a
# DHCP decline message is received (seconds).
#decline_time 3600 #default: 3600 (1 hour)
# The amount of time that an IP will be reserved (leased) for if an
# ARP conflict occurs (seconds).
#conflict_time 3600 #default: 3600 (1 hour)
# How long an offered address is reserved (leased) in seconds
#offer_time 60 #default: 60 (1 minute)
# If a lease to be given is below this value, the full lease time is
# instead used (seconds).
#min_lease 60 #defult: 60
# The location of the leases file
#lease_file /var/lib/misc/udhcpd.leases #defualt: /var/lib/misc/udhcpd.leases
# The location of the pid file
#pidfile /var/run/udhcpd.pid #default: /var/run/udhcpd.pid
# Everytime udhcpd writes a leases file, the below script will be called.
# Useful for writing the lease file to flash every few hours.
#notify_file #default: (no script)
#notify_file dumpleases # <--- usefull for debugging
# The following are bootp specific options, setable by udhcpd.
#siaddr 192.168.0.22 #default: 0.0.0.0
#sname zorak #default: (none)
#boot_file /var/nfs_root #default: (none)
# The remainer of options are DHCP options and can be specifed with the
# keyword 'opt' or 'option'. If an option can take multiple items, such
# as the dns option, they can be listed on the same line, or multiple
# lines. The only option with a default is 'lease'.
#Examples
opt dns 116.116.116.116
option subnet 255.255.255.0
opt router 192.168.0.1
#opt wins 192.168.10.10
option dns 129.219.13.81 # appened to above DNS servers for a total of 3
option domain local
option lease 864000 # 10 days of seconds
# Currently supported options, for more info, see options.c
#opt subnet
#opt timezone
#opt router
#opt timesvr
#opt namesvr
#opt dns
#opt logsvr
#opt cookiesvr
#opt lprsvr
#opt bootsize
#opt domain
#opt swapsvr
#opt rootpath
#opt ipttl
#opt mtu
#opt broadcast
#opt wins
#opt lease
#opt ntpsrv
#opt tftp
#opt bootfile
# Static leases map
#static_lease 00:60:08:11:CE:4E 192.168.0.54
#static_lease 00:60:08:11:CE:3E 192.168.0.44
官方配置文档: udhcpd.conf
三、实例说明
下面是一份可以直接使用的 udhcpd.conf
start 192.168.175.2
end 192.168.175.254
interface ap0
max_leases 234
opt router 192.168.175.1
四、使用示例
至此,我们所有的准备工作都已经完成,下面来启动 udhcpd 程序。
1. 为 ap0 分配 IP 地址,默认为网关地址
# ifconfig ap0 192.168.175.1
2. 启动 udchpcd 程序
# udhcpd /etc/udhcpd/udhcpd.conf &
加 ‘&’ 是程序运行在后台
总结
现在我们已经通过 hostapd 和 udhcpd 这两个工具完成了一个 WiFi 热点的配置了,赶快连接试试吧。
版权声明:本文为 wit_732 原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本文链接:https://blog.csdn.net/wit_732/article/details/121038477
|