三層交換機
一、三層交換機的配置
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
命令實時調試更新信息。