交换机 vlan设置

由糟糕的网络设计,引出划分vlan的必要性

  • 巨大的广播域,导致泛洪验证,性能低下
  • 一台机器中了病毒,由于没有隔离,会迅速传染给网络内的其他主机。
  • 不方便管理

vlan概要

办公楼有3层,每层都有IT部,HR部,销售部。把在1,2,3层的IT划分成一个vlan;把在1,2,3层的HR部划分成一个vlan;在1,2,3层的销售划分成一个vlan;这样做的目的是便于管理。

vlan特点

相同vlan即便在不同的交换机下,也可以相互通信;不同vlan即便是在同一个交换机下也不能通信。

vlan运作

红色的是一个vlan,黑色的是一个vlan,绿色的是一个vlan,它们都在交换机A里。

虽然红色和黑色在同一个交换机下,但是它们之间不能通信。

红色的是一个vlan,黑色的是一个vlan,绿色的是一个vlan,它们既在交换机A里,也在交换机B里。

即使不在同一个交换机下,红色的vlan是可以跨交换机通信的。

扫描二维码关注公众号,回复: 8662861 查看本文章

交换机A和交换机B连接的2个接口,必须修改成Trunk接口。Trunk接口不属于任何vlan,所以所有vlan的数据都能通过它传输。

那么trunk如何能够识别vlan号的呢,出交换机A时,trunk接口在原来的报文的以太网头部加一个4个字节的标记,标记里存放的是vlan号,然后传到了交换机B的trunk接口后,trunk接口查看以太网头部的标记,就知道了是哪个vlan过来的数据,然后把标记去掉后,传递个目标vlan。

打标记和去掉标记的协议有2种:

  • ISL:cisco专有的协议,是在报文的以太网头部在加一个ISL头部,把vlan号放进去。
  • 802.1Q:国际标准,所有厂商的交换机都支持。直接把vlan号放到报文的以太网头部里。所有一般都使用此协议。

  • cisco交换机下所有接口,默认都属于vlan1
  • 交换机最多支持4096个vlan号。范围是0-4095.其中0号和4095号不能用。
  • vlan1,vlan1002,vlan1003,vlan1004,vlan1005是交换机初始就有的vlan号,而且不能被删除。vlan1能使用,其他4个不能使用,是保留的。
    • 1-1001:标准vlan
    • 1006-4094:扩展vlan。cisco 3560以上的型号才支持,vtp协议的transparent模式下可用。

实验

添加2个pc,给pc设置ip的命令:

PC1> ip 192.168.1.10 255.255.255.0
Checking for duplicate address...
PC1 : 192.168.1.10 255.255.255.0

添加一个交换机,查看vlan表show vlan brief

IOU2#show vlan brief

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Et0/0, Et0/1, Et0/2, Et0/3
                                                Et1/0, Et1/1, Et1/2, Et1/3
                                                Et2/0, Et2/1, Et2/2, Et2/3
                                                Et3/0, Et3/1, Et3/2, Et3/3
1002 fddi-default                     act/unsup
1003 token-ring-default               act/unsup
1004 fddinet-default                  act/unsup
1005 trnet-default                    act/unsup

创建vlan方法1:先进入创建模式vlan database,然后vlan vlan号

但执行完vlan database后,不是立即创建vlan,必须使用exit,保存并退出后,才真正创建vlan。

IOU2#vlan database
% 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.
IOU2(vlan)#vlan 10
VLAN 10 added:
    Name: VLAN0010
IOU2(vlan)#exit
APPLY completed.
Exiting....
IOU2#show vlan brief

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Et0/0, Et0/1, Et0/2, Et0/3
                                                Et1/0, Et1/1, Et1/2, Et1/3
                                                Et2/0, Et2/1, Et2/2, Et2/3
                                                Et3/0, Et3/1, Et3/2, Et3/3
10   VLAN0010                         active
1002 fddi-default                     act/unsup
1003 token-ring-default               act/unsup
1004 fddinet-default                  act/unsup
1005 trnet-default                    act/unsup

修改vlan的名字:vlan 10 name NAME

IOU2(vlan)#vlan 10 name hr
VLAN 10 modified:
    Name: hr
IOU2(vlan)#exit
APPLY completed.
Exiting....
IOU2#show vlan b
IOU2#show vlan brief

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Et0/0, Et0/1, Et0/2, Et0/3
                                                Et1/0, Et1/1, Et1/2, Et1/3
                                                Et2/0, Et2/1, Et2/2, Et2/3
                                                Et3/0, Et3/1, Et3/2, Et3/3
10   hr                               active
1002 fddi-default                     act/unsup
1003 token-ring-default               act/unsup
1004 fddinet-default                  act/unsup
1005 trnet-default                    act/unsup

创建vlan方法2:先进入全局模式conf t,然后vlan vlan号,然配置名字name Name

IOU2#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
IOU2(config)#vlan 20
IOU2(config-vlan)#name it
IOU2(config-vlan)#exit
IOU2(config)#exit
*Jan 16 16:36:48.962: %SYS-5-CONFIG_I: Configured from console by console
IOU2#show vlan brief

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Et0/0, Et0/1, Et0/2, Et0/3
                                                Et1/0, Et1/1, Et1/2, Et1/3
                                                Et2/0, Et2/1, Et2/2, Et2/3
                                                Et3/0, Et3/1, Et3/2, Et3/3
10   hr                               active
20   it                               active
1002 fddi-default                     act/unsup
1003 token-ring-default               act/unsup
1004 fddinet-default                  act/unsup
1005 trnet-default                    act/unsup

把接口设置成输入接口

输入接口只能连pc。下面的设置:把交换机的接口e0/0设置成了输入接口

IOU2(config-if)#switchport mode access

IOU2(config)#int e0/0
IOU2(config-if)#switchport mode ?
  access        Set trunking mode to ACCESS unconditionally
  dot1q-tunnel  set trunking mode to TUNNEL unconditionally
  dynamic       Set trunking mode to dynamically negotiate access or trunk mode
  private-vlan  Set private-vlan mode
  trunk         Set trunking mode to TRUNK unconditionally
IOU2(config-if)#switchport mode access

把接口划分到某个vlan里

把接口e0/0划分到vlan 10里,

IOU2(config-if)#switchport access vlan 10
IOU2#show vlan brief

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Et0/1, Et0/2, Et0/3, Et1/0
                                                Et1/1, Et1/2, Et1/3, Et2/0
                                                Et2/1, Et2/2, Et2/3, Et3/0
                                                Et3/1, Et3/2, Et3/3
10   hr                               active    Et0/0
20   it                               active

虽然pc1和pc2在同一个局域网内,但由于pc1连接到了e0/0,pc2连接到了e0/1,e0/0和e0/1已经不在同一vlan里了,所有pc1和pc2已经不能互相通信了。

PC1> sho ip
NAME        : PC1[1]
IP/MASK     : 192.168.1.10/24
GATEWAY     : 255.255.255.0
DNS         :
MAC         : 00:50:79:66:68:00
LPORT       : 20004
RHOST:PORT  : 127.0.0.1:20005
MTU:        : 1500
PC1> ping 192.168.1.20
host (192.168.1.20) not reachable

PC2> show ip
NAME        : PC2[1]
IP/MASK     : 192.168.1.20/24
GATEWAY     : 255.255.255.0
DNS         :
MAC         : 00:50:79:66:68:01
LPORT       : 20006
RHOST:PORT  : 127.0.0.1:20007
MTU:        : 1500
PC2> ping 192.168.1.10
host (192.168.1.10) not reachable

把接口e0/1,划分到vlan 20

IOU2(config)#int e0/1
IOU2(config-if)#switchport mode access
IOU2(config-if)#switchport access vlan 20
IOU2(config-if)#exit
IOU2(config)#exit
IOU2#
*Jan 16 16:58:41.162: %SYS-5-CONFIG_I: Configured from console by console
IOU2#show vlan brief

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Et0/2, Et0/3, Et1/0, Et1/1
                                                Et1/2, Et1/3, Et2/0, Et2/1
                                                Et2/2, Et2/3, Et3/0, Et3/1
                                                Et3/2, Et3/3
10   hr                               active    Et0/0
20   it                               active    Et0/1

由于pc1和pc2已经不在同一vlan里了,所以最好修改pc2的ip,和pc1不在一个网段里。

PC2> ip 192.168.2.10 255.255.255.0
Checking for duplicate address...
PC1 : 192.168.2.10 255.255.255.0

再添加一台交换机switch2,和2台pc,分别是pc3和pc4

在交换机switch2里,添加vlan 10和vlan 20.

switch2#vlan database
switch2(vlan)#vlan 10 name hr
VLAN 10 added:
    Name: hr
switch2(vlan)#vlan 20 name it
VLAN 20 added:
    Name: it
switch2(vlan)#exit
APPLY completed.
Exiting....
switch2#show vlan b

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Et0/0, Et0/1, Et0/2, Et0/3
                                                Et1/0, Et1/1, Et1/2, Et1/3
                                                Et2/0, Et2/1, Et2/2, Et2/3
                                                Et3/0, Et3/1, Et3/2, Et3/3
10   hr                               active
20   it                               active

把交换机switch2的接口e0/1划分到vlan10;把e0/2划分到vlan20

switch2#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
switch2(config)#int e0/1
switch2(config-if)#switchport mode access
switch2(config-if)#switchport access vlan 10
switch2(config-if)#int e0/2
switch2(config-if)#switchport mode access
switch2(config-if)#swi acc vlan 20
switch2(config-if)#exit
switch2(config)#exit
switch2#show vlan b

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Et0/0, Et0/3, Et1/0, Et1/1
                                                Et1/2, Et1/3, Et2/0, Et2/1
                                                Et2/2, Et2/3, Et3/0, Et3/1
                                                Et3/2, Et3/3
10   hr                               active    Et0/1
20   it                               active    Et0/2

配置pc3和pc4的ip,让pc3和pc1在同一个网段,让pc4和pc2在同一个网段

PC3> show ip

NAME        : PC3[1]
IP/MASK     : 192.168.1.20/24
GATEWAY     : 255.255.255.0
DNS         :
MAC         : 00:50:79:66:68:02
LPORT       : 20014
RHOST:PORT  : 127.0.0.1:20015
MTU:        : 1500

PC4> show ip

NAME        : PC4[1]
IP/MASK     : 192.168.2.20/24
GATEWAY     : 255.255.255.0
DNS         :
MAC         : 00:50:79:66:68:03
LPORT       : 20016
RHOST:PORT  : 127.0.0.1:20017
MTU:        : 1500

这时pc1和pc3能ping通吗

是ping不通的,因为连接switch1(e0/2)和swtch2(e0/0)的接口,还并没有划分在vlan 10里。

划分到vlan 10里后,pc1和pc3就能ping通了。

IOU2(config)#int e0/2
IOU2(config-if)#switchport access vlan 10
IOU2#show vlan b
VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Et0/3, Et1/0, Et1/1, Et1/2
                                                Et1/3, Et2/0, Et2/1, Et2/2
                                                Et2/3, Et3/0, Et3/1, Et3/2
                                                Et3/3
10   hr                               active    Et0/0, Et0/2

switch2(config)#int e0/0
switch2(config-if)#switchport acc vlan 10
switch2#show vlan b
VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Et0/3, Et1/0, Et1/1, Et1/2
                                                Et1/3, Et2/0, Et2/1, Et2/2
                                                Et2/3, Et3/0, Et3/1, Et3/2
                                                Et3/3
10   hr                               active    Et0/0, Et0/1
20   it                               active    Et0/2

这时,pc2和pc4能ping通吗?

不能ping通,因为连接switch1(e0/2)和swtch2(e0/0)的接口,划分在vlan 10里,而pc2和pc4划分在vlan 20里。

修改switch1(e0/2)和swtch2(e0/0)的接口,为trunk接口后,pc2和pc4就能ping通了。

执行:IOU2(config-if)#switchport mode trunk,会报下面的错误
Command rejected: An interface whose trunk encapsulation is "Auto" can not be configured to "trunk" mode.

错误原因:前面说过打标记和解标记有2个协议,一个是cisco特有的ISL,另一个是国际标准802.1Q,所以设置trunk接口时要明确告诉交换机用哪个协议。

告诉交换机用哪个协议:IOU2(config-if)#switchport trunk encapsulation dot1q

dot1q就是802.1Q。

IOU2(config)#int e0/2
IOU2(config-if)#switchport mode trunk
Command rejected: An interface whose trunk encapsulation is "Auto" can not be configured to "trunk" mode.
IOU2(config-if)#switchport trunk encapsulation dot1q
IOU2(config-if)#switchport mode trunk

switch2(config)#int e0/0
switch2(config-if)#swi
switch2(config-if)#switchport mo
switch2(config-if)#switchport mode tru
switch2(config-if)#switchport mode trunk
Command rejected: An interface whose trunk encapsulation is "Auto" can not be configured to "trunk" mode.
switch2(config-if)#switchport trunk encapsulation dot1q
switch2(config-if)#switchport mode trunk

这时pc1和pc3,pc2和pc4都能ping通了。

把switch1(e0/2)和swtch2(e0/0)设置成trunk接口后,它们就消失了。不属于任何一个vlan。

show vlan brief命令结果里消失的接口,有2种情况。一种是:它不属于任何vlan了;另一种是它是trunk接口了。

IOU2#show vlan brief

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Et0/3, Et1/0, Et1/1, Et1/2
                                                Et1/3, Et2/0, Et2/1, Et2/2
                                                Et2/3, Et3/0, Et3/1, Et3/2
                                                Et3/3
10   hr                               active    Et0/0
20   it                               active    Et0/1

switch2#show vlan b

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Et0/3, Et1/0, Et1/1, Et1/2
                                                Et1/3, Et2/0, Et2/1, Et2/2
                                                Et2/3, Et3/0, Et3/1, Et3/2
                                                Et3/3
10   hr                               active    Et0/1
20   it                               active    Et0/2

到此为止,证明了:相同vlan即便在不同的交换机下,也可以相互通信;不同vlan即便是在同一个交换机下也不能通信。

查看哪个接口是trunk

IOU2#show interfaces trunk

Port        Mode             Encapsulation  Status        Native vlan
Et0/2       on               802.1q         trunking      1

Port        Vlans allowed on trunk
Et0/2       1-4094

Port        Vlans allowed and active in management domain
Et0/2       1,10,20

Port        Vlans in spanning tree forwarding state and not pruned
Et0/2       1,10,20

switch2#show interfaces trunk

Port        Mode             Encapsulation  Status        Native vlan
Et0/0       on               802.1q         trunking      1

Port        Vlans allowed on trunk
Et0/0       1-4094

Port        Vlans allowed and active in management domain
Et0/0       1,10,20

Port        Vlans in spanning tree forwarding state and not pruned
Et0/0       1,10,20
  • Encapsulation:802.1q:使用的协议

  • Native vlan:1.

    这个1是vlan号。

    trunk接口把过来的数据打上标记,对端的trunk接口读这个标记。【Native vlan】指定的vlan号,经过trunk接口时,trunk接口不给它打标记。对端的trunk接口读标记时,发现没有标记,则把这个数据包给,自己这段的trunk接口里【Native vlan】定义的vlan里的主机。

    对端的2个trunk接口的【Native vlan】一定要一样!!!

设置【Native vlan】:switchport trunk native vlan vlan号

IOU2(config)#int e0/2
IOU2(config-if)#swi
IOU2(config-if)#switchport tr
IOU2(config-if)#switchport trunk na
IOU2(config-if)#switchport trunk native v
IOU2(config-if)#switchport trunk native vlan 2

IOU2#show interfaces trunk

Port        Mode             Encapsulation  Status        Native vlan
Et0/2       on               802.1q         trunking      2

实验【Native vlan】:

添加pc5和pc6,pc5连switch1的接口e0/3,把e0/3的vlan设置从vlan 2;pc6连switch2的接口e0/3,使用接口e0/3默认的vlan号,就是vlan 1.

把pc5和pc6的ip设置到同一网段。pc5和pc6虽然在不同的vlan,但居然可以ping通了。原因就是对端的2个trunk接口的【Native vlan】不一样导致的。switch1的【Native vlan】是vlan2,所以vlan2下的主机通过此trunk时,没有被打上标记,传递到对端的trunk后,对端的trunk接口发现没有标记,则直接传递个自己的【Native vlan】,是vlan1,所以导致了不同vlan也能通信的现象。

IOU2(config)#int e0/3
IOU2(config-if)#
*Jan 16 18:27:37.386: %CDP-4-NATIVE_VLAN_MISMATCH: Native VLAN mismatch discovered on Ethernet0/2 (2), with switch2 Ethernet0/0 (1).
IOU2(config-if)#sw mo acc
IOU2(config-if)#sw acc v
IOU2(config-if)#sw acc vlan 2
% Access VLAN does not exist. Creating vlan 2

PC5> ip 192.168.3.10 255.255.255.0
Checking for duplicate address...
PC1 : 192.168.3.10 255.255.255.0

PC6> ip 192.168.3.20 255.255.255.0
Checking for duplicate address...
PC1 : 192.168.3.20 255.255.255.0

PC5> ping 192.168.3.20
84 bytes from 192.168.3.20 icmp_seq=1 ttl=64 time=0.783 ms
84 bytes from 192.168.3.20 icmp_seq=2 ttl=64 time=0.985 ms
84 bytes from 192.168.3.20 icmp_seq=3 ttl=64 time=0.777 ms
84 bytes from 192.168.3.20 icmp_seq=4 ttl=64 time=2.228 ms
84 bytes from 192.168.3.20 icmp_seq=5 ttl=64 time=1.446 ms

PC6> ping 192.168.3.10
84 bytes from 192.168.3.10 icmp_seq=1 ttl=64 time=0.950 ms
84 bytes from 192.168.3.10 icmp_seq=2 ttl=64 time=1.014 ms
84 bytes from 192.168.3.10 icmp_seq=3 ttl=64 time=0.703 ms
84 bytes from 192.168.3.10 icmp_seq=4 ttl=64 time=0.498 ms
84 bytes from 192.168.3.10 icmp_seq=5 ttl=64 time=2.023 ms

实验加个路由器R1,实现用单臂路由(vlan间路由),连通vlan10和vlan20下的pc

实现思路:把vlan10和vlan20下的主机里的网关,都指定到路由器R1的接口e0/0下,网关在同一个接口下,那么2个vlan的跨网段请求都发送到了同一个接口,那么路由器就能够知道怎么转发请求了。

那就出现个问题,在一个接口下怎么配置2个网关ip呢?

先启用接口e0/0

R1(config)#int e0/0
R1(config-if)#no sh

在接口e0/0下建立2个子接口e0/0.1和e0/0.2。

子接口可以建立多少个?ip地址有多少个就能创建多少个。所以2个网关的地址问题解决了。

那么子接口如何知道,过来的数据是来自哪个vlan呢?

尝试在e0/0.1接口下,添加网关ip add 192.168.1.1 255.255.255.0,但提示下面的错误。

错误的意思是要提供:用什么协议打标记和解标记,和vlan号。

R1(config)#int e0/0.1
R1(config-subif)#ip add 192.168.1.1 255.255.255.0

% Configuring IP routing on a LAN subinterface is only allowed if that
subinterface is already configured as part of an IEEE 802.10, IEEE 802.1Q,
or ISL vLAN.

提供vlan号,就是告诉这个子接口,来自这个vlan的数据,归你管了。

提供dot1Q,就是告诉这个子接口,用哪个协议去解包,得到vlan号。

给子接口指定了vlan号和协议后,再添加网关就ok了。

R1(config-subif)#encapsulation dot1Q 10
R1(config-subif)#ip add 192.168.1.1 255.255.255.0

接口e0/0.2同样配置

R1(config)#int e0/0.2
R1(config-subif)#encapsulation dot1Q 20
R1(config-subif)#ip add 192.168.2.1 255.255.255.0

添加pc1-4的网关:

PC1> ip 192.168.1.10 255.255.255.0 192.168.1.1
Checking for duplicate address...
PC1 : 192.168.1.10 255.255.255.0 gateway 192.168.1.1

PC2> ip 192.168.2.10 255.255.255.0 192.168.2.1
Checking for duplicate address...
PC2 : 192.168.2.10 255.255.255.0 gateway 192.168.2.1

PC3> ip 192.168.1.20 255.255.255.0 192.168.1.1
Checking for duplicate address...
PC3 : 192.168.1.20 255.255.255.0 gateway 192.168.1.1

PC4> ip 192.168.2.20 255.255.255.0 192.168.2.1
Checking for duplicate address...
PC4 : 192.168.2.20 255.255.255.0 gateway 192.168.2.1

到此为止,vlan10和vlan20能通信了吗?不能?

由于路由器R1的e0/0配置了解包的协议和vlan号,但是交换机switch1的接口e0/3还不是trunk接口,那么从switch1的接口e0/3出来的数据包里,就没有vlan号,那么路由器R1就无法得到vlan号,所以就丢弃过来的数据了。

设置switch1的接口e0/3为trunk接口

IOU2(config)#int e0/3
IOU2(config-if)#switchport trunk encapsulation dot1q
IOU2(config-if)#switchport mo tr

现在vlan10和vlan10下的pc之间就可以互相通信了。

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

猜你喜欢

转载自www.cnblogs.com/xiaoshiwang/p/12205319.html