我们知道不同的vlan之间要相互的通行必须要借用第三层,也就是网络层,可以路由器也可以是三层的交换机,在这里我主要是介绍用三层交换机来实现不同的vlan之间的的通信。
实验拓扑图结构:
实验描述:
pc0和pc1分属于不同的vlan中,在实际的生活中,它们之间存在相互通信的可能。
pc0
pc1
实验命令:
对于二层交换机
Switch>
Switch>en
Switch#conf
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#hostname switch0
switch0#vlan database
% Warning: It is recommended to configure VLAN from config mode,
as VLAN database mode is being deprecated. Please consult user
documentation for configuring VTP/VLAN in config mode.
switch0(vlan)#vlan 2 name 2
VLAN 2 modified:
Name: 2
switch0(vlan)#vlan 3 name 3
VLAN 3 added:
Name: 3
switch0(vlan)#exit
APPLY completed.
Exiting....
switch0#conf
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
switch0(config)#int f0/1
switch0(config-if)#switchport mode access
switch0(config-if)#switchport access vlan 2
switch0(config-if)#exit
switch0(config)#int f0/3
switch0(config-if)#switchport mo
switch0(config-if)#switchport mode a
switch0(config-if)#switchport mode access
switch0(config-if)#switchport access vlan 3
switch0(config-if)#exit
switch0(config)#int f0/2
switch0(config-if)#switchport mode trunk
switch0(config-if)#end
%SYS-5-CONFIG_I: Configured from console by console
switch0#copy run star
Destination filename [startup-config]?
Building configuration...
[OK]
对于三层交换机
Switch>en
Switch#
Switch#conf
Configuring from terminal, memory, or network [terminal]?
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#ip routing
Switch(config)#interface f0/1
Switch(config-if)#switchport mode trunk
Switch(config-if)#end
%SYS-5-CONFIG_I: Configured from console by console
Switch#vlan database
% Warning: It is recommended to configure VLAN from config mode,
as VLAN database mode is being deprecated. Please consult user
documentation for configuring VTP/VLAN in config mode.
Switch(vlan)#vlan 2 name 2
VLAN 2 added:
Name: 2
Switch(vlan)#vlan 3 name 3
VLAN 3 added:
Name: 3
Switch(vlan)#exit
APPLY completed.
Exiting....
Switch#configure t
Switch#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#int
Switch(config)#interface vlan 2
%LINK-5-CHANGED: Interface Vlan2, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan2, changed state to upSwitch(config-if)#ip add
Switch(config-if)#ip address 192.168.1.1 255.255.255.0
Switch(config-if)#no sh
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int vlan 3
%LINK-5-CHANGED: Interface Vlan3, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan3, changed state to upSwitch(config-if)#ip add 192.168.2.1 255.255.255.0
Switch(config-if)#no sh
Switch(config-if)#end
%SYS-5-CONFIG_I: Configured from console by console
Switch#copy run star
Destination filename [startup-config]?
Building configuration...
[OK]
Switch#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route
Gateway of last resort is not set
C 192.168.1.0/24 is directly connected, Vlan2
C 192.168.2.0/24 is directly connected, Vlan3
测试
Pinging 192.168.2.2with 32 bytes of data:
Reply from 192.168.2.1: bytes=32 time=63ms TTL=255
Reply from 192.168.2.1: bytes=32 time=63ms TTL=255
Reply from 192.168.2.1: bytes=32 time=63ms TTL=255
Reply from 192.168.2.1: bytes=32 time=62ms TTL=255
Ping statistics for 192.168.2.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 62ms, Maximum = 63ms, Average = 62ms
实验成功。。。
需要注意的问题
本次试验中对于三层交换机首先要启动它的路由功能ip routing,否则实验是不能成功的,通过show ip route 我们可以看见路由表是建立起来了的。例外,三层设备必须建立多个vlan,虽然建立的vlan没有划分给具体的某些端口,但是我们在建立vlan虚拟地址的先决条件是必须先有这些vlan的存在的,不知大家注意没有,电脑的网关就是它所属vlan的虚拟ip。