使用Hostapd开启AP功能

编辑日期:2013/10/27


环境配置

windows7,  vbox4.2.16, CentOS6.4

vbox中的CentOS采用NAT网络连接eth0

eth0 : 10.0.2.15

route : 10.0.2.2

nameserver : 8.8.8.8 and 8.8.4.4

计划 wlan0 : 192.168.0.1


安装设置

wlan0

wlan0 采用了RTK8192CU的,使用了realtek提供的全套驱动,包括驱动、iw tools、和 wpa + hostapd 0.8

realtek提供的驱动套件,都是修改过的。替换了系统中安装的后,networkmanager中无法管理wlan0,(仅更换驱动是可管理的)。

*** ubuntu 12.04中带8192cu的驱动,是第三方改写的,支持nl80211标准,支持AP界面。


安装 dhcpd

/etc/dhcp/dhcpd.conf  内容如下

################
default-lease-time 600;
max-lease-time 7200;

#wlan0 192.168.0.1  和eth0不在同一网段
subnet 192.168.0.0 netmask 255.255.255.0 {
    range 192.168.0.100 192.168.0.150;

    # routers地址是wlan0的地址
    option routers 192.168.0.1;
    option domain-name-servers 8.8.8.8;
    option ip-forwarding off;
    option broadcast-address 192.168.0.255;
}
################

hostapd 0.8 设置

同网上的设置相同,见后文。修改如下,实际上是不需要的。

wpa=1

auth_algs=1


操作流程

1)关闭NetworkManager,防止管理权的冲突

sudo /etc/init.d/NetworkManager stop

2)启动 Hostapd,可以放在最后启动

sudo hostapd /etc/hostapd/p2p.conf

3)设置wlan0,注意和网络出口的eth0没有在一个网段,因为使用NAT

sudo ifconfig wlan0 192.168.0.1 netmask 255.255.255.0

4)启动 DHCP服务,可以放在后面启动

sudo /etc/init.d/dhcpd start

5)设置iptables表

#多数网上都没有提到先要清空表内容,或者设置内容非常繁琐。

sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F
sudo iptables -t nat -X

#核心的规则,整个iptables中实际上只需要这样一条。  注意 wlan0 192.168.0.1  和eth0不在同一网段
sudo iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j MASQUERADE

#下面两条可以不用
#sudo iptables -A FORWARD -s 192.168.0.0/24 -o eth0 -j ACCEPT
#sudo iptables -A FORWARD -d 192.168.0.0/24 -m conntrack --ctstate ESTABLISHED,RELATED -i eth0 -j ACCEPT

重要参考

http://forum.ubuntu.com.cn/viewtopic.php?p=2954033      少了(6)就不能连上外网

http://hi.baidu.com/gbthereisaway/item/9073a2e4ab2d8a374ddcafad         ***完整,采用DNSmasq,主要参考

http://roylez.herokuapp.com/2011/08/11/hostapd.html       操作流程都对,就是没有提到清空规则表,主要参考

http://www.cnblogs.com/heart-runner/archive/2011/12/14/2287370.html    参考wlan0的设置同出口的eth0不是同一网段

6)

su root
echo 1 > /proc/sys/net/ipv4/ip_forward
exit

操作问题

a)

如果hostapd不正常,会发生连接,断开再连接,不断循环。这时会看到hostapd终端中没有输出。

这时需要重新启动下hostapd。

每次第一次启动hostapd,总是连不上,需要重启下就能正常连上了。

b)

dhcpd 配置的ip地址和wlan0的不匹配。如果匹配,并分配成功,就能ping到wlan0的地址

c)

iptables和ip_forward设置不正确。------结果就是连不上外网。安卓的连接是灰的。

iptables 未清空后设置。CentOS是带有默认规则的,会影响wlan0。

ip_forward 未设置。

可选操作:# 重启生效!/etc/sysctl.conf   net.ipv4.ip_forward=1

d)

eth0的本机不能上网。iptables设置错误。

任何情况下本机都能上网。

e)

以上操作,仅在当前开机状态下有效。


附加可能会用的操作

pre-up iptables -restore < /etc/iptables.rules

post-down iptables -save > /etc/iptables.rules

iptables -t nat -D POSTROUTING 1


 sudo killall hostapd
 sudo /etc/init.d/dhcpd stop
 sudo service iptables restart


参考

http://linux.vbird.org/linux_server/0250simple_firewall.php      鸟哥的说明

http://www.cnblogs.com/zhuwenger/archive/2011/03/11/1980294.html      hostadp设置文件


附件 hostapd.conf  for realtek 8192cu hostapd0.8

##### hostapd configuration file ##############################################

interface=wlan0
ctrl_interface=/var/run/hostapd
ssid=DIRECT-RT
channel=8
#wpa=2
wpa=1
wpa_passphrase=12345678
auth_algs=1
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP TKIP
rsn_pairwise=CCMP
#bridge=br0



##### Wi-Fi Protected Setup (WPS) #############################################

eap_server=1

# WPS state
# 0 = WPS disabled (default)
# 1 = WPS enabled, not configured
# 2 = WPS enabled, configured
#wps_state=2
wps_state=0

uuid=12345678-9abc-def0-1234-56789abcdef0

# Device Name
# User-friendly description of device; up to 32 octets encoded in UTF-8
device_name=RTL8192CU

# Manufacturer
# The manufacturer of the device (up to 64 ASCII characters)
manufacturer=Realtek

# Model Name
# Model of the device (up to 32 ASCII characters)
model_name=RTW_SOFTAP

# Model Number
# Additional device description (up to 32 ASCII characters)
model_number=WLAN_CU

# Serial Number
# Serial number of the device (up to 32 characters)
serial_number=12345

# Primary Device Type
# Used format: <categ>-<OUI>-<subcateg>
# categ = Category as an integer value
# OUI = OUI and type octet as a 4-octet hex-encoded value; 0050F204 for
#       default WPS OUI
# subcateg = OUI-specific Sub Category as an integer value
# Examples:
#   1-0050F204-1 (Computer / PC)
#   1-0050F204-2 (Computer / Server)
#   5-0050F204-1 (Storage / NAS)
#   6-0050F204-1 (Network Infrastructure / AP)
device_type=6-0050F204-1

# OS Version
# 4-octet operating system version number (hex string)
os_version=01020300

# Config Methods
# List of the supported configuration methods
config_methods=label display push_button keypad


##### default configuration #######################################
#hostapd src rtl871xdrv
driver=rtl871xdrv
beacon_int=100
hw_mode=g
ieee80211n=1
wme_enabled=1
ht_capab=[SHORT-GI-20][SHORT-GI-40][HT40+]
#wpa_key_mgmt=WPA-PSK
#wpa_pairwise=CCMP
max_num_sta=8
wpa_group_rekey=86400
supported_rates=60 90 120 180 240 360 480 540
basic_rates=60 120 240




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值