菜鸟学习CCNA-交换机基础

    交换机属于二层设备(也有三层),作用隔离冲突图;交换机是基于收到的数据帧中的源 MAC 地址和目的MAC 地址来进行工作的。以太网交换机转发数据帧有三种交换方式:存储转发(Store-and-Forward)、直接转发(Cut-Through)、无碎片(Fragment-Free)。
基本命令:
(1)步骤 1:交换机名称配置
Switch>enable
Switch#conf terminal
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#hostname S1
(2) 步骤 2:配置密码
S1(config)#enable secret cisco
S1(config)#line vty 0 15
S1(config-line)#password cisco
S1(config-line)#login
(3) 步骤 3:接口基本配置
S1(config)#interface f0/1    //默认时交换机的以太网接口是开启的。
switch(config-if)#duplex { full | half | auto }
//duplex 用来配置接口的双工模式, full—全双工、half—半双工、auto—自动检测双工的模式
switch(config-if)#speed { 10 | 100 | 1000 | auto }
//speed 命令用来配置交换机的接口速度, 10——10M、100——100M、1000——1000M、auto
——自动检测接口速度。
(4) 配置管理地址
交换机也允许被 telnet,这时需要在交换机上配置一个IP 地址,这个地址是在VLAN 接
口上配置的。如下:
S1(config)#int vlan 1
S1(config-if)#ip address 172.16.0.1 255.255.0.0
S1(config-if)#no shutdown
S1(config)#ip default-gateway 172.16.0.254
//以上在 VLAN 1 接口上配置了管理地址,接在VLAN 1 上的计算机可以直接进行telnet 该
地址。为了其他网段的计算机也可以 telnet 交换机,我们在交换机上配置了缺省网关。
(5) 保存配置
S1#copy running-config startup-config
Destination filename [startup-config]?
Building configuration...
[OK]
 
S1(config)#int f0/1
S1(config-if)#shutdown
S1(config-if)#switch mode access
//以上命令把端口改为访问模式,即用来接入计算机,在下一章详细介绍该命令的含义。
S1(config-if)#switch port-securitiy
//以上命令是打开交换机的端口安全功能。
S1(config-if)#switch port-securitiy maximum 1
//以上命令只允许该端口下的 MAC 条目最大数量为1,即只允许一个设备接入
S1(config-if)#switch port-securitiy violation { protect | shutdown | restrict }
? protect:当新的计算机接入时,如果该接口的 MAC 条目超过最大数量,则这个新的计算
机将无法接入,而原有的计算机不受影响
? shutdown:当新的计算机接入时,如果该接口的 MAC 条目超过最大数量,则该接口将会
被关闭,则这个新的计算机和原有的计算机都无法接入,需要管理员使用“ no shutdown”
命令重新打开。
? restrict:当新的计算机接入时,如果该接口的 MAC 条目超过最大数量,则这个新的计
算机可以接入,然而交换机将向发送警告信息。
S1(config-if)#switchport port-security mac-address 0019.5535.b828
//允许 R1 路由器从f0/1 接口接入
S1(config-if)#no shutdown
S1(config)#int vlan1
S1(config-if)#no shutdown
S1(config-if)#ip address 172.16.0.1 255.255.0.0
//以上配置交换机的管理地址
(3) 步骤 3:检查MAC 地址表
S1#show mac-address-table
Mac Address Table
-------------------------------------------
(此处省略)
Vlan Mac Address Type Ports
---- ----------- -------- -----
All 0100.0ccc.cccc STATIC CPU
1 0018.ba11.eb91 DYNAMIC Fa0/15
1 0019.5535.b828 STATIC Fa0/1
Total Mac Addresses for this criterion: 24
//R1 的 MAC 已经被登记在f0/1 接口,并且表明是静态加入的
(4) 步骤 4:模拟非法接入
这时从 R1 ping 交换机的管理地址,可以ping 通,如下:
R1#ping 172.16.0.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 172.16.0.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/4 ms
在 R1 上修改g0/0 的MAC 地址为另一个地址,模拟是另外一台设备接入。如下:
R1(config)#int g0/0
R1(config-if)#mac-address 12.12.12
几秒钟后,则在 S1 上,出现:
01:09:39: %PM-4-ERR_DISABLE: psecure-violation error detected on Fa0/1, putting Fa0/1 in
err-disable state
01:09:39: %PORT_SECURITY-2-PSECURE_VIOLATION: Security violation occurred, caused by MAC address
0012.0012.0012 on port FastEthernet0/1.
01:09:40: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to down
//以上提示 f0/1 接口被关闭
S1#show int f0/1
FastEthernet0/1 is down, line protocol is down (err-disabled)
Hardware is Fast Ethernet, address is 0018.ba11.f503 (bia 0018.ba11.f503)
MTU 1500 bytes, BW 100000 Kbit, DLY 100 usec,
reliability 255/255, txload 1/255, rxload 1/255
//以上表明 f0/1 接口因为错误而被关闭。非法设备移除后,在f0/1 接口下,执行“shutdown”
和“ no shutdown”命令可以重新打开该接口。