且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

andorid/iphone 手机通过 wifi 连接笔记本 无线网卡 共享上网

更新时间:2022-09-03 15:52:22

目的 andorid/iphone 手机通过 wifi 连接笔记本 无线网卡 共享上网!


环境
OS ubuntu 10.04 2.6.32-28-generic #55-Ubuntu SMP Mon Jan 10 23:42:43 UTC 2011 x86_64 GNU/Linux
wlan  Atheros Communications Inc. AR9285 Wireless Network Adapter (PCI-Express) (rev 01)

分为两个部分
第一部分 使用 bridge-utils(网桥) dnsmasq(使用 dnsmasq 的 dhcp 功能)
第二部分 使用 hostapd(软ap)



第一部分 使用 bridge-utils(网桥) dnsmasq(使用 dnsmasq 的 dhcp 功能)
参考 http://www.chentu.info/archives/930

步骤
1. 建立桥接网络
安装 bridge-utils
apt-get install bridge-utils

修改/etc/network/interfaces
增加以下内容:
####################################################
auto vnet0
iface vnet0 inet static
address 172.16.0.1
netmask 255.255.255.0
bridge_ports none
bridge_maxwait 0
bridge_fd 1
up iptables -t nat -I POSTROUTING -s 172.16.0.0/24 -j MASQUERADE
down iptables -t nat -D POSTROUTING -s 172.16.0.0/24 -j MASQUERADE
####################################################

up网卡
sudo ifup vnet0

2. 启用ip 转发
修改/etc/sysctl.conf 去掉这行的注释
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

运行下面命令使其生效
sysctl -p

3. 搭建dhcp服务器
安装 dnsmasq
apt-get install dnsmasq
修改/etc/dnsmasq.conf

去掉下面的注释
# Include a another lot of configuration options.
#conf-file=/etc/dnsmasq.more.conf
conf-dir=/etc/dnsmasq.d

在/etc/dnsmasq.d/目录下面增加一个名为 wifi-dhcp 的文件,内容为:
#######################################
interface=vnet0
dhcp-range=172.16.0.2,172.16.0.254,1h
dhcp-option=option:dns-server,172.16.0.1,208.67.222.222,208.67.220.220
dhcp-option=option:domain-name,precision-m65

#######################################




第二部分 使用 hostapd;
参考 http://forum.ubuntu.org.cn/viewtopic.php?f=116&;t=329742

计划
1.查看芯片是否支持Soft AP
2.编译hostapd
3.配置hostapd-minimal.conf(最简选项),使能Soft AP,手机可以WiFi接入,使得手机可以从网络中获取地址,正常上网
4.提升步骤3中的WLAN的安全性,尝试各种b/g/n和业务


实施:
1.查看Atheros芯片8295是否支持Soft AP,注意,可以看到有AP字样,表明支持
$ sudo apt-get install iw

$ iw list
Wiphy phy0
Band 1:
Capabilities: 0x11ce
HT20/HT40
SM Power Save disabled
RX HT40 SGI
TX STBC
RX STBC 1-stream
Max AMSDU length: 7935 bytes
DSSS/CCK HT40
Maximum RX AMPDU length 65535 bytes (exponent: 0x003)
Minimum RX AMPDU time spacing: 8 usec (0x06)
HT TX/RX MCS rate indexes supported: 0-7
Frequencies:
* 2412 MHz [1] (17.0 dBm)
* 2417 MHz [2] (17.0 dBm)
* 2422 MHz [3] (9.0 dBm)
* 2427 MHz [4] (13.0 dBm)
* 2432 MHz [5] (13.0 dBm)
* 2437 MHz [6] (13.0 dBm)
* 2442 MHz [7] (13.0 dBm)
* 2447 MHz [8] (13.0 dBm)
* 2452 MHz [9] (9.0 dBm)
* 2457 MHz [10] (14.0 dBm)
* 2462 MHz [11] (14.0 dBm)
* 2467 MHz [12] (17.0 dBm) (passive scanning)
* 2472 MHz [13] (17.0 dBm) (passive scanning)
* 2484 MHz [14] (disabled)
Bitrates (non-HT):
* 1.0 Mbps
* 2.0 Mbps (short preamble supported)
* 5.5 Mbps (short preamble supported)
* 11.0 Mbps (short preamble supported)
* 6.0 Mbps
* 9.0 Mbps
* 12.0 Mbps
* 18.0 Mbps
* 24.0 Mbps
* 36.0 Mbps
* 48.0 Mbps
* 54.0 Mbps
max # scan SSIDs: 4
Supported interface modes:
* IBSS
* managed
* AP <---------------硬件芯片支持AP功能
* AP/VLAN
* WDS
* monitor
* mesh point
* Unknown mode (8)
* Unknown mode (9)
Supported commands:
* new_interface
* set_interface
* new_key
* new_beacon
* new_station
* new_mpath
* set_mesh_params
* set_bss
* authenticate
* associate
* deauthenticate
* disassociate
* join_ibss
* Unknown command (68)
* Unknown command (55)
* Unknown command (57)
* Unknown command (59)
* Unknown command (67)
* set_wiphy_netns
* Unknown command (65)
* Unknown command (66)
* connect
* disconnect


2.编译hostapd

找一个目录,例如 /root
安装 git
apt-get install git-core

$ git clone git://w1.fi/srv/git/hostap.git
$ cd hostap/hostapd

$ cp defconfig .config
$ vi .config
配置#CONFIG_DRIVER_NL80211=y,去掉最前面的#

$ sudo apt-get install libnl1 libnl-dev libnl-doc
$ sudo apt-get install libssl-dev
$ make
生成hostapd hostapd_cli两个可执行文件

3.配置hostapd-minimal.conf(最简选项),使能Soft AP,手机可以WiFi接入
hostapd-minimal.conf内容如下:
interface=wlan0
bridge=vmnet0  
driver=nl80211
ssid=for_c8500
hw_mode=g
channel=1

然后启动hostapd,手机就搜到可用AP SSID为for_c8500,直接连接即可
# ./hostapd ./hostapd-minimal.conf
(注意是#,需要su后操作,或者$ sudo ./hostapd ./hostapd-minimal.conf)

注意,在conf文件中,已经加入bridge=vmnet0
用手机搜索WiFi,可以连接,可以获取本地地址,上网,成功!

5.提升步骤3中的WLAN的安全性,尝试各种b/g/n和业务
详见http://wireless.kernel.org/en/us ... apd?action=show&; amp;redirect=RTFM-AP的配置,以及hostapd 中自带的hostapd.conf,有详尽的说明

一般建议开启WPA2
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
wpa_passphrase=12345678

其他各种认证、快速切换、WDS/MESH等等都可以慢慢玩了

还有,如果长期当作AP使用,br0和eth0的绑定关系可以配置到/etc/network/interfaces
auto lo
iface lo inet loopback

# Configure br0 using ifupdown with dhcp and add eth0 to the bridge
auto br0
iface br0 inet dhcp
bridge_ports eth0

其他当作路由器/NAT(Firestarter),开启RADIUS认证什么的,以后再玩了。
一旦固定以后,要么利用Linux的机制自动化,或者写成脚本一条命令搞定!


本文转自 dongnan 51CTO博客,原文链接: 

http://blog.51cto.com/dngood/580540