OpenWRT on Raspberry pi 4
因為手邊缺一台router,看著手邊的Raspberry pi,不如自己搞一台看看。
目標功能:
- 其他Router(AP) => WIFI => Raspberry PI(Station)
- Raspberry PI(Station) => (Ethernet, WIFI) => Device
- Openvpn (連接NordVPN)
- Docker (架測Nextcloud當作雲端儲存)
Environment:
- ASUS USB-AC51(mt7602u) network card
- Raspberry pi 4b+
- Ubuntu 21.04
- 64GB up記憶卡
- TTL to USB (連接Raspberry pi和PC)
我會挑ASUS USB-AC51,原因是OpenWRT有內建mt7602u的driver。
OpenWRT支援的USB WIFI不多,要確定有支援或是有第三方提供(From github),才有辦法支援。
會用到外接USB WIFI還有一個原因,就是因為Raspberry pi本身無線網路卡不支援AP+STA的混合模式,也就是不能一邊連上網,一邊分享。
參考:
Build OpenWRT於Raspberry Pi4
編譯OpenWRT
- 編譯環境安裝:
- 從官方下載OpenWRT Source code
- 下載需要的package
- 設定
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
可以使用如參考頁面的基礎設定(如下),不過因為我會用到其他功能(包含mt7602u驅動),所以可以直接引用我的設定檔。
- Target System = Broadcom BCM27xx
- Subtarget = BCM2711 boards 64bit
- Target Profile = Raspberry Pi 4b
- Target Images = squashfs
- Kernel Modules -
– USB Support = kmod-usb-hid
= kmod-usb-net
= kmod-usb-net-asix
= kmod-usb-net-asix-ax88179
= kmod-usb2
= kmod-usb3
- Libraries
– libssh
– libssh2
– (N) libustream-mbedtls
– (Y) libustream-openssl
– (N) libustream-wolfssl
- LUCI -
– Collections -
= luci
= luci-ssl-openssl
- Exit
- save
如果是使用我的設定檔案,直接拷貝至openwrt目錄底下,並命名為.config就可以了
下載
- 編譯:
第一次編譯,會需要編譯Toolchain,所以需要幾個小時的時間
拷貝至SDCard
- 解壓縮編譯完成的檔案(bin/targets/bcm27xx/bcm2711/openwrt-bcm27xx-bcm2711-rpi-4-squashfs-factory.img.gz)
- 燒錄至SDCard
連接Raspberry pi和PC
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
插上Raspberry pi開機,即可在console操作Raspberry pi
這裡透過screen操作console
-
如果上述步驟成功,應該可已看到初始化過程
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
等到初始化結束(Console畫面停下來),按下Enter鍵,進入互動介面。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
設定網路
- 連接 ASUS USB-AC51(mt7602u)
將ASUS USB-AC51(mt7602u) network card插在Raspberry pi 4上USB 3的Port。
- 輸入ip address
看到wlan2, wlan3表示有抓到Raspberry pi內建網路卡和ASUS USB-AC51
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 初始化網路設定
- 設定無線網路
OpenWRT無線網路設定分成三個檔案:
/etc/config/network (網路設定)
/etc/config/wireless (無線網路設定)
/etc/config/firewall (防火牆設定)
在/etc/config/network新增DHCP設定
在/etc/config/wireless設定無線網路
簡易設定檔介紹:
option path 'platform/soc/fe300000.mmcnr/mmc_host/mmc1/mmc1:0001/mmc1:0001:1' => 表示為Raspberry pi內建無線網路卡
option path 'scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0' => 表示為ASUS USB-AC51(mt7602u)
option network 'lan' => 網路設定檔。(lan預設設定是靜態網路)
option network 'wan' => 網路設定檔。(剛剛設定的DHCP網路)
option mode 'ap' => AP mode (給別人連)
option mode 'sta' => Station mode (連接別人)
option ssid 'ssid' => 設定SSID
option encryption 'encryption' => 設定加密方式(EX: psk2, sae)
option key 'passwrd' => 設定密碼
- 設定防火牆
將wan設定中的option input 'REJECT' 改成 option input 'ACCEPT'。

該步驟是為了等等可以透過網路連入OpenWRT的GUI (Luci)
- 開啟wlan2, wlan3的radio
- 將網路設定應用
確定設定成功
- 輸入以下指令查看各個網卡狀態

- 利用其他裝置,確認是否有收到Raspberry pi發出來的ssid
進入OpenWRT GUI (Luci)
- 在同網域下的隨便一台裝置,開啟瀏覽器輸入剛剛看到wlan3的IP
會看到以下畫面,帳號密碼預設都是root

進入後可看到基本畫面

OpenWRT GUI裡的網路設定,雖然可以設定,但網卡不一定支援,錯誤的設定會導致網卡開不起來。例如 Raspberry pi 4 內建網路卡沒有支援802.11AC, 80mhz,但GUI可以設定。所以,要注意依照自己的網卡能力選擇適合的設定。
基本的OpenWRT建置先寫到這裡,下一篇會補上Docker和NordVPN (OpenVPN)的設定