树莓派软路由

树莓派 4B docker openwrt 软路由快速配置

安装前提

  • Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

我使用的系统环境是

dawn@raspberrypi:/opt/openwrt $ uname -a
Linux raspberrypi 6.1.0-rpi4-rpi-v8 #1 SMP PREEMPT Debian 1:6.1.54-1+rpt2 (2023-10-05) aarch64 GNU/Linux

dawn@raspberrypi:/opt/openwrt $ cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

开启混杂模式

sudo ip link set eth0 promisc on

此命令将 Raspberry Pi 上的以太网接口 eth0 设置为混杂模式。 在混杂模式下,网络接口可以捕获其所连接的网段上的所有网络流量。

创建网络

此网段需要和路由器一致,例如我家的光猫 IP 为 192.168.1.1

sudo docker network create -d macvlan --subnet=192.168.1.0/24 --gateway=192.168.1.1 -o parent=eth0 openwrt-wan

此命令使用 macvlan 驱动程序创建一个名为 openwrt-wan 的 Docker 网络。 它通过 Raspberry Pi 上的 eth0 有效地将 Docker 容器桥接到物理网络。

--subnet=192.168.1.0/24 指定 Docker 网络的子网。

--gateway=192.168.1.1 指定 Docker 网络的网关。

-o parent=eth0 指定 Raspberry Pi 上的物理接口 eth0 应作为此 Docker 网络的父接口。

创建目录和文件

sudo mkdir /opt/openwrt && cd /opt/openwrt && sudo vi network 文件,内容如下

option gateway 和 option dns 必须填写路由器的 IP,若树莓派获得的 IP 为 192.168.1.9【下面的 ipaddr 为容器的固定 IP,改为你自己想要的地址,用于访问 openwrt 】,路由器 IP 为 192.168.1.1

config interface 'loopback'
        option ifname 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config globals 'globals'

config interface 'lan'
        option type 'bridge'
        option ifname 'eth0'
        option proto 'static'
        option netmask '255.255.255.0'
        option ip6assign '60'
        option ipaddr '192.168.1.100'
        option gateway '192.168.1.1'
        option dns '192.168.1.1'

config interface 'vpn0'
        option ifname 'tun0'
        option proto 'none'

创建 docker-compose.yaml 文件

version: '3'

services:
  openwrt:
    image: sulinggg/openwrt:latest
    container_name: openwrt
    network_mode: openwrt-wan
    privileged: true
    restart: always
    volumes:
      - ./network:/etc/config/network
    command: /sbin/init

配置完成后目录如下:

dawn@raspberrypi:/opt/openwrt $ ls
docker-compose.yaml  network

启动 openwrt

docker compose up -d 启动容器

重启网络配置生效

docker exec openwrt /etc/init.d/network restart

访问网页

浏览器打开 192.168.1.100 访问 openwrt

默认用户:root

密码:password