您的位置:首页 > 其它

将树莓派打造成超级无线路由器[自动翻墙+过滤广告](一)

2016-08-10 00:00 603 查看

本文通过 OscPress 同步至oschina,获取最新版本和更好的阅读体验请查看原文:
https://www.cellmean.com/?p=1145
折腾了一段时间的树莓派,目前已经把它变成了一台NAS和无线路由器,树莓派3使用电源是是5V/2A, 外接移动硬盘也不需要额外供电,我在树莓派挂载了一个2T的移动硬盘,开通了Samba共享后,就可以在域网访问树莓派SD卡和移动硬盘的文件,装上相应的app,在手机就可以看视频了。非常方便。近日又把它变成了无线路由器(AP),并且实现了透明代理,局域网内自动翻墙,甚至解决了困惑多时的运营商网页劫持。参考了大量的资料,也走了不少弯路,趁余热总结一下,权当笔记和总结。

无线路由(AP)

1.安装dnsmasq hostapd

sudo apt-get install dnsmasq hostapd

其中 hostapd 是用于将树莓派3板载无线网卡变成无线接入点(AP),dnsmasq 作为DHCP服务器和DNS服务器。其中DHCP服务器也可以使用 udhcpd ,不过我没有尝试这种方式。

2.禁用无线接口wlan0

编辑文件 /etc/dhcpcd.conf ,最后一行加入:

denyinterfaces wlan0

树莓派3的无线网卡默认使用dhcp获取IP的,需要先禁用,后来做无线网卡跟AP模式切换时发现这一步似乎可以省去。

3.修改网卡配置文件 /etc/network/interfaces ,把wlan0区域改成下面的配置:

allow-hotplug wlan0

iface wlan0 inet static

address 172.24.1.1

netmask 255.255.255.0

network 172.24.1.0

broadcast 172.24.1.255

# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

4. 配置hostapd, 修改文件 /etc/hostapd/hostapd.conf

# This is the name of the WiFi interface we configured above
interface=wlan0

# Use the nl80211 driver with the brcmfmac driver
driver=nl80211

# This is the name of the network
ssid=Pi3-AP

# Use the 2.4GHz band
hw_mode=g

# Use channel 6
channel=6

# Enable 802.11n
ieee80211n=1

# Enable WMM
wmm_enabled=1

# Enable 40MHz channels with 20ns guard interval
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]

# Accept all MAC addresses
macaddr_acl=0

# Use WPA authentication
auth_algs=1

# Require clients to know the network name
ignore_broadcast_ssid=0

# Use WPA2
wpa=2

# Use a pre-shared key
wpa_key_mgmt=WPA-PSK

# The network passphrase
wpa_passphrase=raspberry

# Use AES, instead of TKIP
rsn_pairwise=CCMP

这样配置了一个名称是 Pi3-AP 密码是 raspberry 的无线热点,执行:

/usr/sbin/hostapd /etc/hostapd/hostapd.conf

就能在无线网络里搜到这个热点了。但是连接之后并不能上网,我们还需要配置 DNSDHCP 服务。所以按ctrl+C退出服务。

5. 配置 dnsmasq

修改 /etc/dnsmasq.conf 文件,请先执行备份

sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.`date +%F`

内容修改成:

interface=wlan0      # Use interface wlan0
listen-address=172.24.1.1 # Explicitly specify the address to listen on
bind-interfaces      # Bind to the interface to make sure we aren't sending things elsewhere
server=114.114.114.114       # Forward DNS requests to Google DNS
domain-needed        # Don't forward short names
bogus-priv           # Never forward addresses in the non-routed address spaces.
dhcp-range=172.24.1.50,172.24.1.150,12h # Assign IP addresses between 172.24.1.50 and 172.24.1.150 with a 12 hour lease time

其中 server 表示使用DNS服务器IP,dhcp-range 表示IP分配的范围和时间

6. 设置ip转发,修改/etc/sysctl.conf ,将这行代码反注释掉:

net.ipv4.ip_forward=1

或者执行:

sudo echo 1 > /proc/sys/net/ipv4/ip_forward

接着修改 IPTABLES的规则:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

iptables 的规则在关机后会消失,我们需要把规则保存下来并且在开机后还原这些规则

sudo iptables-save > /etc/iptables.ipv4.nat

修改 /etc/rc.local, 在最后加入一行

iptables-restore < /etc/iptables.ipv4.nat

7. 开机启动 hostapd dnsmasq

sudo update-rc.d hostapd defaults

sudo update-rc.d dnsmasq defaults

然后执行

sudo reboot

重启树莓派,就能搜索到无线热点 Pi3-AP ,输入密码就能顺利连接上网了。

突然觉得篇幅有点长了,决定分成几节,下一节介绍树莓派无线网卡模式和 AP 模式切换。

感谢阅读本文,欢迎访问 微言 获取更多内容或 查看原文
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: