教你利⽤三层交换机实现VLAN间路由
本⽂档详细介绍利⽤三层交换机实现 VLAN 间路由,⽂内含长段代码可复制可往左滑,希望对⼤家有帮助!
实验背景
某企业有两个主要部门,技术部和销售部,分处于不同的办公室,为了安全和便于管理对两个部门的主机进⾏了 VLAN 的划分,技术部和销售部分处于不同的VLAN,先由于业务的需求需要销售部和技术部的主机能够相互访问,获得相应的资源,两个部门的交换机通过⼀台三层交换机进⾏了连接。
技术原理
三层交换机具备⽹络层的功能,实现 VLAN 相互访问的原理是:利⽤三层交换机的路由功能,通过识别数据包的 IP 地址,查路由表进⾏选路转发,三层交换机利⽤直连路由可以实现不同 VLAN 之间的相互访问。三层交换机给接⼝配置 IP 地址。采⽤ SVI(交换虚拟接⼝)的⽅式实现 VLAN 间互连。SVI 是指为交换机中的VLAN 创建虚拟接⼝,并且配置 IP 地址。Tag VLAN是基于交换机端⼝的⼀种类型,主要⽤于使交换机的相同Vlan内的主机之间可以直接访问,同时对于不同Vlan的主机进⾏隔离。trunk主要⽤在连接其它交换机,以便在线路上承载多个vlanWLAN是Wireless Local Area Network的简称,指应⽤
⽆线通信技术将计算机设备互联起来,构成可以互相通信和实现资源共享的⽹络体系。802.1x协议是WLAN第⼆代的认证技术,它是基于客户端-服务器(Client-Server)结构的访问控制和认证协议。它可以限制未经授权的⽤户/设备通过接⼊端⼝(Access Port)访问LAN/WLAN。DOT1Q是提供VLAN识别和服务质量(QoS)级别的IEEE标准。电⽓和电⼦⼯程师协会(IEEE,全称是Institute of Electrical and Electronics Engineers)是⼀个美国的电⼦技术与信息科学⼯程师的协会,是世界上最⼤的⾮营利性专业技术学会。ip routing 启⽤IP路由功能no ip routing 关闭IP路由功能
⼆层交换机与三层交换机区别
我们习惯说,在⼆层⽹络环境中相同vlan之间可以通信,不同vlan之间不可以通信,如果想通信必须借助三层设备,所以说三层交换机必须要做的事情是路由转发,但是⼆、三层交换机具体有什么区别呢?⼆层交换机⼯作于OSI模型的第2层(数据链路层),故⽽称为⼆层交换机。⼆层交换技术发展⽐较成熟,⼆层交换机属数据链路层设备,可以识别数据包中的MAC地址信息,根据MAC地址进⾏转发,并将这些MAC 地址与对应的端⼝记录在⾃⼰内部的⼀个地址表中。具体的⼯作流程如下:(1)当交换机从某个端⼝收到⼀个数据包,它先读取包头中的源MAC地址,这样它就知道源MAC地址的机器是连在哪个端⼝上的;(2)再去读取包头中的⽬的MAC地址,并在地址表中查相应的端⼝;(3)如表中有与这⽬的MAC地址对应的端⼝,把数据包直接复制到这端⼝上;(4)如表中不到相应的端⼝则把数据包⼴播到所有端⼝上,当⽬的机器对源机器回应时,交换机⼜可以学习⼀⽬的MAC地址与
哪个端⼝对应,在下次传送数据时就不再需要对所有端⼝进⾏⼴播了。不断的循环这个过程,对于全⽹的MAC地址信息都可以学习到,⼆层交换机就是这样建⽴和维护它⾃⼰的地址表。⼆层交换技术从⽹桥发展到VLAN(虚拟局域⽹),在局域⽹建设和改造中得到了⼴泛的应⽤。第⼆层交换技术是⼯作在OSI七层⽹络模型中的第⼆层,即数据链路层。它按照所接收到数据包的⽬的MAC地址来进⾏转发,对于⽹络层或者⾼层协议来说是透明的。它不处理⽹络层的IP地址,不处理⾼层协议的诸如TCP、UDP的端⼝地址,它只需要数据包的物理地址即MAC地址,数据交换是靠硬件来实现的,其速度相当快,这是⼆层交换的⼀个显著的优点。但是,它不能处理不同IP⼦⽹之间的数据交换。传统的路由器可以处理⼤量的跨越IP⼦⽹的数据包,但是它的转发效率⽐⼆层低,因此要想利⽤⼆层转发效率⾼这⼀优点,⼜要处理三层IP数据包,三层交换技术就诞⽣了。三层交换(也称多层交换技术,或IP交换技术)是相对于传统交换概念⽽提出的。众所周知,传统的交换技术是在OSI⽹络标准模型中的第⼆层——数据链路层进⾏操作的,⽽三层交换技术是在⽹络模型中的第三层实现了数据包的⾼速转发。简单地说,三层交换技术就是:⼆层交换技术+三层转发技术。三层交换机就是具有部分路由器功能的交换机。三层交换机的最重要⽬的是加快⼤型局域⽹内部的数据交换,所具有的路由功能也是为这⽬的服务的,能够做到⼀次路由,多次转发。对于数据包转发等规律性的过程由硬件⾼速实现,⽽像路由信息更新、路由表维护、路由计算、路由确定等功能,由软件实现。三层交换技术就是⼆层交换技术+三层转发技术。三层交换技术的出现,解决了局域⽹中⽹段划分之后,⽹段中⼦⽹必须依赖路由器进⾏管理的局⾯,解决了传统路由器低速、复杂所造成的⽹络瓶颈问题。
实验步骤
新建 packet tracer 拓扑图
(1)在⼆层交换机上配置 VLAN2、VLAN3,分别将端⼝ 2、端⼝ 3 划分给 VLAN2、
VLAN3。
(2)将⼆层交换机与三层交换机相连的端⼝ fa 0/1 都定义为 tag Vlan 模式。
(3)在三层交换机上配置 VLAN2、VLAN3,此时验证⼆层交换机 VLAN2、VLAN3
下的主机之间不能相互通信。
(4)设置三层交换机 VLAN 间的通信,创建 VLAN2,VLAN3 的虚接⼝,并配置
虚接⼝ VLAN2、VLAN3 的 IP 地址。
(5)查看三层交换机路由表。
(6)将⼆层交换机 VLAN2、VLAN3 下的主机默认⽹关分别设置为相应虚拟接⼝
的 IP 地址。
(7)验证⼆层交换机 VLAN2,VALN3 下的主机之间可以相互通信。
实验设备
Switch_2960 1 台;Swithc_3560 1 台;PC 3 台;直连线
PC1
IP: 192.168.1.2
Submark: 255.255.255.0
Gateway: 192.168.1.1
PC2
IP: 192.168.2.2
Submark: 255.255.255.0
Gateway: 192.168.2.1
PC3
IP: 192.168.1.3
Submark: 255.255.255.0
Gateway: 192.168.1.1
S2960
en
conf t
vlan 2
exit
vlan 3
exit
int fa 0/2
switchport access vlan 2
int fa 0/3
switchport access vlan 3
int fa 0/1
switchport mode trunk
end
show vlan
S3560
en
conf t
vlan 2
exit
vlan 3
exit
int fa 0/1
switchport trunk encapsulation dot1q #将0/1端⼝封装成dot1q协议
switchport mode trunk
exit
int fa 0/2
switchport access vlan 2
exit
interface vlan 2
ip address 192.168.1.1 255.255.255.0
no shutdown
exit
interface vlan 3
ip address 192.168.2.1 255.255.255.0
no shutdown
exit
ip routing
一只母猪居然生下8个男婴儿exit
show ip route
路由器用户名是什么show vlan
PC3 Ping PC1
Ping 192.168.1.2
PC3 Ping PC2
Ping 192.168.1.3
<;以上代码可复制粘贴,可往左滑>
实战演练
⼆层交换机
Switch>en
Switch#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#vlan 2
Switch(config-vlan)#exit
Switch(config)#vlan 3
Switch(config-vlan)#int fa 0/2
Switch(config-if)#switchport access vlan 2
Switch(config-if)#int fa 0/3
Switch(config-if)#switchport access vlan 3
Switch(config-if)#int fa 0/1
Switch(config-if)#switchport mode trunk
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to down
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to up Switch(config-if)#end
Switch#
%SYS-5-CONFIG_I: Configured from console by console
Switch#show vlan
VLAN Name Status Ports
---- -------------------------------- --------- -------------------------------
1 default active Fa0/4, Fa0/5, Fa0/6, Fa0/7
Fa0/8, Fa0/9, Fa0/10, Fa0/11
Fa0/12, Fa0/13, Fa0/14, Fa0/15
Fa0/16, Fa0/17, Fa0/18, Fa0/19
Fa0/20, Fa0/21, Fa0/22, Fa0/23
Fa0/24, Gig1/1, Gig1/2
2 VLAN0002 active Fa0/2
3 VLAN0003 active Fa0/3
1002 fddi-default act/unsup
1003 token-ring-default act/unsup
1004 fddinet-default act/unsup
1005 trnet-default act/unsup
怎么在excel中画斜线VLAN Type SAID MTU Parent RingNo BridgeNo Stp BrdgMode Trans1 Trans2
---- ----- ---------- ----- ------ ------ -------- ---- -------- ------ ------
1 enet 100001 1500 - - - - - 0 0
2 enet 100002 1500 - - - - - 0 0
3 enet 100003 1500 - - - - - 0 0
1002 fddi 101002 1500 - - - - - 0 0
--More--
三层交换机
Switch>en
Switch#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#vlan 2
Switch(config-vlan)#exit
Switch(config)#vlan 3
Switch(config-vlan)#exit
Switch(config)#int fa 0/1
Switch(config-if)#switchport trunk encapsulation dot1q
Switch(config-if)#switchport mode trunk
Switch(config-if)#switchport trunk allowed vlan 2,3
Switch(config-if)#exit
Switch(config)#int fa 0/2
Switch(config-if)#switchport access vlan 2
Switch(config-if)#exit
Switch(config)#int vlan 2
%LINK-5-CHANGED: Interface Vlan2, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan2, changed state to up
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 3
%LINK-5-CHANGED: Interface Vlan3, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan3, changed state to up
Switch(config-if)#ip address 192.168.2.1 255.255.255.0
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#ip routing
Switch(config)#exit
Switch#
%SYS-5-CONFIG_I: Configured from console by console
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
Switch#show vlan
VLAN Name Status Ports
---- -------------------------------- --------- -------------------------------
1 default active Fa0/3, Fa0/4, Fa0/5, Fa0/6
Fa0/7, Fa0/8, Fa0/9, Fa0/10
Fa0/11, Fa0/12, Fa0/13, Fa0/14
Fa0/15, Fa0/16, Fa0/17, Fa0/18
马嘉祺个人资料Fa0/19, Fa0/20, Fa0/21, Fa0/22
Fa0/23, Fa0/24, Gig0/1, Gig0/2
2 VLAN0002 active Fa0/2
3 VLAN0003 active
1002 fddi-default act/unsup
1003 token-ring-default act/unsup
1004 fddinet-default act/unsup
1005 trnet-default act/unsup
VLAN Type SAID MTU Parent RingNo BridgeNo Stp BrdgMode Trans1 Trans2 ---- ----- ---------- ----- ------ ------ -------- ---- -------- ------ ------
1 enet 100001 1500 - - - - - 0 0
2 enet 100002 1500 - - - - - 0 0
3 enet 100003 1500 - - - - - 0 0
1002 fddi 101002 1500 - - - - - 0 0
--More--
<;以上代码可复制粘贴,可往左滑>
测试
PC1 分别 ping PC2 和 PC3
PC>ipconfig
: 192.168.1.2
: 255.255.255.0
: 192.168.1.1
PC>ping 192.168.2.2
Pinging 192.168.2.2 with 32 bytes of data:
Request timed out.
Reply from 192.168.2.2: bytes=32 time=25ms TTL=127
Reply from 192.168.2.2: bytes=32 time=20ms TTL=127
Reply from 192.168.2.2: bytes=32 time=20ms TTL=127
Ping statistics for 192.168.2.2:
Packets: Sent = 4, Received = 3, Lost = 1 (25% loss),
Approximate round trip times in milli-seconds:
Minimum = 20ms, Maximum = 25ms, Average = 21ms
PC>ping 192.168.2.2
Pinging 192.168.2.2 with 32 bytes of data:
Reply from 192.168.2.2: bytes=32 time=18ms TTL=127
Reply from 192.168.2.2: bytes=32 time=23ms TTL=127
Reply from 192.168.2.2: bytes=32 time=19ms TTL=127
Reply from 192.168.2.2: bytes=32 time=22ms TTL=127
Ping statistics for 192.168.2.2:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 18ms, Maximum = 23ms, Average = 20ms
PC>ping 192.168.1.3
Pinging 192.168.1.3 with 32 bytes of data:
Reply from 192.168.1.3: bytes=32 time=15ms TTL=128
Reply from 192.168.1.3: bytes=32 time=19ms TTL=128
Reply from 192.168.1.3: bytes=32 time=15ms TTL=128
Reply from 192.168.1.3: bytes=32 time=14ms TTL=128
Ping statistics for 192.168.1.3:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 14ms, Maximum = 19ms, Average = 15ms
PC2 分别 ping PC1 和 PC3
PC>ipconfig
: 192.168.2.2
: 255.255.255.0
: 192.168.2.1
PC>ping 192.168.1.2
Pinging 192.168.1.2 with 32 bytes of data:
Reply from 192.168.1.2: bytes=32 time=24ms TTL=127 Reply from 192.168.1.2: bytes=32 time=23ms TTL=127 Reply from 192.168.1.2: bytes=32 time=24ms TTL=127 Reply from 192.168.1.2: bytes=32 time=20ms TTL=127
Ping statistics for 192.168.1.2:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 20ms, Maximum = 24ms, Average = 22ms
PC>ping 192.168.1.3
Pinging 192.168.1.3 with 32 bytes of data:
Reply from 192.168.1.3: bytes=32 time=13ms TTL=127 Reply from 192.168.1.3: bytes=32 time=17ms TTL=127 Reply from 192.168.1.3: bytes=32 time=19ms TTL=127 Reply from 192.168.1.3: bytes=32 time=19ms TTL=127 Ping statistics for 192.168.1.3:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 13ms, Maximum = 19ms, Average = 17ms PC3 分别 ping PC1 和 PC2
PC>ipconfig利菁
: 192.168.1.3
: 255.255.255.0
: 192.168.1.1
PC>ping 192.168.1.2
Pinging 192.168.1.2 with 32 bytes of data:
Reply from 192.168.1.2: bytes=32 time=14ms TTL=128 Reply from 192.168.1.2: bytes=32 time=17ms TTL=128 Reply from 192.168.1.2: bytes=32 time=17ms TTL=128 Reply from 192.168.1.2: bytes=32 time=16ms TTL=128 Ping statistics for 192.168.1.2:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 14ms, Maximum = 17ms, Average = 16ms PC>ping 192.168.2.2
Pinging 192.168.2.2 with 32 bytes of data:
Request timed out.
Reply from 192.168.2.2: bytes=32 time=17ms TTL=127 Reply from 192.168.2.2: bytes=32 time=16ms TTL=127 Reply from 192.168.2.2: bytes=32 time=18ms TTL=127 Ping statistics for 192.168.2.2:
Packets: Sent = 4, Received = 3, Lost = 1 (25% loss), Approximate round trip times in milli-seconds: Minimum = 16ms, Maximum = 18ms, Average = 17ms PC>ping 192.168.2.2
Pinging 192.168.2.2 with 32 bytes of data:
Reply from 192.168.2.2: bytes=32 time=15ms TTL=127 Reply from 192.168.2.2: bytes=32 time=17ms TTL=127 Reply from 192.168.2.2: bytes=32 time=14ms TTL=127 Reply from 192.168.2.2: bytes=32 time=11ms TTL=127 Ping statistics for 192.168.2.2:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 11ms, Maximum = 17ms, Average = 14ms <;以上代码可复制粘贴,可往左滑>