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 安裝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=0auth_algs=1#ignore_broadcast_ssid=0# Uncomment these for base WPA & WPA2 support with a pre-shared keywpa=1wpa_key_mgmt=WPA-PSKwpa_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=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 /]#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.dinterface=wlan0bind-interfaceslisten-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.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 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 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