Raspberry Pi as a Router
As I mentioned, I needed to have two Ethernet interfaces, and Raspberry gives me only one. I decided to use the internal Ethernet port for my local LAN and a USB-to-Ethernet adapter for WAN. Of course, this assignment is not mandatory, and if you use a USB adapter for LAN instead, your setup should work too. In my case, the LAN port on RPi is connected to a 24-port Gigabit Ethernet switch to provide connectivity for more than one device. You can buy any cheap Gigabit Ethernet switch.
Without going into much detail about how an Ethernet works, a router serves two purposes — it resolves an IP address into device MAC using the ARP protocol, and it routes traffic into outside networks, like the Internet. When two of your devices want to communicate inside the same LAN network, their traffic won’t get through your router at all, only through the switch. Hence, if you buy a cheap Gigabit switch, you’ll get a decent network performance, and it doesn’t matter what kind of router you use.
Therefore, if you have some spare Raspberry Pi 3, it can still serve you well as a router, just hook up some switch behind it, and make sure your Internet link is slower than 200 Mbit/second. Otherwise, your speed is limited. Raspberry Pi 3 won’t handle more traffic because of the internal USB2 hub mentioned earlier. RPi4 is a better choice in that case. I don’t have exact stats, but USB 3.0 throughput is 5 Gbit/s, so I believe the latest RPi should have no problem saturating an outgoing link.
The installation of the system was pretty straight-forward. I followed the official instructions. Note that if you have Raspberry Pi 4, its support is not yet in stable OpenWrt builds, and you need to download the latest development build. The image is gzip-compressed; first you need to unpack the file. I explain the required steps using the Linux command line:
As a result, you’ll get a file without the
*.gz extension. You can now flash this file into a MicroSDXC card. I'm using the
dd if=rpi-4-ext4-factory.img of=/dev/mmcblk0 bs=4096sync
You specify source file as the
if parameter, and target memory card as the
of parameter. Block size,
bs, is optional to speed things up, and
sync command makes sure everything is written into the card.
Unlike distributions like Raspbian, OpenWrt doesn’t expand your system partition to fit the whole space. Typically, you’ll get only 100 MB of space for your apps, even if your memory card has 64 GB of storage. I resized the partition manually on my PC. First, I edited the partition table with
fdisk, and then used the
resize2fs command. The animation below illustrates all these steps:
First, you need to change the partition table so that the second,
rootfs, partition fits all available space. You need to delete the partition first and then create a new one. The key is to make sure the new larger partition starts at the same offset. This operation might sound dangerous, but it's not. Of course, you need to make sure that you've picked the correct device. When you change the partition table, then remove and insert the memory card back so that Linux uses the updated partition table. Next, check the file system for errors, and finally, initiate the
The first run
Raspberry Pi has assigned the IP address of
192.168.1.1. To get access to it, connect your PC into the Ethernet port on RPi, and set its IP address to
192.168.1.2. Then you should be able to access OpenWrt through SSH:
As a next step, it’s a good practice to set up a password:
To proceed further, we need to have Internet connectivity inside OpenWrt. In my case, I changed the Raspberry Pi IP address inside the
/etc/config/network file. If you statically assign an IP from a range matching your LAN, you can hook up the Raspberry as a regular client behind your existing router. It makes further steps easier. There is no universal guideline; you're on your own.
Enabling USB-to-Ethernet adapter
If you check your available interfaces, you’ll most likely see only the internal
eth0 interface, even if your USB adapter is connected. In my case, I had to find out which chipset my adapter is using and install the appropriate kernel module.
After next reboot, you should see your USB Ethernet interface initialized:
Update network configuration
As I mentioned previously, I decided to use a USB adapter for traffic going to the Internet. Raspberry Pi uses the internal
eth0 port for that by default. We need to do changes inside the
/etc/config/network file. This time, however, we can apply the final configuration, including the final IP address for the device. You just need to make sure that you don't apply the configuration, e.g., by rebooting the device.
My file looks like this:
Install webserver and GUI
OpenWrt provides LuCI as a web UI. It allows you to manage all these settings through a simple webpage. On low-end OpenWrt routers, it usually uses
uhttpd as a webserver. However, Raspberry Pi is powerful enough to handle a full-fledged Nginx server.
You can install UI with these commands:
opkg updateopkg install luci-ssl-nginx
Optionally, you can install language packs to switch UI into your preferred language. See the official documentation for the instructions.
To enable the UI, you need to do the following:
root@OpenWrt:~# /etc/init.d/nginx enable
That’s pretty much it! Raspberry Pi is ready now to serve as a router. If you have another home router with OpenWrt (like the Mikrotik in my case), you can transfer the remaining configuration, like firewall rules, DHCP and DNS entries, and then you can turn Raspberry Pi down. It’s ready to be placed as a router now.
After reboot, you can access the web interface in your browser using the static IP set in the network configuration.
In my case, the experiment to use Raspberry Pi as my router turned out well, and I decided to make this setup permanent. After a long time, I have OpenWrt with the latest patches. My VPN performance is significantly higher, and not to mention this solution gives me 4 GB of RAM and almost 60 GB of storage, which brings new possibilities, like running my proxy server or configuring more demanding firewall rules. I’m pleased that, finally, we have development boards capable of replacing network equipment. Such boards may shape the segment of home networking significantly in upcoming years. If you have a spare Raspberry Pi, I encourage you to try to use it as a router too. It’s a fun experience.
Originally published at https://www.zahradnik.io on April 17, 2020.