https://ustcchenjian.wordpress.com/2012/02/13/connnect-android-phone-to-internet-through-laptop-wireless-connection/给出了在win7下setup wifi router的方法,在Ubuntu11下呢?

 
如果你用的是ath9 driver,那么可以将之设为wifi router。
root@computerOfGod:~# lsmod |grep ath
ath9k                 112711  0
mac80211              393421  1 ath9k
ath9k_common           13599  1 ath9k
ath9k_hw              293964  2 ath9k,ath9k_common
ath                    19387  2 ath9k,ath9k_hw
cfg80211              172427  3 ath9k,mac80211,ath

 
需要安装hostapd
apt-get install hostapd
cp /usr/share/doc/hostapd/examples/hostapd.conf.gz /etc/hostapd/
gzip -d /etc/hostapd/hostapd.conf.gz

修改之:

interface=wlan0
driver=nl80211
ssid=test
hw_mode=g
channel=1

apt-get install dsnmasq
修改/etc/dnsmasq.conf
interface=wlan0
dhcp-range=net:wlan0,192.168.0.100,192.168.0.150,255.255.255.0,1440m
dhcp-option=wlan0,3,192.168.0.1
dhcp-option=wlan0,6,208.67.222.222,208.67.220.220

关闭dnsmasq服务在启动时打开
    update-rc.d-insserv dnsmasq disable 2
 
拷贝脚本到/usr/bin/ap_ctl,(如果你internet接口是ppp0,请将eth0改为ppp0)
#!/bin/bash

# broadcasting interface
BROADCAST="wlan0"

# receiving interface broadcast is connected to
RECEIVE="eth0"

if [[ $1 == "-0" || $1 == "--start" ]]
 then
 ## start hostapd
 echo "Starting hostapd"
 echo "    You can view the log at /var/log/hostapd.log"

 # launch hostapd daemon
 hostapd -d /etc/hostapd/hostapd.conf > /var/log/hostapd.log &

 ## start dhcp server
 echo "Starting dnsmasq"

 # set IP address
 ifconfig $BROADCAST 192.168.0.1
 sleep 2

 # launch dhcpd3 daemon
 # echo "INTERFACES=$BROADCAST" > /etc/default/dhcp
 # dhcpd3 $BROADCAST &
 dnsmasq

elif [[ $1 == "-1" || $1 == "--stop" ]]
 then
 # send signal 2 to hostapd and dhcpd3
 killall -2 hostapd  dnsmasq

elif [[ $1 == "-2" || $1 == "--ics" ]]
 then
 # create iptables rules
 iptables -A FORWARD -i $RECEIVE -o $BROADCAST -s 192.168.0.1/24 -m conntrack --ctstate NEW -j ACCEPT
 iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
 iptables -A POSTROUTING -t nat -j MASQUERADE

 # set kernel variable(s)
 echo 1 > /proc/sys/net/ipv4/conf/all/forwarding

 # edit kernel configuration
 cp /etc/sysctl.conf /etc/sysctl.conf.ap_ctl
 echo "net.ipv4.conf.default.forwarding=1" >> /etc/sysctl.conf
 echo "net.ipv4.conf.all.forwarding=1" >> /etc/sysctl.conf

 # restart networking
 /etc/init.d/networking restart

elif [[ $1 == "-3" || $1 == "--noics" ]]
 then
 # remove iptables rules
 iptables -D FORWARD 1
 iptables -D FORWARD 1

 # set kernel variable(s)
 echo 0 > /proc/sys/net/ipv4/conf/all/forwarding

 # revert kernel configuration
 mv -i /etc/sysctl.conf.ap_ctl /etc/sysctl.conf

 # restart networking
 /etc/init.d/networking restart

else
 echo $0
 echo "A tool to manage hostapd and dhcpd3"
 echo "Usage:"
 echo "    -0 --start    Start hostapd and dhcpd3"
 echo "    -1 --stop    Stop hostapd and dhcpd3 with signal 2"
 echo "    -2 --ics    Activate internet connection sharing"
 echo "            between specified interfaces"
 echo "    -3 --noics    Undo internet connection sharing settings"
fi

exit 0

运行命令激活wifi router
sudo ap_ctl --start
sudo ap_ctl --ics

root@computerOfGod:~# iwconfig wlan0
wlan0     IEEE 802.11bgn  Mode:Master  Frequency:2.412 GHz  Tx-Power=14 dBm  
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Power Management:off

可以看到wlan0运行在Master Mode下,好了,可以将你的mobile devices连接到internet了。
 
如果你需要将wifi ap支持WPA,那么你需要进一步修改/etc/hostapd/hostapd.conf
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=YourPassPhrase //这里设置你的接入口令
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Ref:

Advertisements