OpenWrt on Raspberry Pi 4

Setup OpenWrt on Raspberry Pi 4 Model B, using OpenWrt current latest stable 21.02.0.

The plan

Current router is a Asus RT-AC68U Rev. E1 with the official stock image, I’ll be setting this up in AP Mode for the time being, I will however invest in a switch with POE (supporting 802.3af) and dot1q (802.1Q VLANs) with a Ubiquiti Access Point UniFi 6 Lite, use the Raspberry Pi 4 in a router on a stick inter-VLAN configuration getting rid of the USB->Ethernet dongle and power the Raspberry Pi 4 + UniFi AP 6 Lite through POE which I will get a POE HAT for the Raspberry Pi 4, but that’s for another article sometime in the future.

DISCLAIMER: This is not an in-depth guide, you should have overall Linux, network and storage knowledge/experience, proceed on your own risk!

The Execution

Download the latest release from OpenWrt for the Raspberry Pi 4:

wget https://downloads.openwrt.org/releases/$(curl -s -S https://downloads.openwrt.org/releases/ | sed -n -E 's/.*>([0-9]+\.[0-9]+\.[0-9]+).*$/\1/p' | tail -1)/targets/bcm27xx/bcm2711/$(curl -s -S https://downloads.openwrt.org/releases/$(curl -s -S https://downloads.openwrt.org/releases/ | sed -n -E 's/.*>([0-9]+\.[0-9]+\.[0-9]+).*$/\1/p' | tail -1)/targets/bcm27xx/bcm2711/ | sed -n -E 's/.*(openwrt-.*-bcm27xx-bcm2711-rpi-4-ext4-factory.img.gz).*/\1/p')

Flash the file with Etcher to your SDCard, I noticed that the root partition doesn’t use the entire SDCard capacity so let’s increase it:

replace the device path and partition path with your values in case it’s not the same

# resize the rootfs wich is in this case partition 2
sudo parted /dev/mmcblk0 resizepart 2 100%

# fix the consistency of the filesystem
sudo e2fsck -f /dev/mmcblk0p2 -y

# then resize it to the partion size
sudo resize2fs /dev/mmcblk0p2

# and then check/fix the consistency of the filesystem again
sudo e2fsck -f /dev/mmcblk0p2 -y

Since I’ll be configuring the Raspberry Pi 4 without an internet connection with a USB->Ethernet dongle I have to download the required packages beforehand

# download the latest required packages for USB-> Dongle
curl -s -S https://downloads.openwrt.org/releases/$(curl -s -S https://downloads.openwrt.org/releases/ | sed -n -E 's/.*>([0-9]+\.[0-9]+\.[0-9]+).*$/\1/p' | tail -1)/targets/bcm27xx/bcm2711/kmods/$(curl -s -S https://downloads.openwrt.org/releases/$(curl -s -S https://downloads.openwrt.org/releases/ | sed -n -E 's/.*>([0-9]+\.[0-9]+\.[0-9]+).*$/\1/p' | tail -1)/targets/bcm27xx/bcm2711/kmods/ | sed -n -E 's/^  <tr><td class="n"><a href=".*">(.*)<\/a>.*/\1/p')/{$(curl -s -S https://downloads.openwrt.org/releases/$(curl -s -S https://downloads.openwrt.org/releases/ | sed -n -E 's/.*>([0-9]+\.[0-9]+\.[0-9]+).*$/\1/p' | tail -1)/targets/bcm27xx/bcm2711/kmods/$(curl -s -S https://downloads.openwrt.org/releases/$(curl -s -S https://downloads.openwrt.org/releases/ | sed -n -E 's/.*>([0-9]+\.[0-9]+\.[0-9]+).*$/\1/p' | tail -1)/targets/bcm27xx/bcm2711/kmods/ | sed -n -E 's/^  <tr><td class="n"><a href=".*">(.*)<\/a>.*/\1/p')/ | sed -n -E 's/  <tr><td class="n"><a href=".*">(kmod-(mii|usb-net)(_|-rtl8152).*)<\/a>.*/\1/p' | xargs | sed -e 's/ /,/g')} -O

# mount the rootfs volume and copy over the packages
cp /path/to/downloads/kmod-* /path/to/rootfs/root/

Now with that out of the way, eject the SDCard and insert it into the Raspberry Pi 4.

Configure the Asus RT-AC68U in AP Mode with manually configured IP:

IP: 192.168.1.2 # Asus RT-AC68U
Netmask: 255.255.255.0
Gateway: 192.168.1.1 # Raspberry Pi 4
DNS: 192.168.1.1 # Raspberry Pi 4

Disconnect Asus RT-AC68U then connect the Raspberry Pi 4 via the Ethernet port

# ssh to the Raspberry Pi 4
ssh root@192.168.1.1

# set a password for the root account
passwd

# installing the USB-Ethernet driver with it's dependencies
opkg install /root/kmod-*

# make opkg lists permanent
sed -i 's,/var/opkg-lists,/usr/lib/opkg/lists,' /etc/opkg.conf

# remove the Raspberry Pi 4 wifi config
>/etc/config/wireless

# disable internal wifi
echo 'dtoverlay=disable-wifi' >> /boot/distroconfig.txt

# redirect LuCI HTTP -> HTTPS
uci set uhttpd.main.redirect_https=1
uci commit uhttpd

# set SSH listening interface to 'lan'
uci set dropbear.@dropbear[0].Interface="lan"
uci commit dropbear

# power off the Raspberry Pi 4
poweroff

Connect the Raspberry Pi 4’s USB->Ethernet dongle to the ISP WAN port and the Ethernet port to the WAN (blue) port on the Asus RT-AC68U, then connect your computer to whichever of the remaining ports on the Asus RT-AC68U and power on the devices.

Once up and running you should now be able to access the LuCI Web Interface, login through http://192.168.1.1 (use the password set before with passwd)

Head over to

Network -> Interfaces -> Add new interface…

Name: wan
Protocol: DHCP Client
Device: Ethernet Adapter: "eth1"

Click Create interface then select the Firewall Settings tab on the following page and click Create / Assign firewall-zone and add WAN zone if not already added, then hit Save.

You should now have a working router with AP.

Keep your OpenWrt up to date as you would do with regular routers.

Keep your packages up to date with:

# fetch a row with upgradable packages
opkg update && opkg list-upgradable | awk '{printf "%s ", $1}'

# upgrade them (omit those you do not want to upgrade for some reason)
opkg upgrade <row from previous output>

Got a dot1q switch with PoE+: Router-on-a-stick on OpenWrt/Raspberry Pi 4


See also