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

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

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

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

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

作者: 郭文学< 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、怎么查家里的wifi密码 2、家里路由器密码忘记了怎么办 3、路由器密码怎么查? 4、家里的wifi密码忘记了怎么查 5、自家的wifi...

路由器怎么用小火箭

路由器怎么用小火箭

当朋友们看到这个文章时想必是想要了解路由器怎么用小火箭相关的知识,这里同时多从个角度为大家介绍小火箭路由配置相应的内容。 本文内容目录一览: 1、电脑管家的加速小火箭无线网怎么打开 2、小火箭全局路由怎么选择 3、小火箭节点可以直接输入到路由器里面吗? 4、华硕路由器怎么添加小火箭...

路由器怎么设定自动关闭

路由器怎么设定自动关闭

针对路由器怎么设定自动关闭这个问题,本文将综合不同朋友对这个路由器怎么设定自动关闭功能的知识为大家一起来解答,希望能帮到大家 本文内容目录一览: 1、路由器怎么设置定时断网 2、TP-link无线路由器怎样设置成定时自动断网? 3、tplink路由器怎么定时开关 4、如何给路由器设置定时...

路由器怎么设置封闭网站

路由器怎么设置封闭网站

当朋友们看到这个文章时想必是想要了解路由器怎么设置封闭网站相关的知识,这里同时多从个角度为大家介绍路由器如何设置禁止访问网站相应的内容。 本文内容目录一览: 1、蚂蚁路由器屏蔽了网站怎么设置 2、路由器怎么屏蔽网站? 3、如何让无线路由器上网限制那些网站不能上 4、路由器怎么设置某网站禁...

怎么加强路由器信号最好

怎么加强路由器信号最好

今天和朋友们分享怎么加强路由器信号最好相关的知识,相信大家通过本文介绍也能对怎么样加强无线路由器的信号有自已的收获和理解。自己轻松搞问题。本文内容目录一览: 1、怎么才能让自己家里的WiFi信号增强? 2、家里的wifi信号弱怎么增强 3、怎么增强WIFI信号 4、如何增强路由器信号...

桥接路由器怎么插

桥接路由器怎么插

今天给各位分享桥接路由器怎么插的知识,其中也会对桥接路由器插网线进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站 本文内容目录一览: 1、无线路由器怎么桥接另一个路由器 2、路由桥接接线怎么接 3、如何设置桥接路由器? 4、两个路由器怎么桥接 两个无线路由器桥接方法 5、无线...