原理
在交换网络中,通过VLAN对一个物理网络进行了逻辑划分,不同的VLAN之间是无法直接访问的,必须通过三层的路由设备进行连接。一般利用路由器或三层交换机来实现不同VLAN间的相互访问。三层交换机和路由器具备网络层的功能,能够根据数据的IP包头信息,进行选路和转发,从而实现不同网段之间的访问。
三层交换机实现VLAN互访的原理是,利用三层交换机的路由功能,通过识别数据包的IP地址,查找路由表进行选路转发。三层交换机利用直连路由可以实现不同VLAN之间的互相访问。三层交换机给接口配置IP地址,采用SVI(交换机虚拟接口)的方式实现VLAN间互连。SVI是指为交换机中的VLAN创建虚拟接口,并且配置IP地址。
网络拓扑结构如下图,主要是利用三层交换机的路由功能来实现跨vlan通信

开始配置
电脑配置
给每台天脑配置IP地址和网关,这里就不放步骤了,图里有说明。
二层交换机配置
配置二层交换机R0
- 划分Vlan
1
2
3
4
5Switch>en
Switchconf t
Switch(config)#vlan 10
Switch(config-vlan)#vlan 20
Switch(config-vlan)#exit - 划分端口到vlan里
1
2
3
4
5
6Switch(config)#int range f0/1-3
Switch(config-range)#switchport accesss vlan 10
Switch(config-range)#exit
Switch(config)#int f0/4
Switch(config-if)switchport access vlan 20
Switch(config-if)exit - 把与三层交换机连接的端口设为trunk模式
1
2
3
4Switch(config)#int f0/24
Switch(config-if)#switchport mode trunk
Switch(config-if)#no shutdown
Switch(config-if)#exit - 保存配置
1
2Switch(config)#exit
Switch#write
配置二层交换机R1
- 划分vlan
1
2
3
4Switch>en
Switch#conf t
Switch(config)#vlan 30
Switch(config-vlan)#exit - 划分端口到vlan里
1
2
3Switch(config)#int range f0/1-3
Switch(config-range)#switchport access vlan 30
Switch(config-range)#exit - 把与三层交换机连接的端口改为trunk模式
1
2
3
4Switch(config)#int f0/24
Switch(config-if)#switchport mode trunk
Switch(config-if)#no shutdown
Switch(config-if)#exit - 保存配置
1
2Switch(config)#exit
Switch#write
三层交换机配置
配置三层交换机R0
- 划分vlan
1
2
3
4
5
6Switch>en
Switch#conf t
Switch(config)#vlan 10
Switch(config-vlan)#vlan 20
Switch(config-vlan)#vlan 30
Switch(config-vlan)#exit - 给vlan分配IP地址
1
2
3
4
5
6
7
8
9
10
11
12Switch(config)#int vlan 10
Switch(config-if)#ip address 192.168.1.1 255.255.255.0
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int vlan 20
Switch(config-if)#ip address 192.168.2.1 255.255.255.0
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int vlan 30
Switch(config-if)#ip address 192.168.3.1 255.255.255.0
Switch(config-if)#no shurdown
Switch(config-if)#exit - 设置连接二层交换机的端口为trunk模式并封装为802.1q
1
2
3
4
5
6
7
8
9
10Switch(config)#int f0/1
Switch(config-if)#switchport trunk encapsulation dot1q
Switch(config-if)#switchport mode trunk
Switch(config-if)#no shutdown
Switch(confit-if)#exit
Switch(config)#int f0/2
Switch(config-if)#switchport trunk encapsulation dot1q
Switch(config-if)#switchport mode trunk
Switch(config-if)#no shutdown
Switch(config-if)#exit - 保存配置
1
2Switch(config)#exit
Switch#write
测试
测试一下各主机之间跨网段和vlan能不能ping通,如果如下图一样ping通了,那就说明配置成功
