15 KiB
使用树莓派建立一个带有移动网络连接功能的路由器
树莓派是一个小型单板电脑,尽管只有信用卡大小,但是能做许多事情。事实上,这个小电脑几乎可以成为你想让它成为的任何东西。你只需要打开你的想象力。
树莓派爱好者已经做了许多不同的项目,从单个程序到复杂的自动化项目和类似气象站或者甚至智能家居设备的解决方案。这篇文章将展示怎样使用 OpenWRT 项目将你的树莓派变成带有 LTE 移动网络连接功能的路由器。
关于 OpenWRT 和 LTE
OpenWRT 是一个利用 Linux 内核为嵌入式设备开发的开源项目,它已经存在超过15年了,有着大量和活跃的社区。
有许多使用 OpenWRT 的方法,但是它的主要目的还是用在路由器上。它提供了一个带有包管理功能的完全可写的文件系统,并且因为它的的开源属性,你可以查看和修改代码并且可以贡献到开源生态。如果你喜欢对你的路由器获得更多的控制,这就是你想要的系统。
长期演进技术 (LTE) 是一个基于 GSM/EGDE 和 UMTS/HSPA 技术的无线宽带通信标准。我使用的 LTE 调制解调器是一个能够添加 3G 或 4G (LTE) 蜂窝连接到树莓派电脑的 USB 设备。
(Lukas Janenas, CC BY-SA 4.0)
安装前的准备
对这个项目来说,你需要:
- 一个带有电源线的树莓派
- 一台运行 Linux 的电脑
- 一张至少 16GB 的 SD 储存卡
- 以太网线
- LTE 调制解调器 (我使用的是 Teltonika TRM240)
- A SIM card for mobile connectivity 一张移动网络的 SIM 卡
安装 OpenWRT
首先,下载最新的 兼容树莓派的 OpenWRT 的发布版本. 在 OpenWRT 官网,你可以看到 4 个镜像:两个 ext4 文件系统的和两个 squashfs 文件系统的。我使用 ext4 文件系统。你可以下载 factory 或者 sysupgrade 镜像,这两个都运行良好。
(Lukas Janenas, CC BY-SA 4.0)
一旦你下载了镜像,你按照 以下的说明 需要解压并安装它到 SD 卡上。这将会花些时间安装固件,需要些耐心。一旦安装完成,在你的 SD 卡上将会有两个分区。一个是用来放 bootloader ,另一个是 OpenWRT 系统。
启动系统
要启动你的新系统,插入 SD 卡到树莓派,用以太网线把树莓派和你的路由器 (或者交换机) 相连,然后点亮。
如果你有使用树莓派的经验,你可能通过终端使用 SSH 访问过它,或者通过显示器和键盘连接到树莓派。 OpenWRT 工作有一点点不同。你与这个系统交互是通过网络浏览器,所以你必须能够通过网络来访问你的树莓派。
缺省状态下,树莓派使用的 IP 地址是:192.168.1.1 。用来配置树莓派的计算机必须和树莓派在同一个子网中。如果你的网络没有使用 192.168.1.x 地址,或者你不能确定,在 GNOME 打开 Settings ,导航到网络设置,选择 Manual ,然后键入以下的 IP 地址和子网掩码:
- IP address: 192.168.1.15
- Netmask: 255.255.255.0
(Lukas Janenas, CC BY-SA 4.0)
在你的电脑上打开一个浏览器然后导航到 192.168.1.1 。这将打开一个授权网页,你可以登录到你的树莓派。
(Lukas Janenas, CC BY-SA 4.0)
首次登录不需要密码,所以直接点击 Login 按钮继续。
Configure network connection
The Raspberry Pi has only one Ethernet port, while normal routers have a couple of them: one for WAN (wired area network) and the other for LAN (local area network). You have two options:
- Use your Ethernet port for network connectivity
- Use WiFi for network connectivity
使用以太网:
你应该决定使用以太网,导航到 网络 → 接口。在这个设置页面,按下蓝色的 Edit 按钮,将 LAN 接口与之关联。
(Lukas Janenas, CC BY-SA 4.0)
A pop-up window should appear. In that window, you need to enter the IP address to match the subnet of the router to which you will connect the Raspberry Pi. Change the Netmask, if needed, and enter the IP address of the router the Raspberry Pi will connect to.
(Lukas Janenas, CC BY-SA 4.0)
Save this configuration and connect your Pi to the router over Ethernet. You can now reach the Raspberry Pi with this new IP address.
Be sure to set a password for your OpenWRT router before you put it into production use!
To use WiFi
If you would like to connect the Raspberry Pi to the internet through WiFi, navigate to Network → Wireless. In the Wireless menu, press the blue Scan button to locate your home network.
(Lukas Janenas, CC BY-SA 4.0)
In the pop-up window, find your WiFi network and connect to it. Don't forget to Save and Apply the configuration.
In the Network → Interfaces section, you should see a new interface.
(Lukas Janenas, CC BY-SA 4.0)
Be sure to set a password for your OpenWRT router before you put it into production use!
Install the necessary packages
By default, the router doesn't have a lot of packages. OpenWRT offers a package manager with a selection of packages you need to install. Navigate to System → Software and update your package manager by pressing the button labeled "Update lists…".
(Lukas Janenas, CC BY-SA 4.0)
You will see a lot of packages; you need to install these:
- usb-modeswitch
- kmod-mii
- kmod-usb-net
- kmod-usb-wdm
- kmod-usb-serial
- kmod-usb-serial-option
- kmod-usb-serial-wwan (if it's not installed)
Additionally, download this modemmanager package and install it by pressing the button labeled Upload Package… in the pop-up window. Reboot the Raspberry Pi for the packages to take effect.
Set up the mobile interface
After all those packages are installed, you can set up the mobile interface. Before connecting the modem to the Raspberry Pi read, the modem instructions to set it up. Then connect your mobile modem to the Raspberry Pi and wait a little until the modem boots up.
Navigate to Network → Interface. At the bottom of the page, press the Add new interface… button. In the pop-up window, give your interface a name (e.g., mobile) and select ModemManager from the drop-down list.
(Lukas Janenas, CC BY-SA 4.0)
Press the button labeled Create Interface. You should see a new pop-up window. This is the main window for configuring the interface. In this window, select your modem and enter any other information like an Access Point Name (APN) or a PIN.
(Lukas Janenas, CC BY-SA 4.0)
Note: If no modem devices appear in the list, try rebooting your Raspberry Pi or installing the kmod-usb-net-qmi-wwan package.
When you are done configuring your interface, press Save and then Save and Apply. Give some time for the system to take effect. If everything went well, you should see something like this.
(Lukas Janenas, CC BY-SA 4.0)
If you want to check your internet connection over this interface, you can use ssh to connect to your Raspberry Pi shell. In the terminal, enter:
`ssh root@192.168.1.1`
The default IP address is 192.168.1.1; if you changed it, then use that IP address to connect. When connected, execute this command in the terminal:
`ping -I ppp0 google.com`
If everything is working, then you should receive pings back from Google's servers.
(Lukas Janenas, CC BY-SA 4.0)
ppp0 is the default interface name for the mobile interface you created. You can check your interfaces using ifconfig. It shows active interfaces only.
Set up the firewall
To get the mobile interface working, you need to configure a firewall for the mobile interface and the lan interface to direct traffic to the correct interface.
Navigate to Network → Firewall. At the bottom of the page, you should see a section called Zones.
(Lukas Janenas, CC BY-SA 4.0)
The simplest way to configure the firewall is to adjust the wan zone. Press the Edit button and in the Covered networks option, select your mobile interface, and Save and Apply your configuration. If you don't want to use WiFi to connect to the internet, you can remove wwan from the Covered networks or disable the WiFi connection.
(Lukas Janenas, CC BY-SA 4.0)
If you want to set up individual zones for each interface, just create a new zone and assign the necessary interfaces. For example, you may want to have a mobile zone that covers the mobile interface and is used to forward LAN interface traffic through it. Press the Add button, then Name your zone, check the Masquerading check box, select Covered Networks, and choose which zones can forward their traffic.
(Lukas Janenas, CC BY-SA 4.0)
Then Save and Apply the changes. Now you have a new zone.
Set up an Access Point
The last step is to configure a network with an Access Point for your devices to connect to the internet. To set up an Access Point, navigate to Network → Wireless. You will see a WiFi device interface, a disabled Access Point named OpenWRT, and a connection that is used to connect to the internet over WiFi (if you didn't disable or delete it earlier). On the Disable interface, press the Edit button, then Enable the interface.
(Lukas Janenas, CC BY-SA 4.0)
If you want, you can change the interface name by editing the ESSID option. You can also select which network it will be associated with. By default, it with be associated with the lan interface.
(Lukas Janenas, CC BY-SA 4.0)
To add a password for this interface, select the Wireless Security tab. In the tab, select the encryption WPA2-PSK and enter the password for the interface in the Key option field.
(Lukas Janenas, CC BY-SA 4.0)
Then Save and Apply the configuration. If the configuration was set correctly, when scanning available Access Points with your device, you should see a new Access Point with the name you assigned.
Additional packages
If you want, you can download additional packages for your router through the web interface. Just go to System → Software and install the package you want from the list or download it from the internet and upload it. If you don't see any packages in the list, press the Update lists… button.
You can also add other repositories that have packages that are good to use with OpenWRT. Packages and their web interfaces are installed separately. The packages that start with the prefix luci- are web interface packages.
(Lukas Janenas, CC BY-SA 4.0)
Give it a try
This is what my Raspberry Pi router setup looks like.
(Lukas Janenas, CC BY-SA 4.0)
It not difficult to build a router from a Raspberry Pi. The downside is that a Raspberry Pi has only one Ethernet port. You can add more ports with a USB-to-Ethernet adapter. Don't forget to configure the port on the interface's website.
OpenWRT supports a large number of mobile modems, and you can configure the mobile interface for any of them with the modemmanager, which is a universal tool to manage modems.
Have you used your Raspberry Pi as a router? Let us know how it went in the comments.
via: https://opensource.com/article/21/3/router-raspberry-pi
作者:Lukas Janėnas 选题:lujun9972 译者:译者ID 校对:校对者ID