openwrt 适合作为路由器系统使用,我们的路由器上一般会有多个网卡接口可供使用,其中一个作为 wan 口来连接外网,其他的可以作为 lan 口连接本地设备。

在默认的 network 配置文件中 lan 内只绑定了一个网卡,也就是只有一个接口可以连接到 lan 网络,下面介绍如何配置多个网卡通过桥接的方式共享 lan 网络。这样可以实现无论将设备接入哪一个网卡都可以连接到同一个 lan。

下面是一个标准的 /etc/config/network 文件内容:

config interface 'loopback'
    option device 'lo'
    option proto 'static'
    option ipaddr '127.0.0.1'
    option netmask '255.0.0.0'
 
config globals 'globals'
    option ula_prefix 'fd27:70fa:5c1d::/48'
 
config device 'lan_br'
    option name 'br-lan'
    option type 'bridge'
    list ports 'eth0'

config interface 'lan'
    option device 'br-lan'
    option proto 'static'
    option netmask '255.255.255.0'
    option ip6assign '60'
    option ipaddr '192.168.1.1'
        option gateway '27.168.1.1'
 
config interface 'wan'
    option device 'eth1'
    option proto 'dhcp'
 
onfig interface 'wan6'
    option device 'eth1'
    option proto 'dhcpv6'

可以看到默认配置了 lan 和 wan 网络,各自分配了一个网卡 eth0 和 eth1,其中 eth0 属于 lan 网络且设置了 static 静态地址,作为内网的网关,注意 lan 的 downlink 下级链接会自动开启 dhcp 服务,所以通过 eth0 连接的设备可以自动获取到地址。eth1 属于 wan 网络且配置为 dhcp 自动获取 ip 地址,作为连接外网使用。

config device 'lan_br' 定义了一个名称为 br-lan 的类型为桥接网络的设备,list ports 'eth0' 定义了这个设备下绑定的网卡,可以写多个 list ports 将多个网卡绑定到这个设备。如果只有一个设备,也可以直接在 interface 的 device 属性中写网卡名称即可,而不需单独定义一个 device 'lan_br'

interface 'lan' 定义了 lan 网络的设备名称为 br-lan 也就是上面定义那个设备,这样就相当于同时配置了 br-lan 下所有绑定的网卡,如果只有一个设备,也可以直接在 interface 的 device 属性中写网卡名称即可。还定义了 lan 网络为静态ip地址及网关地址,lan 网络下层网络会自动开启 DHCP。

interface 'wan' 定义了 wan 网络绑定 eth1 网卡设备,并开启 DHCP 自动获取 ip 地址。

关于 network 配置文件的结构参考:Network configuration /etc/config/network

示例

下面通过示例来介绍一些典型配置模式。

lan 网络的 Downstream 下层流量配置:

config device 'lan_br'
    option name 'br-lan'
    option type 'bridge'
    list ports 'eth0'

config interface 'lan'
    option device 'br-lan'
    option proto 'static'
    option netmask '255.255.255.0'
    option ip6assign '60'
    option ipaddr '192.168.1.1'
        option gateway '27.168.1.1'

lan 网络的下层链接会自动启用 dhcp 服务。

lan 网络配置多网卡桥接模式:

config device 'lan_br'
    option name 'br-lan'
    option type 'bridge'
    list ports 'eth0'
    list ports 'eth1'
    list ports 'eth2'
    list ports 'eth3'

config interface 'lan'
    option device 'br-lan'
    option proto 'static'
    option netmask '255.255.255.0'
    option ip6assign '60'
    option ipaddr '192.168.1.1'
        option gateway '27.168.1.1'
        option dns '1.1.1.1'

多 dns 配置:

config interface 'lan'
    option device 'br-lan'
    option proto 'static'
    option netmask '255.255.255.0'
    option ip6assign '60'
    option ipaddr '192.168.1.1'
        option gateway '27.168.1.1'
        list dns '192.168.1.1'
        list dns '27.168.1.1'

以上就是 openwrt 21 版本的网络配置文件基本格式。

标签:无

1 条评论

  1. [...]EOF保存以上内容到 listuserpackages.awk 文件中并执行即可列出用户自行安装包列表。下载升级固件x86 版本的 openwrt 的升级固件就是全新安装时候用的固件,直接在官网下载需要版本的 gz 压缩包即可。下载地址:https://downloads.openwrt.org/releases/我下载的是:generic-ext4-combined.img.gz,注意不要解压出[...]

你的评论