日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

树莓派制作无线路由器-如何自制无线路由器

發布時間:2023/12/1 综合教程 28 生活家
生活随笔 收集整理的這篇文章主要介紹了 树莓派制作无线路由器-如何自制无线路由器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

版權: 凌云物網智科實驗室

聲明: 本文檔由凌云物網智科實驗室郭工編著。

作者: 郭文學< 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

總結

以上是生活随笔為你收集整理的树莓派制作无线路由器-如何自制无线路由器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。