Ubuntu Share WiFi(AP)
经过昨天的实践,终于搞定Ubuntu共享WiFi功能,而且不是Ad-hoc模式。从此“二奶”可以作为无线中继使用(这里指的是在Ubuntu系统下)。参考教程:
Ubuntu共享WiFi(AP)给Android/更新方法二 http://weibin.me/538
首先很重要的是,要确定无线网卡驱动是否支持master mode。如果不支持,就别想了。然后就可以按照教程安装并设置hostapd和dnsmasq。根据教程,写了个脚本,方便启动或关闭该功能。第一次写比较长的Shell脚本,所以该脚本还是不太完善,但基本可用。要注意,/etc/dnsmasq.conf还是要手动修改。脚本如下:
#!/bin/sh
# begin file: sharewifi
# Setup wireless AP, share the Internet from interface0 to interface1
# USAGE: sharewifi [ start | stop ] interface0 interface1
# EXAMPLE: sharewifi start wlan1 wlan0
help( )
{
cat << HELP
Setup wireless AP, share the Internet from interface0 to interface1
USAGE: sharewifi [ help | start | stop ] interface0 interface1
EXAMPLE: sharewifi start wlan1 wlan0
HELP
exit 0
}
start( )
{
echo Starting share wifi ......
echo Share Internet $port_in to $port_out
# Configure iptable rules
iptables -F
iptables -t nat -A POSTROUTING -s 192.168.7.0/24 -o $port_in -j MASQUERADE
iptables -A FORWARD -s 192.168.7.0/24 -o $port_in -j ACCEPT
iptables -A FORWARD -d 192.168.7.0/24 -m conntrack --ctstate ESTABLISHED,RELATED -i $port_in -j ACCEPT
# Log the message of route
#iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 80 -j LOG --log-prefix "NEW_HTTP_CONN: "
# Save iptable rules
sh -c "iptables-save > /etc/iptables.rules"
# Configure hostapd
hostapd_conf=/etc/hostapd/hostapd.conf
[ -f $hostapd_conf ] && rm $hostapd_conf
echo >> $hostapd_conf interface=$port_out
echo >> $hostapd_conf driver=nl80211
echo >> $hostapd_conf ssid=AO522-Tether
echo >> $hostapd_conf channel=1
echo >> $hostapd_conf hw_mode=g
echo >> $hostapd_conf auth_algs=1
echo >> $hostapd_conf wpa=3
echo >> $hostapd_conf wpa_passphrase=1234567890
echo >> $hostapd_conf wpa_key_mgmt=WPA-PSK
echo >> $hostapd_conf wpa_pairwise=TKIP CCMP
echo >> $hostapd_conf rsn_pairwise=CCMP
chmod 755 $hostapd_conf
# Configure /etc/dnsmasq.conf
#interface=wlan0
#bind-interfaces #这个是只监听wlan0,没有之会检测所有卡
#except-interface=lo
#dhcp-range=10.1.1.10,10.1.1.110,6h #设置dhcp地址范
killall named
killall hostapd
ifconfig $port_out 192.168.7.1
hostapd -B $hostapd_conf
/etc/init.d/dnsmasq restart
echo Sucess share wifi
exit 0
}
stop( )
{
echo Stopping share wifi ......
echo Stop share Internet $port_in to $port_out
# Configure iptable rules
iptables -F
# Log the message of route
#iptables -A INPUT -m conntrack --ctstate NEW -p tcp --dport 80 -j LOG --log-prefix "NEW_HTTP_CONN: "
# Save iptable rules
sh -c "iptables-save > /etc/iptables.rules"
# Configure hostapd
hostapd_conf=/etc/hostapd/hostapd.conf
[ -f $hostapd_conf ] && rm $hostapd_conf
# Configure /etc/dnsmasq.conf
killall named
killall hostapd
ifconfig $port_out down
ifconfig $port_out del 192.168.7.1
ifconfig $port_out up
echo Sucess stop share wifi
exit 0
}
#port_in is the network interface which connected to Internet, and default wlan1.
port_in=wlan1
#port_out is the network interface which will be setup AP, and default wlan0.
port_out=wlan0
if [ -n "$2" ]; then
port_in=$2
if [ -n "$3" ]; then
port_out=$3
fi
fi
case "$1" in
"help" )
help ;;
"start" )
start ;;
"stop" )
stop ;;
*)
help ;;
esac
# end file: sharewifi
PS. “二奶”升级到Lubuntu 12.04 LTS,感觉更好用了~