教你利用三層交換機實現 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
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

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

IP Address......................: 192.168.1.2
Subnet Mask.....................: 255.255.255.0
Default Gateway.................: 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

IP Address......................: 192.168.2.2
Subnet Mask.....................: 255.255.255.0
Default Gateway.................: 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

IP Address......................: 192.168.1.3
Subnet Mask.....................: 255.255.255.0
Default Gateway.................: 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

<以上代碼可復制粘貼,可往左滑>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM