VLAN 配置及 VLAN 间通信

一、VLAN 概念简述

1. 什么是 VLAN?

VLAN(Virtual Local Area Network,虚拟局域网) 是在二层交换机上对端口进行逻辑划分,使得物理上属于同一广播域的设备被分隔成多个逻辑子网,从而实现不同组之间的隔离。

  • 每个 VLAN 是一个独立的广播域
  • 不同 VLAN 默认不能互通
  • VLAN 提高了网络的安全性和可管理性

二、基本 VLAN 配置

1. 拓扑假设

三台 PC 分别连接到交换机的不同端口,我们将:

  • PC1 → VLAN 10
  • PC2 → VLAN 20
  • PC3 → VLAN 10

2. 配置步骤(以 Cisco 交换机为例)

Step 1:进入 VLAN 配置模式并创建 VLAN

1
2
3
4
5
6
7
Switch(config)# vlan 10
Switch(config-vlan)# name Sales
Switch(config-vlan)# exit

Switch(config)# vlan 20
Switch(config-vlan)# name Finance
Switch(config-vlan)# exit

Step 2:将端口划分进对应 VLAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Switch(config)# interface fastEthernet 0/1
Switch(config-if)# switchport mode access
Switch(config-if)# switchport access vlan 10
Switch(config-if)# exit

Switch(config)# interface fastEthernet 0/2
Switch(config-if)# switchport mode access
Switch(config-if)# switchport access vlan 20
Switch(config-if)# exit

Switch(config)# interface fastEthernet 0/3
Switch(config-if)# switchport mode access
Switch(config-if)# switchport access vlan 10
Switch(config-if)# exit

三、VLAN 间通信(Inter-VLAN Routing)

由于不同 VLAN 默认不能通信,我们需要使用 三层设备(如路由器或三层交换机)来实现 VLAN 间通信,这称为 Inter-VLAN Routing

方法一:Router-on-a-Stick(单臂路由)

使用一台路由器的单个物理接口划分多个子接口,分别绑定不同的 VLAN。

配置示例:

交换机端连接路由器的端口设置为 trunk:

1
2
Switch(config)# interface fa0/24
Switch(config-if)# switchport mode trunk

路由器端配置子接口:

1
2
3
4
5
6
7
Router(config)# interface fastEthernet 0/0.10
Router(config-subif)# encapsulation dot1Q 10
Router(config-subif)# ip address 192.168.10.1 255.255.255.0

Router(config)# interface fastEthernet 0/0.20
Router(config-subif)# encapsulation dot1Q 20
Router(config-subif)# ip address 192.168.20.1 255.255.255.0

方法二:三层交换机实现

直接在三层交换机上配置 VLAN 接口(SVI),更高效。

配置示例:

1
2
3
4
5
6
7
8
9
Switch(config)# interface vlan 10
Switch(config-if)# ip address 192.168.10.1 255.255.255.0
Switch(config-if)# no shutdown

Switch(config)# interface vlan 20
Switch(config-if)# ip address 192.168.20.1 255.255.255.0
Switch(config-if)# no shutdown

Switch(config)# ip routing # 启用三层交换

四、常用命令总结

命令作用
vlan [编号]创建 VLAN
name [名称]命名 VLAN
switchport mode access设置为访问端口
switchport access vlan [编号]指定端口的 VLAN
switchport mode trunk设置为干道端口
interface vlan [编号]配置 SVI 接口
ip routing启用三层交换机的路由功能

五、排错技巧

  • 使用 show vlan brief 查看 VLAN 分配情况
  • 使用 show interfaces trunk 查看 trunk 状态
  • 使用 ping 测试不同 VLAN 主机互通情况
  • 确保子接口、SVI 状态为 up

Native VLAN 与 VTP

一、Native VLAN 是什么?

1. 定义

Native VLAN 是在 IEEE 802.1Q Trunk 链路上传输未打标签(untagged)帧时,默认归属的 VLAN。

2. 工作机制

在 trunk 接口上传输多个 VLAN 流量时,每个 VLAN 的帧都会被打上一个 802.1Q 的标签(tag),以标识它属于哪个 VLAN。但部分帧(例如部分老旧设备或默认配置的系统)不会打上 tag,这时这些帧将自动归属为 Native VLAN。

3. 配置示例(Cisco IOS)

1
2
3
4
Switch(config)# interface GigabitEthernet0/1
Switch(config-if)# switchport trunk encapsulation dot1q
Switch(config-if)# switchport mode trunk
Switch(config-if)# switchport trunk native vlan 99

4. 安全建议

  • 默认的 Native VLAN 是 VLAN 1,这很容易被利用进行 VLAN Hopping 攻击。
  • 建议设置一个独立的、不用于正常业务通信的 VLAN 作为 Native VLAN。
  • 在所有 access 接口禁止 Native VLAN。

二、VTP(VLAN Trunking Protocol) 是什么?

VTP(VLAN Trunking Protocol)是 Cisco 网络设备之间同步 VLAN 配置信息的协议。在配置 VTP 时,首先需要决定使用 VTP 的模式:Server、Client 或 Transparent。以下是 VTP 配置的详细步骤:

1. VTP 配置概述

  • VTP Server 模式:这是默认模式,可以创建、修改和删除 VLAN,并将这些变更同步到其他交换机。
  • VTP Client 模式:不能创建、修改或删除 VLAN,只能接收来自 VTP Server 的 VLAN 配置信息。
  • VTP Transparent 模式:不会参与 VLAN 配置的同步,但可以本地管理 VLAN,并转发 VTP 数据包。

2. 配置步骤

配置 VTP Server 模式

在 VTP Server 模式下,交换机会充当 VLAN 配置的“主控”,所有 VLAN 配置都会从该交换机传播到 VTP 域中的其他交换机。

1
2
3
4
Switch(config)# vtp domain <domain-name>           # 设置 VTP 域名
Switch(config)# vtp mode server # 设置为 VTP Server 模式
Switch(config)# vtp password <password> # 设置 VTP 密码
Switch(config)# vtp version 2 # 可选:设置使用的 VTP 版本(默认为 v1)

配置 VTP Client 模式

在 VTP Client 模式下,交换机会接收来自 VTP Server 的 VLAN 配置信息,但不能对 VLAN 进行任何更改。

1
2
3
4
Switch(config)# vtp domain <domain-name>           # 设置 VTP 域名(必须与 Server 一致)
Switch(config)# vtp mode client # 设置为 VTP Client 模式
Switch(config)# vtp password <password> # 设置 VTP 密码(必须与 Server 一致)
Switch(config)# vtp version 2 # 可选:设置使用的 VTP 版本(默认为 v1)

配置 VTP Transparent 模式

VTP Transparent 模式下,交换机不会同步 VLAN 配置信息,但允许本地 VLAN 配置并转发 VTP 数据包。

1
2
3
4
Switch(config)# vtp domain <domain-name>           # 设置 VTP 域名(必须与 Server 一致)
Switch(config)# vtp mode transparent # 设置为 VTP Transparent 模式
Switch(config)# vtp password <password> # 设置 VTP 密码(必须与 Server 一致)
Switch(config)# vtp version 2 # 可选:设置使用的 VTP 版本(默认为 v1)

3. VTP 配置验证

配置完成后,可以使用以下命令验证 VTP 的状态和配置:

1
2
3
4
5
6
7
8
# 查看 VTP 状态,确认域名、模式等信息
Switch# show vtp status

# 查看 VTP 所有 VLAN 配置
Switch# show vlan brief

# 查看 trunk 端口信息,确认 trunk 链路上的 VLAN 状况
Switch# show interfaces trunk

4. VTP 的注意事项

  • VTP 域名一致性:在同一 VTP 域中的所有交换机需要使用相同的域名,才能进行信息同步。
  • VTP 密码一致性:在同一 VTP 域中的所有交换机需要使用相同的密码。
  • VTP 版本:在 VTP 域中的交换机可以选择不同的 VTP 版本(v1、v2 或 v3),但必须统一,否则可能导致配置错误。
  • VTP Transparent 模式:在透明模式下,交换机不会发送 VLAN 配置更新,但它会转发其他交换机的 VTP 数据包,因此透明模式适用于 VLAN 配置的分发而不参与实际的 VLAN 修改。

5. VTP 版本

VTP 有 3 个版本:

  • VTP v1:默认版本,支持最多 1005 个 VLAN(包括 1-1005 的 VLAN 范围)。
  • VTP v2:改进了支持 Token Ring VLAN 和 VTP 认证。
  • VTP v3:增加了对扩展 VLAN、私有 VLAN 和增强的安全功能支持。

在 VTP v3 中,提供了对 VLAN 配置的更多控制,并提供对增强安全性的支持。使用 vtp version 3 可以切换到 VTP v3。


6. 示例:完整的 VTP 配置步骤

假设我们有两台交换机,交换机 A 配置为 VTP Server,交换机 B 配置为 VTP Client。

交换机 A(VTP Server)配置

1
2
3
4
SwitchA(config)# vtp domain mynetwork           # 设置 VTP 域名
SwitchA(config)# vtp mode server # 设置为 Server 模式
SwitchA(config)# vtp password cisco123 # 设置 VTP 密码
SwitchA(config)# vtp version 2 # 设置 VTP 版本

交换机 B(VTP Client)配置

1
2
3
4
SwitchB(config)# vtp domain mynetwork          # 设置 VTP 域名
SwitchB(config)# vtp mode client # 设置为 Client 模式
SwitchB(config)# vtp password cisco123 # 设置 VTP 密码
SwitchB(config)# vtp version 2 # 设置 VTP 版本

配置完成后,交换机 A 创建的 VLAN 将会自动同步到交换机 B,无需手动配置 VLAN。


7. 总结

  • VTP 简化了 VLAN 配置的管理,尤其是在大型网络中。
  • 根据不同的需求选择 VTP 的工作模式:Server、Client 或 Transparent。
  • 配置时,务必确保 VTP 域名、密码和版本一致,以保证交换机之间的顺利同步。

三、Native VLAN 与 VTP 的区别和联系

项目Native VLANVTP
定义trunk 链路中默认接收 untagged 帧的 VLANVLAN 配置自动同步协议
工作机制不打 tag 的帧自动归入 native VLANServer 自动分发 VLAN 信息,Client 接收
安全风险VLAN Hopping 攻击错误同步可能删除 VLAN
使用场景trunk 链路通信场景多交换机自动化部署 VLAN 配置
是否必须使用否(可以用 Transparent 模式或静态配置)

四、运维实践建议

  • 将 Native VLAN 设置为不参与任何业务的 VLAN,并在 trunk 接口指定它。
  • 禁止 access 接口接受 Native VLAN 流量。
  • 使用 VTP 时谨慎设置 Server,重要交换机推荐使用 Transparent 模式。
  • 限制 trunk 接口允许通过的 VLAN,避免广播风暴和误配置传播。

五、常用命令回顾

1
2
3
4
5
6
7
8
9
10
11
12
# 查看 trunk 接口信息
Switch# show interfaces trunk

# 查看当前 native vlan 配置
Switch# show running-config interface GigabitEthernet0/1

# 查看 VTP 状态
Switch# show vtp status

# 设置 VTP 模式和域
Switch(config)# vtp mode transparent
Switch(config)# vtp domain mydomain