什么是交换机?交换机的作用是什么?以及交换机的基本配置有哪些?这些是我需要思考的。(注意:我们这里所说的是二层交换机)

交换机是软考的必考点,但是理论看多了就有些迷茫,不明白底层的一些含义及想法。

简介交换机

交换机也叫交换式集线器,作用和集线器大题相同。但是为何集线器远远没有交换机出名呢?
这源于它们俩的特性不同,集线器采用共享带宽,而交换机采用独占带宽。所以交换机能获得的带宽,至于原理笔者也不能理解。

交换机作用

交换机的作用是用来连接局域网(LAN)内的多个节点,用于局域网内的数据交换,因此它主要运用于组建局域网使用。
交换机有许多接口,不同的接口可以连接不同的主机,因此我们可以轻易的理解到交换机使相应的主机处于同一个局域网中。

交换机分类

按适用范围划分:接入层交换机、汇聚层交换机、核心层交换机

常用配置命令

配置enable口令、密码和主机名

1
2
3
4
5
6
7
8
Switch>
Switch>enable (简写en)
Switch#config terminal
Switch(config)#enable password cisco
Switch(config)#enable secret cisco1
Switch(config)#hostname C2950
C2950(config)#end
C2950#

配置IP地址、默认网关、域名和域名服务器

1
2
3
4
5
C2950(config)#ip address 192.168.1.1 255.255.255.0
C2950(config)#ip default-gatway 192.168.1.254
C2950(config)#ip domain-name cisco.com
C2950(config)#ip name-senver 200.0.0.1
C2950(config)#end

配置端口属性

1
2
3
4
5
6
7
8
9
C2950(config)#interface fastethernet0/1 (简写int f0/1)
C2950(config-if)#speed ?
C2950(config-if)#speed 100
C2950(config-if)#duplex ?
C2950(config-if)#euplex full
C2950(config-if)#description TO_PC1
C2950(config-if)#^Z
C2950#show interface fastethernet0/1
C2950#show interface fastethernet0/1 status

配置和查看MAC地址表

1
2
3
4
5
6
7
C2950(config)#mac-address-table ?
C2950(config)#mac-address-table aging-time 100
C2950(config)#mac-address-table permanent 0000.0c01.bbcc f0/3
C2950(config)#mac-address-table restricted static 0000.0c02.bbcc f0/3 f0/7
C2950(config)#end
C2950#show mac-address-table
C2950#show mac-address-table restricted static

配置和管理VLAN

什么是VLAN

VLAN是用于把物理上直接相连的网络从逻辑上划分为多个子网。交换机将主机连接在同一个局域网,如果主机过多就会不便于管理,尤其可能形成”广播风暴”之类的现象,如果我们可以将它们划分,那么它们就可以共享一个冲突域,而不是同享一个广播域了。(默认配置下,所有接口处于可用状态并且都属于VLAN1)

VLAN的作用

VLAN划分了网络,那么它就可以对局域网里面的网络进行合理的管理了。对于区分用户主体、路径都是可以自由配置的。为了获得这些能力,我们不能不学习一些协议了,它们分别是VTPVLAN中继VLAN配置了。

VLAN中继

VLAN中继(VLAN Trunk)也称为VLAN主干,在交换机与交换机或交换机与路由器之间连接的端口上配置中继模式,使得属于不同的VLAN的数据库都可以通过这条中继链路进行传播。
这个很好理解,不同的VLAN始终是需要通信的,所有这个问题必须解决。但是笔者疑惑的是不同的VLAN之间原本要引入第三层交换技术才可以解决,VLAN处理数据帧那VLAN中继应该是第二次的协议,如何处理不同VLAN通信。

VTP协议

VTP协议可以维护VLAN信息全网的一致性、VTP有三种工作模式,客户机模式client、服务器模式server、透明模式transparent,详细的差别点这里就不叙述了。

配置2950A交换机为服务器模式
1
2
3
4
5
6
7
8
9
10
11
12
2950A#
2950A#vlan database
2950A#vtp ?
2950A(vlan)#vtp server
Setting device to VTP SERVER mode.
2950A(vlan)#vtp domain vtpserver
Changing VTP domain name from NULL to vtpserver.
2950A(vlan)#vtp pruning
Pruning switched ON
2950A(vlan)#exit
APPLY completed .Exiting...
2950A#show vtp status
配置2950A交换机为客户机模式
1
2
3
4
5
2950A#
2950A#vlan database
2950A(vlan)#vtp client
Setting device to VTP CLIENT mode.
2950A(vlan)#exit
配置VLAN Trunk端口
1
2
3
4
5
6
7
Switch#config terminal
Switch(config)#interface f0/24
Switch(config-if)#switchport mode trunk
Switch(config-if)#switchport trunk allow vlan all
Switch(config-if)#exit
Switch(config)#exit
Switch#

配置VALN

创建VLAN

VLAN信息可以在服务器模式或透明模式交换机创建

1
2
3
4
5
6
7
8
2950A#
2950A#vlan database
2950A(vlan)#vlan 2
VLAN 2 added:
Name:VLAN002
2950A(vlan)#vlan 3 name vlan3
VLAN 3 added: Name vlan3
2950A(vlan)#exit

将端口添加到某VLAN中
1
2
3
4
5
6
7
8
9
10
11
12
Switch#config terminal
Switch(config)#interface f0/9
Switch(config-if)#switchport mode access
Switch(config-if)#switchport access vlan2
Switch(config-if)#exit
Switch(config)#interface f0/10
Switch(config-if)#switchport mode access
Switch(config-if)#switchport access vlan3
Switch(config-if)#exit
Switch(config)#exit
Switch#show vlan
Switch#

生成树协议配置

什么是生成树

由于交换机之间需要冗余连接(当然这可以提高网络的可靠性),因此对于数据帧的转发就可能存在网络环路的现象产生了(多个Trunk端口),这种现象的产生对于网络往往是致命的,严重影响其他正常数据的传播。
因此我们可以通过一直规定告诉交换机哪个端口可以用于转发,哪些会被阻断。于是我们的STP登上了舞台。

生成树配置

对于生成树我们可以根据端口权值路径值实现负载均衡。

端口权值(port-priority)
1
2
3
4
5
6
Switch#config terminal
Switch(config)#interface f0/9
Switch(config-if)#spanning-tree vlan 1 port-priority 10
Switch(config-if)#spanning-tree vlan 2 port-priority 10
Switch(config-if)#end
Switch#copy running-config startup-config
路径值(cost)
1
2
3
4
5
6
Switch#config terminal
Switch(config)#interface f0/9
Switch(config-if)#spanning-tree vlan 1 cost 30
Switch(config-if)#spanning-tree vlan 1 cost 30
Switch(config-if)#exit
Switch#

以上就是对交换机配置的小小见解。