码迷,mamicode.com
首页 > 其他好文 > 详细

CentOS 下配置hostapd共享WiFi

时间:2015-05-14 18:41:23      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:路由   配置   

有两种方法可以实现软AP:一种是桥接模式,也就是利用新建BRIDGE将内网和外网连接起来;还有一种是路由模式,利用NAT将内网数据包与外网数据包进行转换。这里我使用的是路由模式。
tips:首先需要确认无线网卡支持AP mode,[root@localhost /]#iw list,在列出的内容里面查看Supported interface modes字段是否有AP,确认能开启AP mode时在看下面。我用的TP-WN822N V2下载速度在400KBps+,在Windows下用360wifi速度才200KBps不到。

  • 安装hostapd
  • 配置hostapd
  • 安装dnsmasq
  • 配置dnsmasq
  • 路由转发
  • 启动脚本

安装hostapd

下载hostapd
在ustc镜像上找到hostapd安装包,也可以去其他地方下载,能找到相应版本就好

[root@localhost /]#wget mirrors.ustc.edu.cn/fedora/epel/6/x86_64/hostapd-2.0-5.el6.x86_64.rpm

直接安装就可以

[root@localhost /]#yum install hostapd-2.0-5.el6.x86_64.rpm

有时,可能还需要安装libnl,出现错误提示时就安装以下吧。

配置hostapd

hostapd的配置文件在/etc/hostapd/hostapd.conf
直接看我的hostapd.conf吧

[root@localhost /]#cat /etc/hostapd/hostapd.conf
#
# This will give you a minimal, insecure wireless network.
# 
# DO NOT BE SATISFIED WITH THAT!!!
#
# A complete, well commented example configuration file is
# available here:
#
#   /usr/share/doc/hostapd-2.0/hostapd.conf
#
# For more information, look here:
#
#   http://wireless.kernel.org/en/users/Documentation/hostapd
#

#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=1
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
#rsn_pairwise=CCMP

# DO NOT FORGET TO SET A WPA PASSPHRASE!!
wpa_passphrase=XXXXXX

# Most modern wireless drivers in the kernel need driver=nl80211
driver=nl80211

# Customize these for your local configuration...
interface=wlan0
hw_mode=g
channel=11
ssid=XXXXXX

只用修改,ssid—-wifi的名字,wpa_passphrase—-wifi的密码,interface—-指定作为AP的网卡。其他基本可以不修改,hw_mode,a,b,g可选,channel信道也可以随意,1,6,11。
需要特别说明的是,driver=nl80211,nl80211是一种标准的无线驱动接口,如果你的网卡不支持这个接口还可以试试rtlXXX(忘了额)。

安装dnsmasq

看到软件名,还以为是DNS工具,其实也可以做DHCP额。安装dsnmasq的作用就是给wifi的客户端动态的分配ip这些东西,免得每次都需要手工输入。好了,安装!

[root@localhost /]#yum install dnsmasq 

这里就直接安装了,上面安装hostapd,找到了合适的源,也可以这样直接安装#yum install hostapd

配置dnsmasq

dnsmasq的配置文件在/etc/dnsmasq.conf,其实大部分软件的配置文件都在/etc文件下。

[root@localhost /]#cat /etc/dnsmasq.conf

# For debugging purposes, log each DNS query as it passes through
# dnsmasq.
#log-queries

# Log lots of extra information about DHCP transactions.
#log-dhcp

# Include a another lot of configuration options.
#conf-file=/etc/dnsmasq.more.conf
#conf-dir=/etc/dnsmasq.d

interface=wlan0
bind-interfaces
listen-address=192.168.0.1 
#no-dhcp-interface= 
dhcp-range=192.168.0.2,192.168.0.224,12h 
dhcp-option=3,192.168.0.1 
dhcp-option=6,202.114.0.242

这里说明下,interface配置的是你的AP无线网卡。listen-address是你的网卡ip。dhcp-range是你的wifi客户端自动获取ip的范围。dhcp-option=3,设置的是路由。dhcp-option=6,设置的是DNS服务器ip,不知到的话就查询以下:

[root@localhost /]# cat /etc/resolv.conf 
nameserver 202.114.0.242
nameserver 202.114.0.131

填入dhcp-option=6,中,不要和我设置的一样额,除非你知道我在哪里^!^

路由转发

启动路由转发

[root@localhost /]#echo 1 > /proc/sys/net/ipv4/ip_forward 

给无线网卡指定ip

[root@localhost /]#/sbin/ip addr add 192.168.0.1/24 dev wlan0 

这里给wlan0设备指定ip:196.128.0.1,子网掩码:255.255.255.0

NAT映射包,建立iptables规则

[root@localhost /]#iptables -F
[root@localhost /]#iptables -X
[root@localhost /]#iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

启动脚本

每次开启AP共享wifi都需要设置wlan0 IP,开启dnsmasq,开启hostapd,设置iptables规则,额,挺麻烦的。
使用下面这个脚本,开启时#sh /home/my/ap.sh start,关闭时#sh /home/my/ap.sh stop

[root@localhost /]#cat /home/my/ap.sh
#!/bin/sh
#Clean things upinit() 
{ 
#Stop NetworkManager, if already running (it will disturb you)
sysctl net.ipv4.conf.all.forwarding=1
/usr/sbin/serviceconf  network-manager stop
#Stop named, if already running. dnsmasq cannot run because it take up port 53 
#killall named 
#Stop dnsmasq, if already running 
rfkill unblock all
/usr/sbin/serviceconf dnsmasq stop 
#Stop hostapd, if already running 
/usr/bin/pkill hostapd 
#Bring down wlan0 
/sbin/ip link set down dev wlan0
}
start() { 
#First clean things up 
#Start hostapd, and it will automatically be bringed up 
hostapd -B /etc/hostapd/hostapd.conf 
#Set ip on wlan0 
/sbin/ip addr add 192.168.0.1/24 dev wlan0 
#Start dnsmasq 
/usr/sbin/serviceconf dnsmasq start
#Start ip_forward 
echo 1 > /proc/sys/net/ipv4/ip_forward 
#add iptables rule for NAT 
#/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -F
iptables -X
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
}
stop() { 
#Remove iptables rule 
/sbin/iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE 
#Stop dnsmasq 
/usr/sbin/serviceconf dnsmasq stop 
#Stop hostapd 
/usr/bin/pkill hostapd 
#bring down wlan0, and its ip address will automatically be removed 
/sbin/ip link set down dev wlan0
}
case "$1" in
‘start‘) 
  start 
  ;;
‘stop‘) 
  stop 
  ;;
*) 
echo "usage $0 start|stop"
esac

CentOS 下配置hostapd共享WiFi

标签:路由   配置   

原文地址:http://blog.csdn.net/lasercoder/article/details/45723763

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!