劃分VLAN的方式有:基於接口、基於MAC地址、基於IP子網、基於協議、基於策略(MAC地址、IP地址、接口)。其中基於接口劃分VLAN,是最簡單,最多見的劃分方式。html
基於接口劃分VLAN指的是根據交換機的接口來劃分VLAN。網絡管理員預先給交換機的每一個接口配置不一樣的PVID,當一個數據幀進入交換機時,若是沒有帶VLAN標籤,該數據幀就會被打上接口指定PVID的Tag,而後數據幀將在指定PVID中傳輸。網絡
在典型的分層組網中,當接入交換機是三層交換機時,能夠做爲用戶的網關,減小匯聚交換機的配置和複雜度。ui
如圖1所示,PC1和PC2分別屬於VLAN 2和VLAN 3,經過接入交換機SW2接入匯聚交換機SW1。PC3屬於VLAN 4,經過SW3接入匯聚交換機SW1。接入交換機SW2做爲PC1和PC2的網關,SW3做爲PC3的網關,經過在交換機上配置靜態路由,實現用戶PC間的互訪以及和路由器的互連。code
採用以下的思路配置接入層設備做爲網關實現不一樣網段用戶間的通訊:htm
# 建立VLAN。blog
<HUAWEI> system-view
[HUAWEI] sysname SW2//修改設備的名稱爲SW2,便於識別 [SW2] vlan batch 2 to 3//批量建立VLAN 2和VLAN 3
# 將接口加入相應VLAN。接口
[SW2] interface gigabitethernet 1/0/23 [SW2-GigabitEthernet1/0/23] port link-type access//將與PC相鏈接口的接口類型設置爲access [SW2-GigabitEthernet1/0/23] port default vlan 2//將PC1劃分到VLAN 2 [SW2-GigabitEthernet1/0/23] quit [SW2] interface gigabitethernet 1/0/24 [SW2-GigabitEthernet1/0/24] port link-type access [SW2-GigabitEthernet1/0/24] port default vlan 3//將PC2劃分到VLAN 3 [SW2-GigabitEthernet1/0/24] quit
# 配置VLANIF接口,做爲用戶PC的網關。ip
[SW2] interface vlanif 2//建立VLANIF2接口 [SW2-Vlanif2] ip address 192.168.2.1 24//配置IP地址,此IP地址是PC1的網關地址 [SW2-Vlanif2] quit [SW2] interface vlanif 3//建立VLANIF3接口 [SW2-Vlanif3] ip address 192.168.3.1 24//配置IP地址,此IP地址是PC2的網關地址 [SW2-Vlanif3] quit
# 配置SW2和SW1互連。路由
[SW2] vlan batch 5//建立VLAN 5 [SW2] interface gigabitethernet 1/0/1 [SW2-GigabitEthernet1/0/1] port link-type access [SW2-GigabitEthernet1/0/1] port default vlan 5//SW2和SW1以Untagged方式通訊 [SW2-GigabitEthernet1/0/1] quit [SW2] interface vlanif 5//建立VLANIF5接口 [SW2-Vlanif5] ip address 192.168.5.2 24//配置IP地址,此IP地址是SW2與SW1互鏈接口的IP地址 [SW2-Vlanif5] quit [SW2] ip route-static 0.0.0.0 0.0.0.0 192.168.5.1//配置缺省路由,爲PC用戶找到訪問路由器的出口。缺省路由的下一跳是SW1相鏈接口的IP地址
# 建立VLAN。
<HUAWEI> system-view
[HUAWEI] sysname SW3//修改設備的名稱爲SW3 [SW3] vlan batch 4//批量建立VLAN 4
# 將接口加入相應VLAN。
[SW3] interface gigabitethernet 1/0/2 [SW3-GigabitEthernet1/0/2] port link-type access//將與PC相鏈接口的接口類型設置爲access [SW3-GigabitEthernet1/0/2] port default vlan 4//將PC3劃分到VLAN 4 [SW3-GigabitEthernet1/0/2] quit
# 配置VLANIF接口,做爲用戶PC的網關。
[SW3] interface vlanif 4//建立VLANIF4接口 [SW3-Vlanif4] ip address 192.168.4.1 24//配置IP地址,此IP地址是PC3的網關地址 [SW3-Vlanif4] quit
# 配置SW3和SW1互連。
[SW3] vlan batch 5//建立VLAN 5 [SW3] interface gigabitethernet 1/0/1 [SW3-GigabitEthernet1/0/1] port link-type access [SW3-GigabitEthernet1/0/1] port default vlan 5//SW3和SW1以Untagged方式通訊 [SW3-GigabitEthernet1/0/1] quit [SW3] interface vlanif 5//建立VLANIF5接口 [SW3-Vlanif5] ip address 192.168.5.3 24//配置IP地址,此IP地址是SW3與SW1互鏈接口的IP地址 [SW3-Vlanif5] quit [SW3] ip route-static 0.0.0.0 0.0.0.0 192.168.5.1//配置缺省路由,爲PC用戶指定訪問路由器的出口。缺省路由的下一跳是SW1相鏈接口的IP地址
# 建立VLAN。
<HUAWEI> system-view
[HUAWEI] sysname SW1//修改設備的名稱爲SW1 [SW1] vlan batch 5//批量建立VLAN 5
# 將鏈接PC的接口加入相應VLAN。
[SW1] interface gigabitethernet 1/0/1 [SW1-GigabitEthernet1/0/1] port link-type access//將與路由器相鏈接口的接口類型設置爲access [SW1-GigabitEthernet1/0/1] port default vlan 5 [SW1-GigabitEthernet1/0/1] quit [SW1] interface gigabitethernet 1/0/2 [SW1-GigabitEthernet1/0/2] port link-type access//將與SW2相鏈接口的接口類型設置爲access [SW1-GigabitEthernet1/0/2] port default vlan 5 [SW1-GigabitEthernet1/0/2] quit [SW1] interface gigabitethernet 1/0/3 [SW1-GigabitEthernet1/0/3] port link-type access//將與SW3相鏈接口的接口類型設置爲access [SW1-GigabitEthernet1/0/3] port default vlan 5 [SW1-GigabitEthernet1/0/3] quit
# 配置VLANIF接口,實現和路由器的互連。
[SW1] interface vlanif 5//建立VLANIF5接口 [SW1-Vlanif5] ip address 192.168.5.1 24//配置IP地址,此IP地址是與路由器對接的IP地址 [SW1-Vlanif5] quit
# 配置回程明細路由,實現內網網段之間互訪。
[SW1] ip route-static 192.168.2.0 255.255.255.0 192.168.5.2//目的IP是192.168.2.0/24網段的,下一跳是192.168.5.2,該IP地址對應SW2相鏈接口VLANIF的IP地址 [SW1] ip route-static 192.168.3.0 255.255.255.0 192.168.5.2//目的IP是192.168.3.0/24網段的,下一跳是192.168.5.2,該IP地址對應SW2相鏈接口VLANIF的IP地址 [SW1] ip route-static 192.168.4.0 255.255.255.0 192.168.5.3//目的IP是192.168.4.0/24網段的,下一跳是192.168.5.3,該IP地址對應SW3相鏈接口VLANIF的IP地址
# 配置缺省路由,實現內網網段到路由器的訪問。
[SW1] ip route-static 0.0.0.0 0.0.0.0 192.168.5.4//假設路由器與SW1相鏈接口的IP地址是192.168.5.4
PC一、PC二、PC3之間能夠相互訪問,同時PC均可以和路由器進行通訊。
SW1的配置文件
# sysname SW1 # vlan batch 5 # interface Vlanif5 ip address 192.168.5.1 255.255.255.0 # interface GigabitEthernet1/0/1 port link-type access port default vlan 5 # interface GigabitEthernet1/0/2 port link-type access port default vlan 5 # interface GigabitEthernet1/0/3 port link-type access port default vlan 5 # ip route-static 0.0.0.0 0.0.0.0 192.168.5.4 ip route-static 192.168.2.0 255.255.255.0 192.168.5.2 ip route-static 192.168.3.0 255.255.255.0 192.168.5.2 ip route-static 192.168.4.0 255.255.255.0 192.168.5.3 # return
SW2的配置文件
# sysname SW2 # vlan batch 2 to 3 5 # # interface Vlanif2 ip address 192.168.2.1 255.255.255.0 # interface Vlanif3 ip address 192.168.3.1 255.255.255.0 # interface Vlanif5 ip address 192.168.5.2 255.255.255.0 # interface GigabitEthernet1/0/1 port link-type access port default vlan 5 # interface GigabitEthernet1/0/23 port link-type access port default vlan 2 # interface GigabitEthernet1/0/24 port link-type access port default vlan 3 # ip route-static 0.0.0.0 0.0.0.0 192.168.5.1 # return
SW3的配置文件
# sysname SW3 # vlan batch 4 to 5 # interface Vlanif4 ip address 192.168.4.1 255.255.255.0 # interface Vlanif5 ip address 192.168.5.3 255.255.255.0 # interface GigabitEthernet1/0/1 port link-type access port default vlan 5 # interface GigabitEthernet1/0/2 port link-type access port default vlan 4 # ip route-static 0.0.0.0 0.0.0.0 192.168.5.1 # return