版权: 凌云物网智科实验室

声明: 本文档由凌云物网智科实验室郭工编著。

作者: 郭文学< QQ: 281143292 guowenxue@gmail.com>

版本: v1.0.0

该实验主要目的是让树莓派的有线网卡(eth0)连接Internet,然后让无线网卡工作在热点模式(AP,Access Point)提供接入,这样我们的手机、电脑等就可以通过无线连接树莓派上网了。通过该实验,我们可以了解手机电脑开热点、以及无线路由器的工作原理,同时也会对计算机网络有更加深入的认识。下面是我们实验的网络拓补图:

树莓派制作无线路由器-如何自制无线路由器-编程之家

一、软件安装和网络配置

首先安装两个制作无线路由器必需的软件:

hostapd: 该软件能使无线网卡工作在软AP(Access Point)模式,作为无线路由器使用,提供其他无线网卡接入上网;

wpa_supplicant: 该软件是一个连接、配置WIFI的客户端软件,让无线网卡工作在网卡模式,用来连接无线路由器上网;

dnsmasq: 该软件能够同时提供DHCP和DNS服务;

pi@raspberrypi:~ $ sudo apt-get update

pi@raspberrypi:~ $ sudo apt-get install hostapd dnsmasq

在最新的树莓派版本中,所有的网络接口默认使用dhcpd程序来进行管理并动态获取IP地址。因为wlan0工作在AP模式,他要给连上来的客户端提供IP地址,这时我们需要静态配置IP地址,所以先在配置文件 /etc/dhcpcd.conf 中最下面添加一行去禁用 wlan0:

pi@raspberrypi:~$ sudo vim /etc/dhcpcd.conf

#interface eth0
#fallback static_eth0
denyinterfaces wlan0

接下来我们在 /etc/network/interfaces 中静态配置无线网卡的IP地址,这里我们将有线网卡的IP静态配置成192.168.2.13,网关配置成我的无线路由器的IP地址 192.168.2.1,这个配置需要根据大家的实际网络情况来配置,假如树莓派有线网卡连接的路由器的LAN口IP地址是192.168.0.1,那么它的IP地址应该是192.168.0.x,gateway就是路由器的IP地址192.168.0.1:

pi@raspberrypi:~ $ sudo vim /etc/network/interfaces

# 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.d
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.2.13
netmask 255.255.255.0
gateway 192.168.2.1
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.10.1
netmask 255.255.255.0

pi@raspberrypi:~ $ sudo systemctl disable dhcpcd 关闭dhcpd管理树莓派网络服务

pi@raspberrypi:~ $ sudo systemctl enable networking 使用networking管理树莓派网络服务

pi@raspberrypi:~$ sudo reboot 重启生效

二、无线路由器相关软件配置

接下来修改hostapd程序的配置文件,该配置文件是无线路由器的相关配置,该配置中ssid选项用来配置路由器SSID为Pi3-APwpa_passphrase用来配置连接无线路由器的密码为raspberry

pi@raspberrypi:~$ sudo vim /etc/hostapd/hostapd.conf

# 该选项配置hostapd监听树莓派的无线网卡wlan0
interface=wlan0
# 使用Linux内核里的nl80211驱动,树莓派linux内核里默认使能了
driver=nl80211
# 这里配置树莓派的热点名称
ssid=Pi3-AP
# 配置AP兼容802.11g
hw_mode=g
# IEEE 802.11b/g标准工作在2.4G频段,频率范围为2.400—2.4835GHz,共83.5M带宽。划分为14个子信道,每个子信道宽度为22MHz,相邻信道的中心频点间隔5MHz。这里设置使用频段6
channel=6
# 配置AP兼容802.11n
ieee80211n=1
# 使能WMM,它是WiFi多媒体的缩写,是802.11e 标准的一个子集,可以把它看作某种协议或者功能。 wmm主要是用来优化语音、视频等多媒体数据流的网络通信质量。
wmm_enabled=1
# 设置IEEE 802.11n标准支持的各项特性
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
# 指定MAC地址过滤规则。0表示除非在禁止列表否则同意,1表示除非在同意列表否则禁止。2表示使用外部RADIUS服务器
macaddr_acl=0
# auth_algs指定采用哪种认证算法,采用位域(bit fields)方式来指定
auth_algs=1
# 加入此配置项后重启启动wifi热点模块即可很方便的隐藏SSID,如需不隐藏则将设置为0
ignore_broadcast_ssid=0
# 使用WPA2认证方式
wpa=2
# 使用WPA2-PSK
wpa_key_mgmt=WPA-PSK
# 设置连接该WiFi的密码
wpa_passphrase=raspberry
# 使用安全性能更高的AES加密,而不是TKIP
rsn_pairwise=CCMP

修改hostapd的启动配置文件,让系统启动时能够找到hostapd的配置文件:

pi@raspberrypi:~ $ sudo vim /etc/default/hostapd

# Defaults for hostapd initscript
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
DAEMON_CONF=”/etc/hostapd/hostapd.conf”
# Additional daemon options to be appended to hostapd command:-
# -d show more debug messages (-dd for even more)
# -K include key data in debug messages
# -t include timestamps in some debug messages
#
# Note that -B (daemon mode) and -P (pidfile) options are automatically
# configured by the init.d script and must not be added to DAEMON_OPTS.
#
#DAEMON_OPTS=””

这时候,可以使用下面命令启动测试 hostapd

pi@raspberrypi:~ $ sudo hostapd -B /etc/hostapd/hostapd.conf

Configuration file: /etc/hostapd/hostapd.conf

Failed to create interface mon.wlan0: -95 (Operation not supported) 不用关心这个错误

wlan0: Could not connect to kernel driver

Using interface wlan0 with hwaddr b8:27:eb:e1:95:c3 and ssid “Pi3-AP”

wlan0: interface state UNINITIALIZED->ENABLED

wlan0: AP-ENABLED

通过笔记本或电脑会发现 无线AP Pi3-AP,但是连接不上,这是因为树莓派的无线网卡并没有开启 DHCP和DNS服务器,接下来我们配置dnsmasq。

pi@raspberrypi:~$ sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig

pi@raspberrypi:~$ sudo vim /etc/dnsmasq.conf

# Configuration file for dnsmasq.
#
# Format is one option per line, legal options are the same
# as the long options legal on the command line. See
# “/usr/sbin/dnsmasq –help” or “man 8 dnsmasq” for details.
# Use interface wlan0
interface=wlan0
# Explicitly specify the address to listen on
listen-address=192.168.10.1
# Bind to the interface to make sure we aren’t sending things elsewhere
bind-interfaces
# Forward DNS requests to 114DNS
server=114.114.114.114
# Don’t forward short names
domain-needed
# Never forward addresses in the non-routed address spaces.
bogus-priv
# Assign IP addresses between 192.168.10.100 and 192.168.10.200 with a 12 hour lease time
dhcp-range=192.168.10.100,192.168.10.200,12h
#log-queries
#log-facility=/var/log/dnsmasq.log

pi@raspberrypi:~$ sudo service dnsmasq restart

开启DHCP和DNS服务之后,我们的电脑可以获取IP地址,并连接到树莓派上,但是电脑还是不能上网。这时我们需要开启Linux的内核的IP转发以及使用iptables做NAT表,让无线网卡的数据通过有线网卡转发出去。

开启Linux内核的IP转发功能:

pi@raspberrypi:~$ sudo sh -c “echo 1 > /proc/sys/net/ipv4/ip_forward”

开启树莓派有线网卡和无线网卡的转发功能:

不管现在eth0的出口获得了怎样的动态ip,MASQUERADE会自动读取eth0现在的ip地址然后做SNAT出去这样就实现了很好的动态SNAT地址转换:

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.local

sh -c “echo 1 > /proc/sys/net/ipv4/ip_forward”

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

exit 0

然后重启生效:

pi@raspberrypi:~$ sudo reboot

接下来我们的电脑就可以连到树莓派上上网了。

树莓派制作无线路由器-如何自制无线路由器-编程之家树莓派制作无线路由器-如何自制无线路由器-编程之家

三、树莓派切换回网卡模式

在做完无线路由器模式后,如果需要把树莓派切换回网卡模式连接无线路由器上网,则可以进行如下修改:

首先取消无线路由器的静态IP地址配置,并使能wpa_supplicant软件:

pi@raspberrypi:~ $ sudo vim /etc/network/interfaces

# 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.d
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
# wpa_supplicant station mode
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
# hostapd AP mode
#allow-hotplug wlan0
#iface wlan0 inet static
#address 192.168.10.1
#netmask 255.255.255.0

取消iptables路由转发默认启动:

pi@raspberrypi:~ $ sudo vim /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will “exit 0” on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ “$_IP” ]; then
printf “My IP address is %s\n” “$_IP”
fi
#sh -c “echo 1 > /proc/sys/net/ipv4/ip_forward”
#iptables-restore < /etc/iptables.ipv4.nat
exit 0

停止hostapd服务,启动wpa_supplicant服务

pi@raspberrypi:~ $ sudo systemctl disable hostapd.service

pi@raspberrypi:~ $ sudo systemctl enable wpa_supplicant.service

pi@raspberrypi:~ $ sudo reboot