当前位置:首页 > wifi设置知识 > 正文内容

树莓派制作无线路由器-如何自制无线路由器,我们应该这样做

秋天2023年12月31日 14:02:45wifi设置知识89
如果您遇到了树莓派制作无线路由器-如何自制无线路由器 的问题,那么本文将为您提供详细的解决方案,同时还会涉及到无线路由器的相关知识点。

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

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

作者: 郭文学< QQ: 281143292 guowenxuegmail.com>

版本: v1.0.0

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

一、软件安装和网络配置

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

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

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

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

piraspberrypi:~当你遇到路由器和WiFi网络问题时,不要惊慌,只需参考我们的文章或联系我们的客服获取帮助,我们将尽快为你解决问题。 $ sudo apt-get update

piraspberrypi:~ $ sudo apt-get install hostapd dnsmasq

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

piraspberrypi:~$ 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:

piraspberrypi:~ $ 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

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

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

piraspberrypi:~$ sudo reboot 重启生效

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

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

piraspberrypi:~$ 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的配置文件:

piraspberrypi:~ $ 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

piraspberrypi:~ $ 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。

piraspberrypi:~$ sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig

piraspberrypi:~$ 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

piraspberrypi:~$ sudo service dnsmasq restart

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

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

piraspberrypi:~$ sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

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

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

piraspberrypi:~$ sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

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

piraspberrypi:~$ sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

这时候笔记本或手机再连上树莓派上,就可以上网了。当然,由于上面命令都是手动执行的,树莓派上电后,并不会执行他们,这时我们需要进行一些配置,让系统启动后就生效:

保存当前的防火墙策略到配置文件中:

piraspberrypi:~$ sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

修改系统启动脚本,添加启动任务:

piraspberrypi:~$ sudo vim /etc/rc.local

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

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

exit 0

然后重启生效:

piraspberrypi:~$ sudo reboot

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

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

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

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

piraspberrypi:~ $ 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路由转发默认启动:

piraspberrypi:~ $ 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服务

piraspberrypi:~ $ sudo systemctl disable hostapd.service

piraspberrypi:~ $ sudo systemctl enable wpa_supplicant.service

piraspberrypi:~ $ sudo reboot

~

扫描二维码推送至手机访问。

版权声明:本文由路由设置网发布,如需转载请注明出处。

本文链接:https://www.shoulian.org/luyou/post/133459.html

分享给朋友:

“树莓派制作无线路由器-如何自制无线路由器,我们应该这样做” 的相关文章

路由器怎么打开屏蔽

路由器怎么打开屏蔽

针对路由器怎么打开屏蔽这个问题,本文将综合不同朋友对这个路由器怎么打开屏蔽网络的知识为大家一起来解答,希望能帮到大家 本文内容目录一览: 1、路由器怎么设置屏蔽 2、tp-link路由器怎么设置屏蔽 3、路由器怎么屏蔽设置 4、无线路由器怎么去屏蔽无线网络怎么设置,求大神 路由器怎么设...

云服务路由器怎么设置

云服务路由器怎么设置

针对云服务路由器怎么设置这个问题,本文将综合不同朋友对这个路由虚拟服务器怎么设置的知识为大家一起来解答,希望能帮到大家 本文内容目录一览: 1、云路由器TPDDNS如何使用 2、tp云路由器怎么设置dmz主机 3、阿里智能云路由器如何设置? 4、紫光Mywifi云智能路由器是怎么设置的?...

路由器怎么放才稳定

路由器怎么放才稳定

今天给各位分享路由器怎么放才稳定的知识,其中也会对路由器的如何设置才能快速和稳定进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站 本文内容目录一览: 1、路由器怎么设置更稳定 2、路由器怎么设置稳定性 3、如何设置路由器让wifi更稳定? 4、路由器怎么设置最稳定 5、怎么设...

路由器跟终端怎么接

路由器跟终端怎么接

有很多朋友不知道路由器跟终端怎么接要如何操作,今天为大家整理了很多路由器和终端设备如何连接相关的答案,组成一篇内容丰富的文章,希望能到您 本文内容目录一览: 1、联通FTTH终端连接路由器的方法 2、终端和电脑与路由器怎样连接 3、EOC终端设备怎么和路由器连接? 4、光纤终端怎么接路由...

移动路由器双频怎么设置

移动路由器双频怎么设置

当朋友们看到这个文章时想必是想要了解移动路由器双频怎么设置相关的知识,这里同时多从个角度为大家介绍移动路由器双频怎么设置的相应的内容。 本文内容目录一览: 1、怎么设置双频路由器 2、双频无线路由器怎么设置 3、双频路由器怎么设置 怎么设置双频路由器 导语:双频无线路由器,指同时工作在2....

路由器原有密码怎么设置

路由器原有密码怎么设置

本篇文章给大家谈谈路由器原有密码怎么设置,以及路由器的密码设置怎么弄对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。 本文内容目录一览: 1、路由器怎么设置密码? 2、路由器怎么设置密码 3、无线路由器怎么重置密码? 路由器怎么设置密码? 现在使用无线路由器的朋友很多,因为无线路由...