Linux下无线网卡 的AP模式

在Windows下,我们可以用猎豹、360和腾讯管家里的一些软件助手,把我们的无线网卡开一个热点,同样,在Linux中。我们也可以把我们无线网卡的热点打开,让其他设备能够连接。这里基于天嵌的IMX6UL平台,使用8723bu WIFI模块发送AP热点

前提条件:
1.文件系统有 hostapd 工具,如没有需自行移植
2.文件系统有 udhcpd 工具,如没有需自行移植
1)打开 hostapd 配置文件,修改配置内容:

vi /etc/hostapd.conf:
ctrl_interface=/var/run/hostapd
#change wlan0 to your wireless device
interface="wlan0"//这个是网卡设备节点,修改成 wlan0
driver=nl80211
ssid="test"//热点名称,可自定义
channel=1
# ....../............ macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=3
wpa_passphrase="12345678" //热点的密码,自定义
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

2)打开分配网络 ip 的配置文件(这里粘贴部分):
vi /etc/udhcpd.conf(只需修改开头部分即可)

# Sample udhcpd configuration file (/etc/udhcpd.conf)# Values shown are defaults
# The start and end of the IP lease block
start 192.168.0.20  //自动分配 ip 的开始范围
end  192.168.0.254  //自动分配 ip 的结束范围
# The interface that udhcpd will useinterface 
wlan0 //自动分配的网卡设备节点

3)配置完这两个文件后开始创建热点:

hostapd -B /etc/hostapd.conf -f /var/log/hstap.log

4)设置 wlan0 的 ip

ifconfig wlan0 192.168.0.2

5)启动自动分配 ip 后台,为连接上的设备分配 ip

udhcpd -f /etc/udhcpd.conf &

6)执行以下发送热点命令脚本send_ap.sh

#!/bin/sh
ifconfig wlan0 down
ps -ef | grep hostapd | grep -v grep | awk '{print $1}' | xargs kill -9
ps -ef | grep wpa_supplicant | grep -v grep | awk '{print $1}' | xargs kill -9
ps -ef | grep udhcpd | grep -v grep | awk '{print $1}' | xargs kill -9
hostapd -B /etc/hostapd.conf -f /var/log/hostap.log
ifconfig wlan0 192.168.0.2
udhcpd -f /etc/udhcpd.conf &

然后可以用手机或者其他 wifi 设备连接 8723 发送的 ap 热点“test”,密码 12345678

7)以上几步仅仅保证了,AP服务,你可以连接、有了dhcp你可以获得动态IP,但是如何保证设备能通过这个无线网卡来上网呢,这需要配置,将无线网卡wlan0的设备传输数据转发到有线网卡eth0上面。所以需要配置一下路由转发规则,打通数据传输的链路。
执行以下路由转发脚本iptables.sh

#! /bin/sh
#设置linux系统允许ip包转发
echo "1" > /proc/sys/net/ipv4/ip_forward
  
#清除原设置
iptables -F
iptables -t nat -F
iptables -t mangle -F
iptables -X
iptables -t nat -X
iptables -t mangle -X

#设置filter table默认原则
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT

#启用NAT功能
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

进行路由转发时如果iptables出错,需要进行相关的内核配置:

Networking —->
  Networking options —->
  [*] Network packet filtering (replaces ipchains)  —>
    Core Netfilter Configuration  —>
      <*> Netfilter Xtables support (required for ip_tables)
    IP: Netfilter Configuration —>
      <*> Connection tracking (required for masq/NAT)
      <*> IP tables support (required for filtering/masq/NAT)
      <*>   IP range match support
      <*>   Packet filtering
      <*>     REJECT target support
      <*>   Full NAT

进行路由转发时如果nat出错,需要进行相关的内核配置:

Type  : tristate                                                                                                                                                                             │  
  │ Prompt: iptables NAT support                                                                                                                                                                 │  
  │   Location:                                                                                                                                                                                  │  
  │     -> Networking support (NET [=y])                                                                                                                                                         │  
  │       -> Networking options                                                                                                                                                                  │  
  │         -> Network packet filtering framework (Netfilter) (NETFILTER [=y])                                                                                                                   │  
  │           -> IP: Netfilter Configuration                                                                                                                                                     │  
  │ (1)         -> IP tables support (required for filtering/masq/NAT) (IP_NF_IPTABLES [=y])       

CONFIG_IP_NF_NAT
  │   Location:                                                                                                                                                                                  │  
  │     -> Networking support (NET [=y])                                                                                                                                                         │  
  │       -> Networking options                                                                                                                                                                  │  
  │         -> Network packet filtering framework (Netfilter) (NETFILTER [=y])                                                                                                                   │  
  │ (1)       -> IP: Netfilter Configuration 
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值