本文主要目的是学习笔记,参考N多网上资料,在此谢过前辈先贤们。
First注意:
本文是在centos6下用hostapd+dnsmasq实现Wifi热点配置,以下操作请首先排除wpa_supplicant、NetworkManager这类网络管理工具的影响。
三种方式检测网卡是否支持master mode:
iwconfig wlan0 mode master #不报error就是支持做热点;
iw list #查看列出内容中support interface modes下是否有支持AP mode;
ethtool -i wlan0 #查看无线网卡的driver,查看driver是否支持AP mode(通过http://wireless.kernel.org/en/users/Drivers 这个AP模式支持的驱动列表里查找AP项为yes的为支持)
开始安装:
yum install hostapd dnsmasq -y
配置hostapd,配置文件在/etc/hostapd/hostapd.conf
【conf内容】
ctrl_interface=/var/run/hostapd
ctrl_interface_group=wheel
# Some usable default settings...
macaddr_acl=0
auth_algs=1
# ignore_broadcast_ssid=0
# Uncomment these for base WPA & WPA2 support with a pre-shared key
wpa=3
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
# rsn_pairwise=CCMP
# DO NOT FORGET TO SET A WPA PASSPHRASE!!
wpa_passphrase=******
# Most modern wireless drivers in the kernel need driver=nl80211
driver=nl80211
# Customize these for your local configuration...
interface=wlan0
hw_mode=g
channel=7
ssid=HOST-AP
配置完了,可执行hostapd /etc/hostapd/hostapd.conf测试驱动/热点是否可用;
配置dnsmasq,配置文件/etc/dnsmasq.conf
【conf修改内容】
interface=wlan0
listen-address=192.168.0.1
#no-dhcp-interface=
dhcp-range=192.168.0.50,192.168.0.150,12h
dhcp-option=3,192.168.0.1
dhcp-option=6,8.8.8.8
打开主机的路由转发:
echo 1 >/proc/sys/net/ipv4/ip_forward
ip 转发设置默认在重启系统之后会消失;若想保存 ip_forward 设置:
修改 /etc/sysctl.conf 中:
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1(if found it)
可执行sysctl -p来使内核参数即时生效。
设置无线接入点IP地址:
ifconfig wlan0 192.168.0.1/24
若已经有IP,这里可执行:ifup wlan0
若想服务开机启动,可直接编辑/etc/sysconfig/network-scripts/ifcfg-wlan0,修改BOOTPROTO=static,并添加IPADDR=1.2.3.4 NETMASK=255.255.255.0。
启动hostapd及dnsmasq网络服务:
service hostapd start
service dnsmasq start
若想实现服务开机自启动:
chkconfig hostapd on
chkconfig dnsmasq on
创建iptables规则(数据转发通过有线网卡eth0作为internet出口):
iptables -F
iptables -X
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables 设置默认在重启系统之后会消失,保存iptables规则:
/etc/init.d/iptables save
正常的话,已经可以用手机连你的wifi啦(上文中SSID好像叫HOST-AP)
********************1.
客户端使用HOSTAPD无线AP时,网速不稳定,导致某些时刻无法打开网页或者其它因为网络超时导致的网络不可访问故障。 该问题主要是因为无线数据传输校验中随机种子数较少导致的无线网络数据传输时延过大导致的。
cat /proc/sys/kernel/random/entropy_avail查看具体数值,一般小于1000;sudo apt-get install haveged安装随机数生成器(haveged);
/etc/init.d/haveged start 启动随机数生成器,提高entropy_avail数值。这样无线网络时延将恢复到正常状态,网络不在出现以上描述的问题。
2.
如果启动hostapd总是报:nl80211: Failed to set interface wlan0 into AP mode,可能是因为hostapd已经启动了,或者当前设备驱动不支持AP mode。如果是后者,有一种情况况是需要升级内核。
内核编译之前须执行make menuconfig,找到下列选项,必须勾选: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(根据linux发布,以下三项可能name不同)
<*> REJECT target support
<*> Full NAT
3.
其它相关网址:
https://wireless.wiki.kernel.org/en/users/download(linux wireless driverdownload)
http://www.361way.com/hostapd-soft-ap/2933.html(centos配置wifi热点)
http://forum.ubuntu.org.cn/viewtopic.php?p=2975544(ubuntu下配置热点)
http://blog.itpub.net/29901741/viewspace-1294741/(桥接模式配置热点)