Show Table of Contents
7.4. 使用命令行配置 802.1Q VLAN 标记
在 Red Hat Enterprise Linux 7 中,默认载入
8021q 模块。如有必要,可作为 root 运行以下命令确定已载入该模块:
~]# modprobe --first-time 8021q
modprobe: ERROR: could not insert '8021q': Module already in kernel
请运行以下命令显示该模块信息:
~]$ modinfo 8021q
更多命令选项请查看 modprobe(8) man page。
7.4.1. 使用 ifcfg 文件设置 802.1Q VLAN 标记
- 在
/etc/sysconfig/network-scripts/ifcfg-ethX中配置上级接口,其中 X 是与具体接口对应的唯一号码,如下:DEVICE=ethX TYPE=Ethernet BOOTPROTO=none ONBOOT=yes
- 在
/etc/sysconfig/network-scripts/目录中配置 VLAN 接口。配置文件名应为上级接口加上.字符再加上 VLAN ID 号码。例如:如果 VLAN ID 为 192,上级接口为 eth0,那么配置文件名应为ifcfg-eth0.192:DEVICE=ethX.192 BOOTPROTO=none ONBOOT=yes IPADDR=192.168.1.1 PREFIX=24 NETWORK=192.168.1.0 VLAN=yes
如果需要在同一接口 eth0 中配置第二个 VLAN,比如 VLAN ID 193,请添加名为eth0.193的新文件,文件中包含 VLAN 配置详情。 - 重启联网服务以便更改生效。请作为
root运行以下命令:~]#
systemctl restart network
7.4.2. 使用 ip 命令配置 802.1Q VLAN 标记
要在以太网接口 eth0 中创建名为 VLAN8、ID 为
8 的 802.1Q VLAN 接口,请作为 root 运行以下命令:
~]# ip link add link eth0 name eth0.8 type vlan id 8
请运行以下命令查看 VLAN:
~]$ ip -d link show eth0.8
4: eth0.8@eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT
link/ether 52:54:00:ce:5f:6c brd ff:ff:ff:ff:ff:ff promiscuity 0
vlan protocol 802.1Q id 8 <REORDER_HDR>
注:如果使用
0x 开头,则 ip 程序会将 VLAN ID 解析为十六进制数值,如果使用 0 开头,则将其解析为八进制数值。,如果要为 VLAN ID 分配十进制数值 22,则一定不能在开头添加任何 0。
请作为
root 运行以下命令移除 VLAN:
~]# ip link delete eth0.8
注意
系统关闭或重启后,会丢失使用 ip 命令在命令提示符后创建的 VLAN 接口。要将接口配置为在系统重启后仍保留,请使用
ifcfg 文件。详情请查看 第 7.4.1 节 “使用 ifcfg 文件设置 802.1Q VLAN 标记”。

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.