A-A+

Raspberry Pi做成路由器的无线AP的方法

2015年11月27日 站长资讯 暂无评论

曾经看到很多文章把Raspberry Pi制作成无线AP,但是我今天要做的是把Raspberry Pi做成一个有NAT功能的路由器,我做这个的初衷是因为到荷兰出差后发现我的bambook无法接入宿舍里的WiFi,也许是因为宿舍无线路由器是WEP的认证方式,总之死活连不上。后来决定用Raspberry Pi+北极星光无线路由器来解决问题。

思路:

【无线路由器】-----【无线网卡--Raspberry Pi--有线RJ45端口】------【有线RJ45端口--北极星光无线路由器--无线】----Bambook

步骤一:

配置Raspberry Pi的无线网卡与有线网卡

无线网卡通过WEP连到宿舍无线路由器,并配置一个固定IP,有线网卡也配置固定IP

  1. pi@raspberrypi:~$ cat /etc/network/interfaces  
  2. auto lo  
  3.    
  4. iface lo inet loopback  
  5.    
  6. iface eth0 inet static   
  7. address 172.16.1.100  
  8. netmask 255.255.255.0  
  9. gateway 172.16.1.1  
  10.    
  11. #########################################  
  12. allow-hotplug wlan0  
  13. iface wlan0 inet static  
  14. #wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf  
  15. #iface default inet dhcp  
  16.   wireless-essid  ADSL-WiFi-c91f44  
  17.   wireless-key    1234567890  
  18. address 192.168.1.100  
  19. netmask 255.255.255.0  
  20. gateway 192.168.1.254  

步骤二:

在Raspberry Pi上架设DHCP服务器

pi@raspberrypi:~$ sudo apt-get install isc-dhcp-server

编辑dhcp.conf文件

pi@raspberrypi:~$ sudo vi /etc/dhcp/dhcpd.conf

在dhcp.conf文件的最后加上以下几行

  1. subnet 172.16.1.0 netmask 255.255.255.0 {  
  2. range 172.16.1.1 172.16.1.99;  
  3. option routers 172.16.1.100;  
  4. option domain-name-servers 8.8.8.8,8.8.4.4;  
  5. }  

在Raspberry Pi的RJ45口上连上笔记本后测试是否可以分配IP地址

pi@raspberrypi:~$ sudo service isc-dhcp-server restart

Stopping ISC DHCP server: dhcpd.

Starting ISC DHCP server: dhcpd.

步骤三:

启用Raspberry Pi的路由转发功能,并开启NAT

开启路由转发功能

pi@raspberrypi:~$ sudo vi /etc/sysctl.conf

把sysctl.conf里的 net.ipv4.ip_forward=1前的"#"号去掉后保存

开启NAT功能

制作一个开启NAT的脚本,保存为nat

#!/bin/sh

sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

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

sudo iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT

运行此脚本

  1. pi@raspberrypi:~$ ls | grep nat  
  2. nat  
  3. pi@raspberrypi:~$ sh ./nat  
  4.   
  5. pi@raspberrypi:~$ sudo iptables -L  
  6. Chain INPUT (policy ACCEPT)  
  7. target     prot opt source               destination           
  8.    
  9. Chain FORWARD (policy ACCEPT)  
  10. target     prot opt source               destination           
  11. ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED  
  12. ACCEPT     all  --  anywhere             anywhere              
  13.    
  14. Chain OUTPUT (policy ACCEPT)   
  15. target     prot opt source               destination           
  16. pi@raspberrypi:~$ sudo iptables -t nat -L  
  17. Chain PREROUTING (policy ACCEPT)  
  18. target     prot opt source               destination           
  19.    
  20. Chain INPUT (policy ACCEPT)  
  21. target     prot opt source               destination           
  22.    
  23. Chain OUTPUT (policy ACCEPT)  
  24. target     prot opt source               destination           
  25.    
  26. Chain POSTROUTING (policy ACCEPT)   
  27. target     prot opt source               destination           
  28. MASQUERADE  all  --  anywhere             anywhere              
  29. pi@raspberrypi:~$  

在/etc/network/目录下创建一个iptables的文件

pi@raspberrypi:~$ sudo touch /etc/network/iptables

把iptables内容保存到/etc/network/iptables中

  1. pi@raspberrypi:~$ sudo sh -c "iptables-save > /etc/network/iptables"  
  2.   
  3. pi@raspberrypi:~$ cat /etc/network/iptables   
  4. # Generated by iptables-save v1.4.14 on Sun Jun 15 05:45:28 2014  
  5. *filter  
  6. :INPUT ACCEPT [22972:1979567]  
  7. :FORWARD ACCEPT [0:0]  
  8. :OUTPUT ACCEPT [2421:275063]  
  9. -A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT  
  10. -A FORWARD -i eth0 -o wlan0 -j ACCEPT  
  11. COMMIT  
  12. # Completed on Sun Jun 15 05:45:28 2014  
  13. # Generated by iptables-save v1.4.14 on Sun Jun 15 05:45:28 2014  
  14. *nat  
  15. :PREROUTING ACCEPT [9719:1105033]  
  16. :INPUT ACCEPT [1273:238753]  
  17. :OUTPUT ACCEPT [675:88515]  
  18. :POSTROUTING ACCEPT [219:34192]  
  19. -A POSTROUTING -o wlan0 -j MASQUERADE  
  20. COMMIT  
  21. # Completed on Sun Jun 15 05:45:28 2014  
  22. pi@raspberrypi:~$  
  23.     在/etc/network/interfaces上加上一句up iptables-restore < /etc/network/iptables使得每次启动的时候自动生效  
  24.   
  25. pi@raspberrypi:~$ cat /etc/network/interfaces  
  26. auto lo  
  27.    
  28. iface lo inet loopback  
  29.    
  30. iface eth0 inet static   
  31. address 172.16.1.100  
  32. netmask 255.255.255.0  
  33. gateway 172.16.1.1  
  34.    
  35. #########################################  
  36. allow-hotplug wlan0  
  37. iface wlan0 inet static  
  38. #wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf  
  39. #iface default inet dhcp  
  40.   wireless-essid  ADSL-WiFi-c91f44  
  41.   wireless-key    1234567890  
  42. address 192.168.1.100  
  43. netmask 255.255.255.0  
  44. gateway 192.168.1.254  
  45.    
  46. up iptables-restore < /etc/network/iptables  

保存重启发现连上Raspberry Pi的RJ45口的便携机能自动获取IP地址,并且可以ping通外网了。

标签:

给我留言