文章目录
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。具体可以ifcofnig看下当前设备下偶那些网口
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=smartlife123456
wpa=2
wpa_passphrase=123456
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的版本
成功启动后如图:

可以看到此时我们设备的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 maximim number of leases (includes addressesd reserved
# by OFFER's, DECLINE's, and ARP conficts
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 conflct 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'.
#Examles
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程序。
- 为ap0分配IP地址,默认为网关地址
# ifconfig ap0 192.168.175.1
- 启动udchpcd程序
# udhcpd /etc/udhcpd/udhcpd.conf &
// 加‘&’是程序运行在后台
总结
现在我们已经通过hostapd和udhcpd这两个工具完成了一个WiFi热点的配置了,赶快连接试试吧。

本文详细介绍了如何在Linux系统上使用hostapd创建无线接入点,并通过udhcpd配置DHCP服务,实现WiFi热点的开启与IP地址的自动分配。讲解了hostapd.conf和udhcpd.conf的配置方法,以及启动hostapd和udhcpd的步骤,提供了无密码和加密模式的配置示例。
1160





