无忧启动论坛

 找回密码
 注册
搜索
系统gho:最纯净好用系统下载站投放广告、加入VIP会员,请联系 微信:wuyouceo
查看: 2044|回复: 10
打印 上一主题 下一主题

Linux 配置 WiFi 为 AP 模式

  [复制链接]
跳转到指定楼层
1#
发表于 2022-7-15 11:49:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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


  1. # 选择的网口名称,我这里是 ap0。具体可以 ifconfig 看下当前设备下有那些网口
  2. interface=ap0
  3. # 线驱动,一般有两种:wext/nl80211,wext 版本较旧,目前一般使用 nl80211
  4. driver=nl80211
  5. # iEEE802.11n
  6. ieee80211n=1
  7. # 802.11g, 一般三个模式: a,b,g。a->5GHZ,g->2.4GHZ
  8. hw_mode=g
  9. # wifi工作的信道,2.4GHZ(1~14)
  10. channel=6
  11. # AP的名称,类似于我们的路由器名称
  12. ssid=smartlife123456
  13. # 选择加密方式为 WPA2, 常用加解密方法有 WEP、WPA、WPA2、WPA3
  14. wpa=2
  15. # 密码
  16. wpa_passphrase=12345678
  17. # 加密方式
  18. wpa_key_mgmt=WPA-PSK
  19. # 加密算法
  20. rsn_pairwise=CCMP TKIP
  21. 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 的版本

成功启动后显示:

  1. # ./hostapd /mnt/hi3881/hostapd.conf -B
  2. Configuration file: /mnt/hi3881/hostapd.conf
  3. rfkill: Cannot open RFKILL control device
  4. ap0: Could not connect to kernel driver
  5. Using interface ap0 with hwaddr 3a:13:24:33:05:08 and ssid "rufan123456"

  6. random: Only 19/20 bytes of strong random data available from /dev/random
  7. random: Not enough entropy pool available for secure operations
  8. WPA: Not enough entropy in random pool for secure operations - update keys later when the first station connects
  9. ap0: interface state UNINITIALIZED->ENABLED
  10. 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







评分

参与人数 1无忧币 +5 收起 理由
zhczf + 5 赞一个!

查看全部评分

2#
发表于 2022-7-15 12:47:39 | 只看该作者
感谢分享,有空试试。
回复

使用道具 举报

3#
发表于 2022-7-15 14:04:04 | 只看该作者
我突然想到,如果使用WSL2能不能开启热点
回复

使用道具 举报

4#
发表于 2022-7-16 03:55:30 | 只看该作者
不明觉厉
回复

使用道具 举报

5#
 楼主| 发表于 2022-7-16 20:02:43 | 只看该作者
按照一楼的说明进行设置,最后失败了!

又参考了 https://elinux.org/RPI-Wireless-Hotspot ,补充了如下几条命令,就成功了!

  1. sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
复制代码


由于接下来要使用 iptables 命令,因此必须执行上述命令才行。

  1. sudo iptables -t nat -A POSTROUTING -o wlx7cdd90e027b1 -j MASQUERADE

  2. sudo iptables -A FORWARD -i wlx7cdd90e027b1 -o wlx70f11c1153ac -m state --state RELATED,ESTABLISHED -j ACCEPT

  3. sudo iptables -A FORWARD -i wlx70f11c1153ac -o wlx7cdd90e027b1 -j ACCEPT
复制代码


注意:

wlx7cdd90e027b1 是用来连接互联网的网卡(也是无线网卡)<---- 这个换成你的有线网卡 eth0 也行,它是可以访问互联网的。

wlx70f11c1153ac 是我的第二个无线网卡,专门用来创建热点。

这两个无线网卡都是 USB 无线网卡。当然,你的无线网卡如果是内置的也行,只要支持 AP 功能即可。

回复

使用道具 举报

6#
发表于 2022-7-19 18:59:52 | 只看该作者
iwlwifi是否可行,OpenWrt无效
回复

使用道具 举报

7#
发表于 2022-8-28 20:54:34 | 只看该作者
我机子支持的无线网卡都买不到了
回复

使用道具 举报

8#
发表于 2022-10-13 10:03:49 | 只看该作者
有空试试。
回复

使用道具 举报

9#
发表于 2022-10-14 19:48:06 | 只看该作者
赞一个!
回复

使用道具 举报

10#
发表于 2022-10-19 18:39:42 | 只看该作者
WiFi 现在到处都有
回复

使用道具 举报

11#
发表于 2023-6-5 12:45:06 | 只看该作者
感谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|捐助支持|无忧启动 ( 闽ICP备05002490号-1 )

闽公网安备 35020302032614号

GMT+8, 2025-12-10 18:33

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表