CentOS 下配置hostapd共享WiFi

有两种方法可以实现软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 在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 /]=1=1wpa_key_mgmt=WPA-PSKwpa_pairwise=TKIP=XXXXXX# Most modern wireless drivers in the kernel need driver=nl80211driver=nl80211# Customize these for your local configuration…interface=wlan0hw_mode=gchannel=11ssid=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 /]=wlan0bind-interfaceslisten-address=dhcp-range=,,12h dhcp-dhcp-

这里说明下,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.242nameserver 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 stoprfkill 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 /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 MASQUERADEiptables -Fiptables -Xiptables -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

相关文章:

你感兴趣的文章:

标签云: