不同vlan之间如何ping通_Cisco Packet Tracer配置三层路由器实现不同vlan之间互连

时间:2024-11-19 22:56:00

0、导言:

题图中是笔者自己在Cisco Packet Tracer上模拟的一个简单局域网,包括3个vlan,使用了3台路由器(SW)以及一个三层路由器(3LSW)。花了一些时间学习ccna就开始自己上手,算是一点简单实践。

下文将介绍简易的vlan路由网络搭建过程,包含4台主机,一台路由器以及一台三层路由器。

拓扑结构如下:

1、配置VLAN

各台主机按照拓扑图中信息设置网关以及本机IP地址及掩码。路由器Switch0共有24个fastEthernet接口,将其中0/1、0/2、0/3、0/4使用直连线分别接到主机PC0、PC1、PC2、PC3。

进入Switch0 CLI界面,开始设置:

  1. # 1、进入config模式
  2. Switch>en
  3. Switch#config
  4. Configuring from terminal, memory, or network [terminal]?
  5. Enter configuration commands, one per line. End with CNTL/Z.
  6. # 2、创建vlan,创建10以及20两个
  7. Switch(config)#vlan 10
  8. Switch(config-vlan)#exit
  9. Switch(config)#vlan 20
  10. Switch(config-vlan)#exit
  11. # 3、将fastEthernet端口配置为相应的vlan接入口
  12. Switch(config)#interface fastEthernet 0/1
  13. Switch(config-if)#switchport access vlan 10
  14. Switch(config-if)#exit
  15. Switch(config)#interface fastEthernet 0/3
  16. Switch(config-if)#switchport access vlan 10
  17. Switch(config-if)#exit
  18. Switch(config)#interface fastEthernet 0/2
  19. Switch(config-if)#switchport access vlan 20
  20. Switch(config-if)#exit
  21. Switch(config)#interface fastEthernet 0/4
  22. Switch(config-if)#switchport access vlan 20
  23. Switch(config)#^Z

经过以上步骤,vlan设置完成,可以通过show vlan查看各个vlan对应的接口:

  1. Switch#show vlan
  2. VLAN Name Status Ports
  3. ---- -------------------------------- --------- -------------------------------
  4. 1 default active Fa0/5, Fa0/6, Fa0/7, Fa0/8
  5. Fa0/9, Fa0/10, Fa0/11, Fa0/12
  6. Fa0/13, Fa0/14, Fa0/15, Fa0/16
  7. Fa0/17, Fa0/18, Fa0/19, Fa0/20
  8. Fa0/21, Fa0/22, Fa0/23, Fa0/24
  9. Gig0/1, Gig0/2
  10. 10 VLAN0010 active Fa0/1, Fa0/3
  11. 20 VLAN0020 active Fa0/2, Fa0/4
  12. 1002 fddi-default active
  13. 1003 token-ring-default active
  14. 1004 fddinet-default active
  15. 1005 trnet-default active
  16. ...

验证:

PC0 ping PC2

  1. C:>ping 192.168.10.2
  2. Pinging 192.168.10.2 with 32 bytes of data:
  3. Reply from 192.168.10.2: bytes=32 time=1ms TTL=128
  4. Reply from 192.168.10.2: bytes=32 time<1ms TTL=128
  5. Reply from 192.168.10.2: bytes=32 time<1ms TTL=128
  6. Reply from 192.168.10.2: bytes=32 time<1ms TTL=128
  7. Ping statistics for 192.168.10.2:
  8. Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
  9. Approximate round trip times in milli-seconds:
  10. Minimum = 0ms, Maximum = 1ms, Average = 0ms

PC0 ping PC1

  1. C:>ping 192.168.20.1
  2. Pinging 192.168.20.1 with 32 bytes of data:
  3. Request timed out.
  4. Request timed out.
  5. Request timed out.
  6. Request timed out.
  7. Ping statistics for 192.168.20.1:
  8. Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

vlan划分完毕。

2、设置三层交换机路由,实现不同VLAN互连

三层交换机与交换机通过交叉线连接,三层交换机端口为fastEthernet 0/1,交换机端口为fastEthernet 0/24。

首先要对交换机之间接口配置为trunk模式,对于交换机Switch0配置如下:

  1. Switch>en
  2. Switch#config
  3. Configuring from terminal, memory, or network [terminal]?
  4. Enter configuration commands, one per line. End with CNTL/Z.
  5. Switch(config)#interface fastEthernet 0/24
  6. Switch(config-if)#switchport mode trunk
  7. Switch(config-if)#
  8. %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/24, changed state to down
  9. %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/24, changed state to up

对于三层交换机,需要进行以下配置:

1)设置互联端口模式

2)开启路由功能

3)配置vlan并开启路由转发

配置如下:

  1. Switch>en
  2. Switch#config
  3. Configuring from terminal, memory, or network [terminal]?
  4. Enter configuration commands, one per line. End with CNTL/Z.
  5. # 1、配置接口模式,将包封装为802.1Q帧,并开启trunk模式
  6. Switch(config)#interface fastEthernet 0/1
  7. Switch(config-if)#switchport trunk encapsulation dot1q
  8. Switch(config-if)#switchport mode trunk
  9. Switch(config-if)#
  10. Switch(config-if)#exit
  11. # 2、开启路由
  12. Switch(config)#ip routing
  13. # 3、配置vlan,输入no shutdown开启路由模式。vlan的网络地址和对应网关相同
  14. Switch(config)#vlan 10
  15. Switch(config-vlan)#exit
  16. Switch(config)#vlan 20
  17. Switch(config-vlan)#exit
  18. Switch(config)#interface vlan 10
  19. Switch(config-if)#
  20. %LINK-5-CHANGED: Interface Vlan10, changed state to up
  21. %LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan10, changed state to up
  22. Switch(config-if)#ip address 192.168.10.254 255.255.255.0
  23. Switch(config-if)#no shutdown
  24. Switch(config-if)#exit
  25. Switch(config)#interface vlan 20
  26. Switch(config-if)#
  27. %LINK-5-CHANGED: Interface Vlan20, changed state to up
  28. %LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan20, changed state to up
  29. Switch(config-if)#ip address 192.168.20.254 255.255.255.0
  30. Switch(config-if)#no shutdown
  31. Switch(config-if)#^Z
  32. Switch#
  33. %SYS-5-CONFIG_I: Configured from console by console
  34. # 4、查看路由表
  35. Switch#show ip route
  36. Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
  37. D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
  38. N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
  39. E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
  40. i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
  41. * - candidate default, U - per-user static route, o - ODR
  42. P - periodic downloaded static route
  43. Gateway of last resort is not set
  44. C 192.168.10.0/24 is directly connected, Vlan10
  45. C 192.168.20.0/24 is directly connected, Vlan20

设置完毕后,可以进行验证:

PC0 ping PC2

  1. C:>ping 192.168.10.2
  2. Pinging 192.168.10.2 with 32 bytes of data:
  3. Reply from 192.168.10.2: bytes=32 time<1ms TTL=128
  4. Reply from 192.168.10.2: bytes=32 time<1ms TTL=128
  5. Reply from 192.168.10.2: bytes=32 time<1ms TTL=128
  6. Reply from 192.168.10.2: bytes=32 time=1ms TTL=128
  7. Ping statistics for 192.168.10.2:
  8. Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
  9. Approximate round trip times in milli-seconds:
  10. Minimum = 0ms, Maximum = 1ms, Average = 0ms

PC0 ping PC1

  1. C:>ping 192.168.20.1
  2. Pinging 192.168.20.1 with 32 bytes of data:
  3. Reply from 192.168.20.1: bytes=32 time<1ms TTL=127
  4. Reply from 192.168.20.1: bytes=32 time=1ms TTL=127
  5. Reply from 192.168.20.1: bytes=32 time=6ms TTL=127
  6. Reply from 192.168.20.1: bytes=32 time<1ms TTL=127
  7. Ping statistics for 192.168.20.1:
  8. Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
  9. Approximate round trip times in milli-seconds:
  10. Minimum = 0ms, Maximum = 6ms, Average = 1ms

使用tracert检验路由功能:

  1. C:>tracert 192.168.10.2
  2. Tracing route to 192.168.10.2 over a maximum of 30 hops:
  3. 1 0 ms 1 ms 0 ms 192.168.10.2
  4. Trace complete.
  5. C:>tracert 192.168.20.1
  6. Tracing route to 192.168.20.1 over a maximum of 30 hops:
  7. 1 0 ms 0 ms 0 ms 192.168.10.254
  8. 2 1 ms 0 ms 0 ms 192.168.20.1
  9. Trace complete.

PC0一跳就到达PC2(同个vlan),而PC0到PC1需要一条到达自己的网关192.168.10.254,再通过网关跳到目标主机PC1:192.168.20.1。

流程图如下:

3、总结

交换机创建vlan步骤为:命名vlan——设置switchport access为对应vlan

三层交换机配置流程为:开启路由功能——设置interface vlan开启对应vlan的路由功能

如果有新的机器加入,那么直接接入路由器并设置接口即可。而如果有新的vlan需要划分,则需要在三层路由器以及路由器上创建vlan,并在三层路由器进行网关配置。