用hostapd架设无线AP分享有线网络连接

简介

hostapd是一款软件AP(Soft AP)软件,它能够让你的无线网卡切换为master模式,模拟无线AP(无线路由器)的认证服务,负责控制管理Stations(通常可以认为带无线网卡的PC)的接入和认证。 
 
hostapd可以将电脑的无线网卡切换为AP/Master模式,支持开放式(不加密)、WEP、WPA或WPA2的工作方式,并可以设置无线网卡的各种参数,包括频率、信号、beacon包时间间隔、是否发送beacon包、是否响应探针请求、是否设置mac地址过滤条件等等。

参考文档

网上关于hostapd介绍的文章多如牛毛,但靠谱的不多,其中对此问题介绍得比较完整的主要有两篇:

安装配置

基于这两篇文章,整理我的安装过程如下。
其中:
  • hostapd,作为软AP模拟
  • dhcp,用于启动DHCP服务
  • iptables,用于数据包转发

环境

服务器:Arch Linux, Kernel 3.1.4;

软件安装

确保Arch Linux中已经正确配置好无线网络。
hostapd
# pacman -S hostapd 
resolving dependencies...
looking for inter-conflicts...

Targets (1): hostapd-0.7.3-5

Total Download Size: 0.22 MB
Total Installed Size: 0.67 MB

Proceed with installation? [Y/n]
:: Retrieving packages from community...
 hostapd-0.7.3-5-i686 224.3K 105.5K/s 00:00:02 [######################] 100%
(1/1) checking package integrity [######################] 100%
(1/1) checking for file conflicts [######################] 100%
(1/1) installing hostapd [######################] 100%
> Note: You will find some config examples in
> /etc/hostapd. The default location of several
> key configuration files for hostapd are in
> /etc/hostapd as well. They are:
> hostapd.conf --> primary configuration file
> hostapd.allow --> MAC allow list
> hostapd.deny --> MAC deny list
> Also, note that there are some self-explanatory
> examples in the directory. Feel free to change the mac
> list location by defining it differently in your hostapd.conf
> file. Please, note, however. The daemon start/stop
> script assumes you have a working config file at
> /etc/hostapd/hostapd.conf. One final thing to remember.
> You need to install madwifi if you want support for it.
#
dhcp
# pacman -S dhcp 
resolving dependencies...
looking for inter-conflicts...

Targets (1): dhcp-4.2.1.1-1

Total Download Size: 0.82 MB
Total Installed Size: 4.58 MB

Proceed with installation? [Y/n]
:: Retrieving packages from extra...
 dhcp-4.2.1.1-1-i686 835.0K 106.5K/s 00:00:08 [######################] 100%
(1/1) checking package integrity [######################] 100%
(1/1) checking for file conflicts [######################] 100%
(1/1) installing dhcp [######################] 100%

==> The dhcp server has now two rc.d scripts.
==> Use '/etc/rc.d/dhcp6' to use IPv6 dhcp server or
==> '/etc/rc.d/dhcp4' to use IPv4 dhcp server.
==> Make sure that you change your DAEMONS array in '/etc/rc.conf'!
#

配置

hostapd
hostapd的配置路径位于/etc/hostadp/hostapd.conf,在默认安装的基础上根据自己的环境修改以下配置参数。
interface=wlan0 
driver=nl80211
ssid=YOUR_SSID
channel=9
hw_mode=g
ignore_broadcast_ssid=0
auth_algs=1
wpa=3
wpa_passphrase=YOUR_PASS_PHRASE
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
dhcp
dhcp的配置路径位于/etc/dhcpd.conf。
 
这里需要注意文件名,同目录下还有一个配置文件是/etc/dhcpcd.conf,用户配置DHCP client的使用参数。
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

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

subnet 192.168.9.0 netmask 255.255.255.0 {
    range 192.168.9.10 192.168.9.100;
    option routers 192.168.9.1;
    option domain-name-servers 192.168.3.1;
    option ip-forwarding off;
    option broadcast-address 192.168.9.255;
}

使用

安装、配置完成后,即能着手开启无线AP服务。
# rc.d start hostapd 
:: Starting hostapd [DONE]
# ifconfig wlan0 192.168.9.1 netmask 255.255.255.
# rc.d start dhcp4 
:: Starting DHCPv4 Server [DONE]
# iptables -t nat -A POSTROUTING -s 192.168.9.0/24 -o eth0 -j MASQUERADE
# echo "1" > /proc/sys/net/ipv4/ip_forward
注意,为保证DHCP服务成功启动,必须核对以下配置:
  • 无线网卡的IP地址配置和DHCP配置参数相符,
  • 必须在开启DHCP服务前配置无线网卡
 




posted @ 2011-12-14 11:39  __BSD__  阅读(5563)  评论(2编辑  收藏  举报