本文共 3668 字,大约阅读时间需要 12 分钟。
前言:接着上一篇博客,这次使用同一个树莓派制作无线路由器。上一次已经使能了树莓派远程登录的功能,通过SecureCRT登录到树莓派上操作。
一.更新树莓派软件列表,下载并安装制作无线路由器必须的两个软件。
pi@raspberrypi:~ $ sudo apt-get update 运行该命令后下载需要等待一段时间pi@raspberrypi:~ $ sudo apt-get install hostapd dnsmasq
hostapd: 该软件能使无线网卡工作在软AP(Access Point)模式,即无线路由器; dnsmasq: 该软件能够同时提供DHCP和DNS服务;二.关闭无线路由模式。
在最新的树莓派版本中,所有的网络接口默认使用dhcpd程序来进行配置,因为wlan0工作在AP模式,所以我们要手动给他静态配置IP地址,所以先在配置文件 /etc/dhcpcd.conf 中最下面添加一行去禁用 wlan0:
pi@raspberrypi:~ $ sudo vim /etc/dhcpcd.conf#interface eth0#fallback static_eth0denyinterfaces wlan0
三.在 /etc/network/interfaces 中静态配置无线网卡的IP地址:
pi@raspberrypi:~ $ sudo vim /etc/network/interfaces sudo: vim: command not found 树莓派中没有安装vim编辑器,执行下面这个命令,再执行上面的命令进入配置文件。 pi@raspberrypi:~ $ sudo apt-get install vim 添加修改如下:#interfaces(5) file used by ifup(8) and ifdown(8)#Please note that this file is written to be used with dhcpcd#For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'#Include files from /etc/network/interfaces.d:source-directory /etc/network/interfaces.dauto loiface lo inet loopbackauto eth0iface eth0 inet staticaddress 192.168.x.xnetmask 255.255.255.0gataway192.168.x.1allow-hotplug wlan0iface wlan0 inet staticaddress 192.168.y.ynetmask 255.255.255.0
关闭dhcpd管理树莓派网络服务
pi@raspberrypi:~ $ sudo systemctl disable dhcpcd 使用networking管理树莓派网络服务 pi@raspberrypi:~ $ sudo systemctl enable networking 重启生效 pi@raspberrypi:~ $ sudo reboot 此时树莓派已经失去无线功能,用一根网线插到同一网段的路由器的lan口,这样树莓派就可以通过有线上网了。四.修改hostapd程序的配置文件
pi@raspberrypi:~ $ sudo vim /etc/hostapd/hostapd.conf
#设置默认的接入点为无线网卡 wlan0interface=wlan0#设置驱动程序为 nl80211driver=nl80211#设置无线网络 SSID 为 Liushoujin's Raspberrypissid=Liushoujin's Raspberrypi#设置网卡的工作模式为 2.4GHzhw_mode=g#设置无线通道为6,如果发现连接速度慢或有干扰,也可以设置为其他数值 channel=6#使能 802.11nieee80211n=1#使能 WMMwmm_enabled=1#使能具有20ns保护间隔的40MHz通道ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]#接受所有MAC地址macaddr_acl=0#使用WPA身份验证auth_algs=1#要求客户端知道网络名称ignore_broadcast_ssid=0#使用WPA2wpa=2#使用预共享密钥wpa_key_mgmt=WPA-PSK#网络密码wpa_passphrase=12345678#使用AES代替TKIPrsn_pairwise=CCMP
修改hostapd的启动配置文件,让系统启动时能找到hostspd的配置文件:
pi@raspberrypi:~ $ sudo vim /etc/default/hostapd 取消“#DAEMON_CONF = “””最前面的注释号“#”。DAEMON_CONF="/etc/hostapd/hostapd.conf"
这时候,可以使用下面命令启动测试hostapd
pi@raspberrypi:~ $ sudo hostapd -B /etc/hostapd/hostapd.conf 通过笔记本或者手机会发现我之前配置的无线网,但是连接不上,这是因为树莓派的无线网卡并没有开启DHCP和DNS服务。五.配置dnsmasq。
pi@raspberrypi:~ $ sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
pi@raspberrypi:~ $ sudo vim /etc/dnsmasq.conf#使用接口wlan0interface=wlan0#显示指定要监听的地址,为之前树莓派静态配置的IPlisten-address=192.168.y.y#绑定到接口bind-interfaces#配置DNS服务器的IP,也可为4.2.2.2server=8.8.8.8#不要转发短名字domain-need#不要在非路由地址空间中转发地址bogus-priv#路由器动态分配的IP范围,也决定可以接受最大主机范围,24h是主机连接的最长时间dhcp-range=192.168.y.100,192.168.y.254,24h
DNS服务重启:
pi@raspberrypi:~ $ sudo service dnsmasq restart开启DHCP和DNS服务之后,我们的电脑可以获取IP地址,并连接到树莓派上,但是电脑还是不能上网。这时我们需要开启Linux的内核的IP转发以及使用iptables做NAT表,让无线网卡的数据通过有线网卡转发出去。
六.开启数据转发功能
开启树莓派有线网卡和无线网卡的转发功能:
pi@raspberrypi:~ $ sudo sh -c “echo 1 > /proc/sys/net/ipv4/ip_forward”将下面命令作为一个shell脚本,写入系统的配置中,树莓派上电执行。
pi@raspberrypi:~ $ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE pi@raspberrypi:~ $ sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT pi@raspberrypi:~ $ sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
保存到当前的防火墙策略到配置文件中:
pi@raspberrypi:~ $ sudo sh -c “iptables-save > /etc/iptables.ipv4.nat”修改系统,添加启动任务:
pi@raspberrypi:~ $ sudo vim /etc/rc.localsh -c "echo 1 > /proc/sys/net/ipv4/ip_forward" iptables-restore < /etc/iptables.ipv4.nat exit 0
重启生效:
pi@raspberrypi:~ $ sudo reboot最后就可以通过树莓派上网啦!!!
后序:做路由器摸索了快一个星期。这次边做边写文档,真的遇到不少问题,好几次做到一半继续不下去,文档也卡住。最后通过借鉴大佬的文档终于将树莓派做成了无线路由器。还是有很多问题不好理解的,比如DNS服务和数据转发,下去之后再好好消化吧!
转载地址:http://oyhgn.baihongyu.com/