现在的位置: 首页 > 综合 > 正文

linux 发无线 软AP hostapd+dnsmasq

2013年11月26日 ⁄ 综合 ⁄ 共 1794字 ⁄ 字号 评论关闭

取自http://blog.csdn.net/q1302182594/article/details/8689603,博主的情况和我基本一样,都是校园网玩linux想给安卓发无线,配置信息基本不用改,只是他命令里一些空格不清楚,而且我所需要的只是这篇博文的一部分,另外还是想自己记录一下,所以转过来。

1.安装软件:sudo apt-getinstall hostapd dnsmasq 

2.配置hostapd:配置文件是/etc/hostapd.conf,如果装了以后没有就自己新建一个,写入:

interface=wlan0
driver=nl80211
ssid=gilzhyWifi #wifi名
hw_mode=g
channel=11
dtim_period=1
rts_threshold=2347
fragm_threshold=2346
macaddr_acl=0
auth_algs=3
ieee80211n=0
wpa=3
wpa_passphrase=12345678 #密码,行末不要有空格
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

3.配置dnsmasq:配置文件是/etc/dnsmasq.conf,同样的,没有就自己新建一个,写入:

interface=wlan0
bind-interfaces #这个是只监听wlan0,没有之会检测所有卡
except-interface=lo
dhcp-range=192.168.1.100,192.168.1.254,12h #设置dhcp地址范围,即租借时间6小时
dhcp-option=3,192.168.1.1 #为手机配置网关,要和dhcp-arange对应,这涉及网络的知识,不详细介绍。
dhcp-option=6,202.114.128.2 #为手机配置dns,你可以根据实际情况去修改

写入前配置文件应该是只有注释的,如果是这样就只要写入这几行,如果有非注释的address=/#/10.0.0.1,把它注释掉

4.写个脚本添加路由规则,新建myap,写入:

#!/bin/sh
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t nat -APOSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
iptables -A FORWARD -s 192.168.1.0/24 -o eth0 -j ACCEPT
iptables -A FORWARD -d 192.168.1.0/24 -m conntrack --ctstate ESTABLISHED,RELATED -i eth0 -j ACCEPT

5.确保打开转发功能:/etc/sysctl.conf找到net.ipv4.ip_forward=1,如果被注释了,取消注释

6.启动AP:

su
killall named
killall hostapd
ifconfig wlan0 192.168.1.1
hostapd -B /etc/hostapd.conf
/etc/init.d/dnsmasq restart
echo 1 >/proc/sys/net/ipv4/ip_forward
./myap

7.以后用时,每次都需要输:

ifconfig wlan0 192.168.1.1
hostapd -B /etc/hostapd.conf
/etc/init.d/dnsmasq restart

比较麻烦,修改myaq:

#!/bin/sh
ifconfig wlan0 192.168.1.1
hostapd -B /etc/hostapd.conf
/etc/init.d/dnsmasq restart
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t nat -APOSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
iptables -A FORWARD -s 192.168.1.0/24 -o eth0 -j ACCEPT
iptables -A FORWARD -d 192.168.1.0/24 -m conntrack --ctstate ESTABLISHED,RELATED -i eth0 -j ACCEPT

每次使用时,切换到root然后运行myap

抱歉!评论已关闭.