VLAN
![]() 此信息已过时,不适用于使用 ifupdown-ng 的系统。安装 vlan 软件包可能会阻止您的网络被正确配置(讨论) |
本文介绍如何将网络接口配置为 IEEE 802.1q VLAN 干线。
安装
首先,安装 vlan 软件包。这将为您提供在 /etc/network/interfaces 文件中对 VLAN 的支持。
apk add vlan

警告: 安装 vlan 软件包可能会导致 ifupdown-ng 被 busybox-ifupdown 替换。如果您使用 ifupdown-ng 特定的配置,这可能会阻止您的网络被正确配置。
配置
编辑 /etc/network/interfaces 文件
auto eth0.8 iface eth0.8 inet static address 192.168.0.2 netmask 255.255.255.0 gateway 192.168.0.1 vlan-raw-device eth0 vlan_id 8
安装 vlan 软件包后,ifup
将在 eth0.8 中找到尾部的 .8,并将创建一个 VLAN 接口,vid 为 8,通过 eth0。
或者使用 vlan8 通过 eth0
auto vlan8 iface vlan8 inet static address 192.168.0.2 netmask 255.255.255.0 gateway 192.168.0.1 vlan-raw-device eth0
上述示例中使用了静态 IP 地址,但也可以使用 DHCP。
桥接与 VLAN 关联的示例,通过绑定在不同 VLAN 上使用不同的 MTU
这可以作为一些更复杂的网络配置示例。特别是,这对于连接到专用存储 VLAN 的 hypervisor 非常适用。通过仅删除不适用的部分,可以实现更简单的实现。
auto lo iface lo inet loopback auto bond0 iface bond0 inet manual bond_slaves eth0 eth1 bond_mode 802.3ad bond_miimon 100 bond_xmit_hash_policy layer2+3 post-up ip link set dev bondi0 mtu 9000 iface bond0.1 auto br1 iface br1 address 192.168.1.196/24 gateway 192.168.1.1 bridge_ports bond0.1 bridge_stp off bridge_fd 0.0 post-up ip link set dev bond0.1 mtu 1500 iface bond0.10 inet manual auto br10 iface br10 inet static address 192.168.10.1/24 bridge_ports bond0.10 bridge_stp off bridge_fd 0.0
同一适配器上两个接口的示例。一个使用 VLAN,一个不使用
由于 Linux 不允许多个默认网关,我们需要使用 iproute2 使用第二个路由表。
apk add iproute2
然后我们将向配置文件添加两个新的路由表。每个网络一个。
echo "1 rt1" >> /etc/iproute2/rt_tables;echo "2 rt2" >> /etc/iproute2/rt_tables;
现在我们需要编辑 /etc/network/interfaces
auto lo iface lo inet loopback # the native interface without a VLAN (also called untagged) auto eth0 iface eth0 address 192.168.1.100/24 gateway 192.168.1.1 post-up ip route add 192.168.1.0/24 dev eth0 src 192.168.1.100 table rt1 post-up ip route add default via 192.168.1.1 dev eth0 table rt1 # the actual gateway for this interface post-up ip rule add from 192.168.1.100/32 table rt1 post-up ip rule add to 192.168.1.100/32 table rt1 # second interface with the vlan tag 5 auto eth0.5 iface eth0.5 address 192.168.5.100/24 post-up ip route add 192.168.5.0/24 dev eth0.5 src 192.168.5.100 table rt2 post-up ip route add default via 192.168.5.1 dev eth0.5 table rt2 # the actual gateway for this interface post-up ip rule add from 192.168.5.100/32 table rt2 post-up ip rule add to 192.168.5.100/32 table rt2
请注意,如果您想以这种方式添加第三个接口,则必须添加另一个路由表。