wifi配置ap,ubuntu14

来源:互联网 发布:linux怎么退出编辑模式 编辑:程序博客网 时间:2024/04/30 01:26

先说以下hostapd源码:hostapd_cli是通过wpa_cli库与hostapd进行通讯的。通讯地址是hostapd配置文件的ctrl_interface=/var/run/hostapd 。

用hostapd_cli -h 可以看到他支持的命令。它只支持简单的命令。

我们也可以直接使用wpa_cli来对hostapd来进行通讯,参考:

参考:wpa_cli -i wlan0 -p /var/run/hostapd disable wlan0

wpa_cli -i wlan0 -p /var/run/hostapd status

wpa_cli -i wlan0 -p /var/run/hostapd all

wpa_cli -i wlan0 -p /var/run/hostapd set channel 11

wpa_cli -h支持的命令不光是hostapd的,因此我们可以查看源码./hostapd/ctrl_iface.c 中的hostapd_ctrl_iface_receive函数,来看看它支持的命令。



将WIFI设备配置为ap。本次文档将ubuntu14的wlan0配置为AP,连接到wlan0上的设备通过eth0连接到外网。

参考:http://www.tuicool.com/articles/2iEbAv

步骤:先桥接,再建立AP。

安装:udhcpd 建立dhcp server,hostapd建立ap。
sudo apt-get install udhcpd hostapd

配置文件:
################################dhcp配置文件:udhcpd.conf##################################################
########################################/home/surpas1/actiontec/install-dir/hostap/wlan0.conf
# Sample udhcpd configuration file (/etc/udhcpd.conf)

# The start and end of the IP lease block

start         192.168.75.20    #default: 192.168.0.20
end        192.168.75.254    #default: 192.168.0.254


# The interface that udhcpd will use

interface    wlan0        #default: eth0

#DHCPD_ENABLE="yes"


# The maximim number of leases (includes addressesd reserved
# by OFFER's, DECLINE's, and ARP conficts

max_leases    200        #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    2        #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    /home/surpas1/actiontec/install-dir/hostapudhcpd.leases    #defualt: /var/lib/misc/udhcpd.leases

# The location of the pid file
pidfile    /home/surpas1/actiontec/install-dir/hostapudhcpd.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    192.168.10.2 192.168.10.10
option    subnet    255.255.255.0
#opt    router    192.168.10.2
#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
#subnet
#timezone
#router
#timesvr
#namesvr
#dns
#logsvr
#cookiesvr
#lprsvr
#bootsize
#domain
#swapsvr
#rootpath
#ipttl
#mtu
#broadcast
#wins
#lease
#ntpsrv
#tftp
#bootfile

#################################hostapd配置文件:hostapd.conf#################################################
########################################/home/surpas1/actiontec/install-dir/hostap/hostapd_wlan0.conf
interface=wlan0
##指定配置网卡。

ctrl_interface=hostapd
#指定命令

ssid=Surpass
#配置SSID

driver=nl80211
#指定网卡驱动。

#ieee80211n=1
#802.11n标准,支持2.4,5G。100M+速率。

#wmm_enabled=1
#音视频媒体优先

channel=1

#无线网卡选用1信道,一般为11.如2.4G(2.40-2.48)有14个信道(一个channel=22mhz),channel之间也是有重叠处,1-6-11不重叠。5g又有:34,36,38...64,100,104,108...140,149,153,157,161.

#channel与hw_mode有密切的关系。如果channel与hw_mode不一致会导致AP启动失败。mode=1 chan=1-11(2.4g),mode=2 chan=36-165(5g)。如果用2.4gchannel对应的hw_mode=g,5gchannel对应的hw_mode=a.      802.11协议指定了802.11a对应5g,802.11g对应2g,其中802.11b也是2g,802.11n都支持。参考文档:http://wenku.baidu.com/link?url=K-I08AFxnJa0V3Y_2bW6EyvktIbkBUUfxpW6ZsjGdtD6Oxtw7kRR6x-nu2SaDeYRTJT_WdeexaTP270LiWYbSyG92u13w5t_F4bZmSPQPri


hw_mode=g
#网卡工作在802.11G模式(54M,2.4g,这样的和802.11n冲突。。) a,b,g选择  

ignore_broadcast_ssid=0
#This enables/disables broadcasting the ssid,1 disable.如果=0,广播ssid,sta可以扫描到他。=1不广播,扫描不到他。

wpa_group_rekey=86400
#指定86400s更新一次组密钥,如果手机等客服端不支持该功能,将会断开。

wpa=1
#认证方式为WPA1对饮TKIP CCMP

wpa_pairwise=TKIP CCMP
#加密方式为TKIP CCMP

#wpa=2
#wpa2对应CCMP
#rsn_pairwise=CCMP
#加密方式为TKIP CCMP

#WPA=0无加密

wpa_passphrase=12345678
#wpa密码

max_num_sta=5
#最多可以连接5个无线设备。


auth_algs=1

#auth_algs=1 支持 WPA 身份验证算法。auth_algs=2 表示支持 WEP。永远不要使用有线等效加密 (wired equivalent privacy, WEP),因为它非常容易破解,并且多年前就已经被完全破解了。auth_algs=3 表示支持这两种方式。

#wpa=3
#wpa_key_mgmt=WPA-PSK
#wpa_pairwise=TKIP
#rsn_pairwise=CCMP

###################################shell启动####################################################
#!/bin/bash
echo "ifconfig!"

sudo killall udhcpd

sudo killall hostapd

#下面两句解决nl80211: Could not configure driver mode问题。
##改变NetworkManager里的状态,关闭wifi,同时软锁定
sudo nmcli nm wifi off

#启用wifi设备,不同于网络状态中的启用wifi(后者改变NetworkManager里的状态)
sudo rfkill unblock wlan

sudo ifconfig wlan0 192.168.75.1 netmask 255.255.255.0

sudo ifconfig wlan0 down
#sudo ifconfig wlan0 up

echo "udhcpd!"
sudo udhcpd /home/surpas1/actiontec/install-dir/hostap/wlan0.conf

echo "hostapd!"
#-B后台运行,-d检测hostapd_wlan0.conf正确性
sudo hostapd -B -d /home/surpas1/actiontec/install-dir/hostap/hostapd_wlan0.conf

sleep 1

sudo ifconfig wlan0 up

#取消桥接:

sudo ifconfig br0 down
sudo brctl delif br0 wlan0
sudo brctl delif br0 eth0
sudo ifconfig br0 down

sudo brctl delbr br0

sudo dhclient eth0
echo "bridge is down,now up it!"
#进行桥接:
sudo brctl addbr br0
sudo brctl addif br0 wlan0
sudo ifconfig eth0 inet 0.0.0.0
sudo brctl addif br0 eth0
sudo ifconfig br0 up
sudo dhclient br0
echo "finish!"


#######################################分割线########################################################
运行shell脚本可能不能成功:nl80211: Could not configure driver mode
#下面两句解决nl80211: Could not configure driver mode问题。
##改变NetworkManager里的状态,关闭wifi,同时软锁定
#sudo nmcli nm wifi off
#启用wifi设备,不同于网络状态中的启用wifi(后者改变NetworkManager里的状态)
#sudo rfkill unblock wlan

###################################################################################################

通过上面的shell文件就可以将网卡配置为AP了,可以通过手机连接到AP,但是这个时候还不能上网。需要将wifi桥接到eth0上:
安装桥接工具:
sudo apt-get install bridge-utils

进行桥接:
sudo brctl addbr br0
sudo brctl addif br0 wlan0
sudo ifconfig eth0 inet 0.0.0.0
sudo brctl addif br0 eth0
sudo ifconfig br0 up
sudo dhclient br0

取消桥接:
sudo ifconfig br0 down
sudo brctl delif br0 wlan0
sudo brctl delif br0 eth0
sudo ifconfig br0 down
sudo dhclient eth0









###################################################################

wifi可以工作在2.4g和5g

channel=1-14为2.4g

对应设置hw_mode=g

channel>14为5g

对应设置hw_mode=a

ieee80211n=1

支持n模式,这个模式支持2.4g和5g

ht_capab=[SHORT-GI-20][SHORT-GI-40][DSSS_CCK-40][HT40+]

这个是高吞吐量属性---带宽,使用omnipeek/wireshark(需要特殊网卡,可以抓mac层数据的)抓包可以看到HT支持的属性,HT40+表示设置为40带宽上偏移,1-5channel需要使用HT40+,而9-14需要设置HT40-,表示下偏移。

具体配置hostapd的文档可以参考hostad官网对应的configuration file:即便wifi制造商会有不同,但都会安装这个标准来写。

http://w1.fi/cgit/hostap/plain/hostapd/hostapd.conf

如果网卡支持ac:

ieee80211ac=1

vht_capab=xx配置超高吞吐量

需要工作在5g channel上,它可以支持80-160带宽,80m带宽data rata可以可到390-433m/s,160m带宽可以到780-866这是环境(channel没人用的情况下)很好的情况下才可以。并且这种情况触发点是在STA那边,就是STA网卡支持AC模式,可以使用iperl工具来测试他的data rata。

0 0