一、在一台交换机下分配VLAN

 搭建网络拓扑,在一个交换机下分配VLAN,交换机使用CISCO的3725路由器模仿

怎么在GNS3中打开vlan功能 gns3配置vlan_gns3

 启动所有设备,并为每台PC配置IP地址

PC1> ip 192.168.0.101 255.255.255.0
 Checking for duplicate address...
 PC1 : 192.168.0.101 255.255.255.0 PC2> ip 192.168.0.102 255.255.255.0
 Checking for duplicate address...
 PC1 : 192.168.0.102 255.255.255.0PC3> ip 192.168.0.103 255.255.255.0
 Checking for duplicate address...
 PC1 : 192.168.0.103 255.255.255.0PC4> ip 192.168.0.104 255.255.255.0
 Checking for duplicate address...
 PC1 : 192.168.0.104 255.255.255.0

接下来在交换机中分配每个端口的VLAN id。在交换机中建立两个VLAN,id分别是10 和20

ESW1#vlan d
 % Warning: It is recommended to configure VLAN from config mode,
   as VLAN database mode is being deprecated. Please consult user
   documentation for configuring VTP/VLAN in config mode.ESW1(vlan)#vlan 10 name vlan10
 VLAN 10 modified:
     Name: vlan10
 ESW1(vlan)#vlan 20 name vlan20
 VLAN 20 modified:
     Name: vlan20
 ESW1(vlan)#apply
 APPLY completed.
 ESW1(vlan)#exit
 APPLY completed.
 Exiting....

 

为f1/1 f1/2分配VLAN 10,为f1/3 f1/4分配VLAN20

ESW1#conf t
 Enter configuration commands, one per line.  End with CNTL/Z.
 ESW1(config)#int f1/1
 ESW1(config-if)#switchport mode access
 ESW1(config-if)#switchport access vlan 10
 ESW1(config-if)#exit
 ESW1(config)#int f1/2
 ESW1(config-if)#switchport mode access
 ESW1(config-if)#switchport access vlan 10
 ESW1(config-if)#exit
 ESW1(config)#int f1/3
 ESW1(config-if)#switchport mode access
 ESW1(config-if)#switchport access vlan 20
 ESW1(config-if)#ex
 ESW1(config)#int f1/4
 ESW1(config-if)#switchport access vlan 20
 ESW1(config-if)#exit

在配置f1/4时我们没有设置端口的类型为access,但是最后得到的效果是一样的,说明交换机默认的端口类型为access

access类型只允许被标记为被分配VLAN id的信息通过,如端口f1/4分配VLAN id 20,交换机中消息标签是20的消息才会被转发到该接口

配完VLAN后各PC的通信状况

怎么在GNS3中打开vlan功能 gns3配置vlan_gns3_02

怎么在GNS3中打开vlan功能 gns3配置vlan_IP_03

二、跨交换机分配VLAN

扩充网络拓扑如下

怎么在GNS3中打开vlan功能 gns3配置vlan_IP_04

 

和上文中分配IP地址一样,为PC5和PC6分配id 192.168.0.105 和192.168.0.106,类似的,在ESW2上建立两个VLAN id分别是10和20,并为f1/1和f1/2分别分配VLAN id 10和20

到现在PC1 PC2 PC5属于VLAN10 ,PC2 PC3 PC6属于VLAN20 ,但还不能跨交换机通信,需要在两个交换机上配置汇聚链路 trunk

ESW1(config)#int f1/0
 ESW1(config-if)#switchport mode trunk
 *Mar  1 00:33:06.135: %DTP-5-TRUNKPORTON: Port Fa1/0 has become dot1q trunk   //(可以看到trunk类型下默认是dot1q类型的封装,所以可以不用设置trunk的封装类型)
 ESW1(config-if)#switchport trunk allowed vlan all
 ESW1(config-if)#exESW2(config)#int f1/0
 ESW2(config-if)#switchport mode trunk
 *Mar  1 00:24:00.687: %DTP-5-TRUNKPORTON: Port Fa1/0 has become dot1q trunk
 ESW2(config-if)#switchport trunk allowed vlan all
 ESW2(config-if)#exit

 

现在PC1 PC2 PC5可以互相通信 ,PC2 PC3 PC6可以互相通信,但是VLAN10和VLAN20不能互相通信

怎么在GNS3中打开vlan功能 gns3配置vlan_gns3_05

怎么在GNS3中打开vlan功能 gns3配置vlan_怎么在GNS3中打开vlan功能_06

三、利用单臂路由进行跨VLAN实验

网络拓朴如下

怎么在GNS3中打开vlan功能 gns3配置vlan_链路_07

 

其中R1是c3725路由器,ESW连接R1的网络层接口

将ESW的f1/5接口类型设置为trunk类型,和设置两个交换机之间的汇聚链路操作相同。

启动R1的f0/0接口,并在配置f0/0两个子接口f0/0.1和f0/0.2的VLAN id 和ip地址

R1#conf t
 Enter configuration commands, one per line.  End with CNTL/Z.
 R1(config-if)#no shutdown
 *Mar  1 00:03:33.375: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
 *Mar  1 00:03:34.375: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
 R1(config-if)#ex
 R1(config)#int f0/0.1
 R1(config-subif)#encapsulation dot1Q 10
 *Mar  1 00:03:58.903: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
 R1(config-subif)#ip address 192.168.0.1 255.255.255.0
 R1(config-subif)#ex
 R1(config)#int f0/0.2
 R1(config-subif)#en
 R1(config-subif)#encapsulation dot1Q 20
 R1(config-subif)#ip address 192.168.1.1 255.255.255.0
 R1(config-subif)#ex
 R1(config)#exit

怎么在GNS3中打开vlan功能 gns3配置vlan_IP_08

可以看到此时两个子接口都已经分配了IP地址,并且已打开

一个物理接口的子接口需要在不同网段,所以为了使子接口f0/0.2可以和VLAN 20中的PC通信,将VLAN 20中的主机的IP地址的第三位修改为1,并将网关设置为192.168.1.1

如主机PC3

怎么在GNS3中打开vlan功能 gns3配置vlan_跨vlan_09

 

主机PC4和主机PC6分别设置 ip 192.168.1.104 255.255.255.0 192.168.1.1和ip 192.168.1.106 255.255.255.0 192.168.1.1

 

为VLAN10中的主机添加网关192.168.0.1

如PC1

怎么在GNS3中打开vlan功能 gns3配置vlan_IP_10

PC2和PC5分别设置ip 192.168.0.102 255.255.255.0 192.168.0.1和ip 192.168.0.105 255.255.255.0 192.168.0.1

打开R1的路由功能,在配置环境下输入ip routing

在VLAN10中的PC1上pingVLAN20中的PC6

怎么在GNS3中打开vlan功能 gns3配置vlan_怎么在GNS3中打开vlan功能_11