三层交换机
一、三层交换机的配置
1.关于如何配置三层交换机,首先我们应该先创建VLAN
Switch>en
Switch#vlan database
% Warning: It is recommended to configure VLAN from config mode,as VLAN database mode is being deprecated. Please consult userdocumentation for configuring VTP/VLAN in config mode.Switch(vlan)#vlan 10
VLAN 10 added:Name: VLAN0010
Switch(vlan)#vlan 20
VLAN 20 added:Name: VLAN0020
Switch(vlan)#vlan 30
VLAN 30 added:Name: VLAN0030
Switch(vlan)#exit
APPLY completed.
Exiting....
2.将端口分配进VLAN
Switch(config)#int f0/1
Switch(config-if)#switchport mode access
Switch(config-if)#switchport access vlan 10
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int f0/2
Switch(config-if)#switchport mode access
Switch(config-if)#switchport access vlan 20
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int f0/3
Switch(config-if)#switchport mode access
Switch(config-if)#switchport access vlan 30
Switch(config-if)#no shutdown
Switch(config-if)#exit
3.进入VLAN进行地址网关的分配
Switch(config)#int vlan 10
Switch(config-if)#
%LINK-5-CHANGED: Interface Vlan10, changed state to up%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan10, changed state to upSwitch(config-if)#ip address 192.168.10.254 255.255.255.0
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int vlan 20
Switch(config-if)#
%LINK-5-CHANGED: Interface Vlan20, changed state to up%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan20, changed state to upSwitch(config-if)#ip address 192.168.20.254 255.255.255.0
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int vlan 30
Switch(config-if)#
%LINK-5-CHANGED: Interface Vlan30, changed state to up%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan30, changed state to upSwitch(config-if)#ip address 192.168.30.254 255.255.255.0
Switch(config-if)#no shutdown
Switch(config-if)#exit
4.一定要在全局模式下输入
Switch#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#ip routing
此为开启交换机路由器功能的全局模式
5.配置交换机接口模式
Switch(config)#int f0/1
Switch(config-if)#switchport mode access
Switch(config-if)#switchport mode trunkSwitch(config-if)#
%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 upSwitch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int f0/2
Switch(config-if)#switchport mode access
Switch(config-if)#switchport mode trunkSwitch(config-if)#
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/2, changed state to down%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/2, changed state to upSwitch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int f0/3
Switch(config-if)#switchport mode access
Switch(config-if)#switchport mode trunkSwitch(config-if)#
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/2, changed state to down%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/2, changed state to upSwitch(config-if)#no shutdown
Switch(config-if)#exit
二、三层交换机的原理
三层交换机结合了传统交换机和路由器的功能,能够在数据链路层(第二层)和网络层(第三层)进行高效的数据转发。以下是其核心工作原理:
1.基于硬件的转发
三层交换机使用专用集成电路(ASIC)或现场可编程门阵列(FPGA)实现高速数据转发。与软件路由相比,硬件转发大幅降低延迟,提升吞吐量。
2.一次路由多次交换
首次跨网段通信时,三层交换机会像路由器一样进行路由表查询和ARP解析,建立转发路径。后续相同流量的数据包直接由交换引擎处理,绕过路由模块。
3.VLAN间路由
通过创建虚拟接口(SVI),三层交换机能够在不同VLAN间直接路由,无需外接路由器。每个SVI对应一个VLAN的默认网关。
4.路由表与转发表
维护两种表结构:
路由表:存储网络拓扑信息,通过静态配置或动态路由协议(如OSPF)生成
转发表:记录MAC地址与端口的映射关系,用于二层交换
5.协议支持
同时处理二层协议(如STP)和三层协议(如IP、ICMP),支持ACL、QoS等高级功能。
三层交换机示例
保证全网段通信,网段配置已经给出
主机配置p1-p5
交换机s1
Switch>en
Switch#vlan database
% Warning: It is recommended to configure VLAN from config mode,as VLAN database mode is being deprecated. Please consult userdocumentation for configuring VTP/VLAN in config mode.Switch(vlan)#vlan 10
VLAN 10 added:Name: VLAN0010
Switch(vlan)#vlan 20
VLAN 20 added:Name: VLAN0020
Switch(vlan)#exit
APPLY completed.
Exiting....
Switch#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#int f0/1
Switch(config-if)#switchport mode access
Switch(config-if)#switchport access vlan 10
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int f0/2
Switch(config-if)#switchport mode access
Switch(config-if)#switchport access vlan 20
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int f0/3
Switch(config-if)#switchport mode trunkSwitch(config-if)#
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/3, changed state to down%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/3, changed state to upSwitch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#end
Switch#
%SYS-5-CONFIG_I: Configured from console by console
交换机s2
Switch>en
Switch#vlan database
% Warning: It is recommended to configure VLAN from config mode,as VLAN database mode is being deprecated. Please consult userdocumentation for configuring VTP/VLAN in config mode.Switch(vlan)#vlan 20
VLAN 20 added:Name: VLAN0020
Switch(vlan)#vlan 30
VLAN 30 added:Name: VLAN0030
Switch(vlan)#exit
APPLY completed.
Exiting....
Switch#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#int f0/1
Switch(config-if)#switchport mode access
Switch(config-if)#switchport access vlan 20
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int f0/2
Switch(config-if)#switchport mode access
Switch(config-if)#switchport access vlan 30
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int f0/3
Switch(config-if)#switchport mode trunkSwitch(config-if)#
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/3, changed state to down%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/3, changed state to upSwitch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#end
Switch#
%SYS-5-CONFIG_I: Configured from console by console
三层交换机ms1
Switch>en
Switch#vlan database
% Warning: It is recommended to configure VLAN from config mode,as VLAN database mode is being deprecated. Please consult userdocumentation for configuring VTP/VLAN in config mode.Switch(vlan)#vlan 10
VLAN 10 added:Name: VLAN0010
Switch(vlan)#vlan 20
VLAN 20 added:Name: VLAN0020
Switch(vlan)#vlan 30
VLAN 30 added:Name: VLAN0030
Switch(vlan)#exit
APPLY completed.
Exiting....
Switch#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#ip routing
Switch(config)#int vlan 10
Switch(config-if)#
%LINK-5-CHANGED: Interface Vlan10, changed state to up%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan10, changed state to upSwitch(config-if)#ip address 192.168.10.254 255.255.255.0
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int vlan 20
Switch(config-if)#
%LINK-5-CHANGED: Interface Vlan20, changed state to up%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan20, changed state to upSwitch(config-if)#ip address 192.168.20.254 255.255.255.0
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int vlan 30
Switch(config-if)#
%LINK-5-CHANGED: Interface Vlan30, changed state to up%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan30, changed state to upSwitch(config-if)#ip address 192.168.30.254 255.255.255.0
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int f0/1
Switch(config-if)#switchport mode trunk
Command rejected: An interface whose trunk encapsulation is "Auto" can not be configured to "trunk" mode.
Switch(config-if)#switchport mode access
Switch(config-if)#switchport mode trunkSwitch(config-if)#
%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 upSwitch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int f0/2
Switch(config-if)#switchport mode access
Switch(config-if)#switchport mode trunkSwitch(config-if)#
%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/2, changed state to down%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/2, changed state to upSwitch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#int f0/3
Switch(config-if)#no switchport
Switch(config-if)#ip address 10.0.0.1 255.255.255.252
Switch(config-if)#no shutdown
Switch(config-if)#exit
Switch(config)#ip route 0.0.0.0 0.0.0.0 10.0.0.2
Switch(config)#end
Switch#
%SYS-5-CONFIG_I: Configured from console by console
路由器r1配置
Router>en
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#int f0/0
Router(config-if)#ip address 10.0.0.2 255.255.255.252
Router(config-if)#no shutdownRouter(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/0, changed state to up%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to upRouter(config-if)#exit
Router(config)#int f0/1
Router(config-if)#ip address 1.0.0.1 255.0.0.0
Router(config-if)#no shutdownRouter(config-if)#
%LINK-5-CHANGED: Interface FastEthernet0/1, changed state to up%LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to upRouter(config-if)#exit
Router(config)#ip route 192.168.10.0 255.255.255.0 10.0.0.1
Router(config)#ip route 192.168.20.0 255.255.255.0 10.0.0.1
Router(config)#ip route 192.168.30.0 255.255.255.0 10.0.0.1
Router(config)#end
Router#
%SYS-5-CONFIG_I: Configured from console by console
结果
RIP动态路由
RIP(Routing Information Protocol)是一种基于距离矢量的动态路由协议,适用于小型网络。它使用跳数(hop count)作为度量标准,最大跳数为$15$,超过$16$跳的路由被视为不可达。RIP通过定期广播或组播路由更新(默认每30秒)来实现路由信息的动态交换。下面基于您提供的v1和v2介绍,进一步扩展协议细节、配置步骤和注意事项。
一、RIP版本详细比较
RIP v1:
- 仅支持有类地址(Classful Addressing),例如:$192.168.1.0$(默认掩码为$255.255.255.0$)。
- 自动汇总子网:当路由器发送更新时,会将子网汇总到类边界(如将$192.168.1.0/24$汇总为$192.168.0.0/16$),这可能导致路由不精确或环路。
- 使用广播更新(目标地址$255.255.255.255$),不支持认证,安全性较低。
- 最大跳数限制为$15$,适用于小规模网络。
RIP v2:
- 支持无类地址(Classless Addressing),允许使用子网掩码(如$192.168.1.0/26$),实现更精细的路由控制。
- 默认不自动汇总子网(通过
no auto-summary
命令禁用),路由器发送更新时包含精确的子网信息,减少路由错误。 - 使用组播更新(目标地址$224.0.0.9$),提高效率;支持MD5认证,增强安全性。
- 同样有跳数限制$15$,但更适合现代网络环境。
默认情况下,Cisco设备启用RIP v1;切换到v2需显式配置。v2的主要优势是解决了v1的局限性,如支持VLSM(可变长度子网掩码)和CIDR(无类域间路由)。
二、完整配置步骤
在Cisco设备上配置RIP时,需进入全局配置模式(configure terminal
),然后执行以下命令。配置核心是network
语句,指定路由器直连的IP网段(必须与接口IP匹配)。以下是一个扩展示例,包括v1和v2的典型场景。
示例:配置RIP v2(推荐用于无类地址环境)
Switch# configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)# router rip ! 启用RIP进程
Switch(config-router)# version 2 ! 切换到v2版本
Switch(config-router)# no auto-summary ! 禁用自动汇总
Switch(config-router)# network 192.168.1.0 ! 指定直连网段,例如192.168.1.0/24
Switch(config-router)# network 10.0.0.0 ! 添加另一个直连网段,例如10.0.0.0/8
Switch(config-router)# exit
Switch(config)# exit
Switch# write memory ! 保存配置到NVRAM
示例:配置RIP v1(传统模式)
Switch# configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)# router rip ! 启用RIP进程(默认v1)
Switch(config-router)# network 172.16.0.0 ! 指定直连网段,例如172.16.0.0/16
Switch(config-router)# exit
Switch(config)# exit
Switch# write memory
关键配置说明:
network
命令:必须输入路由器自身接口的直连网段(如$192.168.1.0$),RIP仅在这些网段上发送和接收更新。每个网段单独添加。- 版本切换:在
router rip
模式下,version 2
将协议升级;省略此命令则使用v1。 - 禁用汇总:v2中
no auto-summary
确保不自动汇总子网,避免路由黑洞。 - 添加认证(v2可选):使用
key chain
和ip rip authentication
命令实现MD5认证,提高安全性。
三、注意事项和最佳实践
网络设计:
- RIP适用于跳数少(小于$10$)的网络;大型网络推荐OSPF或EIGRP。
- 在v2中,确保所有路由器运行相同版本,避免兼容问题。
性能优化:
- 调整计时器:使用
timers basic
命令修改更新间隔(默认$30$秒),例如设为$20$秒以加快收敛,但会增加带宽开销。 - 被动接口:在
router rip
模式下,用passive-interface
命令阻止接口发送更新,减少不必要流量。
- 调整计时器:使用
常见问题排查:
- 路由环路:启用
split-horizon
(默认开启)或poison reverse
来预防。 - 更新丢失:检查
network
语句是否覆盖所有直连网段;使用show ip rip database
查看路由表。 - 安全风险:v1易受攻击;v2中务必配置认证。
- 路由环路:启用
迁移建议:
- 从v1升级到v2时,逐步部署:先在部分路由器启用v2,测试后全网切换。
- 监控工具:使用
debug ip rip
命令实时调试更新信息。