我们通常说的MAC地址与交换机端口绑定其实就是交换机端口安全功能。端口安全功能能让您配置一个端口只允许一台或者几台确定的设备访问那个交换机;能根据MAC地址确定允许访问的设备;允许访问的设备的MAC地址既可以手工配置,也可以从交换机“学到”;当一个未批准的MAC地址试图访问端口的时候,交换机会挂起或者禁用该端口等等。
 

sw1#show run int f0/1
Building configuration...

Current configuration : 160 bytes
!
interface FastEthernet0/1
 switchport mode access
 switchport port-security
 switchport port-security violation shutdown
 switchport port-security mac-address 001f.162c.f110 
 spanning-tree portfast
end
sw1#

Found duplicate mac-address 001f.162c.f110
clear mac-address-table dynamic address mac|j
1.端口mac被其他的端口给绑定过了
2.端口上打no switchport portsecurity
3.上联的端口可能打了switchport portsecurity mac address 1111.1111.1111.111 ,所以导致下联的设备无法再次绑定此mac-address

 一、首先必须明白两个概念:

可靠的MAC地址。配置时候有三种类型。

静态可靠的MAC地址:在交换机接口模式下手动配置,这个配置会被保存在交换机MAC地址表和运行配置文件中,交换机重新启动后不丢失(当然是在保存配置完成后),具体命令如下:

Switch(config-if)#switchport port-security mac-address Mac地址

动态可靠的MAC地址:这种类型是交换机默认的类型。在这种类型下,交换机会动态学习MAC地址,但是这个配置只会保存在MAC地址表中,不会保存在运行配置文件中,并且交换机重新启动后,这些MAC地址表中的MAC地址自动会被清除。

黏性可靠的MAC地址:这种类型下,可以手动配置MAC地址和端口的绑定,也可以让交换机自动学习来绑定,这个配置会被保存在MAC地址中和运行配置文件中,如果保存配置,交换机重起动后不用再自动重新学习MAC地址,虽然黏性的可靠的MAC地址可以手动配置,但是CISCO官方不推荐这样做。具体命令如下:

Switch(config-if)#switchport port-security mac-address sticky
其实在上面这条命令配置后并且该端口得到MAC地址后,会自动生成一条配置命令
Switch(config-if)#switchport port-security mac-address sticky Mac地址
这也是为何在这种类型下CISCO不推荐手动配置MAC地址的原因。

二、违反MAC安全采取的措施:
当超过设定MAC地址数量的最大值,或访问该端口的设备MAC地址不是这个MAC地址表中该端口的MAC地址,或同一个VLAN中一个MAC地址被配置在几个端口上时,就会引发违反MAC地址安全,这个时候采取的措施有三种:

1.保护模式(protect):丢弃数据包,不发警告。

2.限制模式(restrict):丢弃数据包,发警告,发出SNMP trap,同时被记录在syslog日志里。

3.关闭模式(shutdown):这是交换机默认模式,在这种情况下端口立即变为err-disable状态,并且关掉端口灯,发出SNMP trap,同时被记录在syslog日志里,除非管理员手工激活,否则该端口失效。

具体命令如下:

Switch(config-if)#switchport port-security violation {protect | restrict | shutdown }
下面这个表一就是具体的对比
Violation Mode Traffic is forwarded Sends SNMP trap Sends syslog message Displays error
message Shuts down port
protect No No No No No
restrict No Yes Yes No No
shutdown No Yes Yes No Yes
表一

配置端口安全时还要注意以下几个问题:

端口安全仅仅配置在静态Access端口;在trunk端口、SPAN端口、快速以太通道、吉比特以太通道端口组或者被动态划给一个VLAN的端口上不能配置端口安全功能;不能基于每VLAN设置端口安全;交换机不支持黏性可靠的MAC地址老化时间。protect和restrict模式不能同时设置在同一端口上。
下面把上面的知识点连接起来谈谈实现配置步骤的全部命令。

1.静态可靠的MAC地址的命令步骤:

Switch#config terminal
 Switch(config)#interface interface-id 进入需要配置的端口
 Switch(config-if)#switchport mode Access 设置为交换模式
 Switch(config-if)#switchport port-security 打开端口安全模式

Switch(config-if)#switchport port-security violation {protect | restrict | shutdown }
上面这一条命令是可选的,也就是可以不用配置,默认的是shutdown模式,但是在实际配置中推荐用restrict。

Switch(config-if)#switchport port-security maximum value
上面这一条命令也是可选的,也就是可以不用配置,默认的maximum是一个MAC地址,2950和3550交换机的这个最大值是132。

其实上面这几条命令在静态、黏性下都是一样的,
Switch(config-if)#switchport port-security mac-address MAC地址
上面这一条命令就说明是配置为静态可靠的MAC地址

2.动态可靠的MAC地址配置,因为是交换机默认的设置。
3.黏性可靠的MAC地址配置的命令步骤:
Switch#config terminal
  Switch(config)#interface interface-id
  Switch(config-if)#switchport mode Access
  Switch(config-if)#switchport port-security
  Switch(config-if)#switchport port-security violation {protect | restrict | shutdown }
Switch(config-if)#switchport port-security maximum value 字串4

上面这几天命令解释和前面静态讲到原因一样,不再说明。

Switch(config-if)#switchport port-security mac-address sticky
上面这一条命令就说明是配置为黏性可靠的MAC地址。
最后,说说企业中如何快速MAC地址与交换机端口绑定。在实际的运用中常用黏性可靠的MAC地址绑定,现在我们在一台2950EMI上绑定。

方法1:在CLI方式下配置
2950 (config)#int rang fa0/1 - 48
2950 (config-if-range)#switchport mode Access
2950 (config-if-range)#switchport port-security
2950 (config-if-range)#switchport port-security mac-address violation restrict
2950 (config-if-range)#switchport port-security mac-address sticky
这样交换机的48个端口都绑定了,注意:在实际运用中要求把连在交换机上的PC机都打开,这样才能学到MAC地址,并且要在学到MAC地址后保存配置文件,这样下次就不用再学习MAC地址了,然后用show port-security address查看绑定的端口,确认配置正确。

方法2:在WEB界面下配置,也就是CMS(集群管理单元)
我们通过在IE浏览器中输入交换机IP地址,就可以进入,然后在port—port security下可以选定交换机端口,在Status和Sticky MAC Address中选Enable或Disabled,Violation Action可以选Shutdown、Restrict、Protect中的一种,Maximum Address Count(1-132)可以填写这个范围的数值。
当然还有要求绑定IP地址和MAC地址的,这个就需要三层或以上的交换了,因为我们知道普通的交换机都是工作在第二层,也就是使数据链路层,是不可能绑定IP的。假如企业是星型的网络,中心交换机是带三层或以上功能的。我们就可以在上绑定,
Switch(config)#arp Ip地址 Mac地址 arpa
 

实验十八、交换机端口与MAC 绑定
一、 实验目的
1、了解什么是交换机的 MAC 绑定功能;
2、熟练掌握 MAC 与端口绑定的静态、动态方式。
二、 应用环境
1、当网络中某机器由于中毒进而引发大量的广播数据包在网络中洪泛时,网络管理员的唯一想法就是尽快地找到根源主机并把它从网络中暂时隔离开。当网络的布置很随意时,任何用户只要插上网线,在任何位置都能够上网,这虽然使正常情况下的大多数用户很满意,但一旦发生网络故障,网管人员却很难快速准确定位根源主机,就更谈不上将它隔离了。端口与地址绑定技术使主机必须与某一端口进行绑定,也就是说,特定主机只有在某个特定端口下发出数据帧,才能被交换机接收并传输到网络上,如果这台主机移动到其他位置,则无法实现正常的连网。这样做看起来似乎对用户苛刻了一些,而且对于有大量使用便携机的员工的园区网并不适用,但基于安全管理的角度考虑,它却起到了至关重要的作用。
2、为了安全和便于管理,需要将 MAC 地址与端口进行绑定,即,MAC 地址与端口绑定后,该MAC 地址的数据流只能从绑定端口进入,不能从其他端口进入。该端口可以允许其他MAC 地址的数据流通过。但是如果绑定方式采用动态lock 的方式会使该端口的地址学习功能关闭,因此在取消lock 之前,其他MAC 的主机也不能从这个端口进入。
三、 实验设备
1、DCS-3926S 交换机1 台
2、PC 机2 台
3、Console 线1 根
4、直通网线 2 根
四、 实验拓扑
五、 实验要求
1、交换机 IP 地址为192.168.1.11/24,PC1 的地址为192.168.1.101/24;PC2 的地址为
192.168.1.102/24。
2、在交换机上作 MAC 与端口绑定;
3、PC1 在不同的端口上ping 交换机的IP,检验理论是否和实验一致。
4、PC2 在不同的端口上ping 交换机的IP,检验理论是否和实验一致。
六、 实验步骤
第一步:得到PC1主机的mac地址
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:\>ipconfig/all
Windows IP Configuration
Host Name . . . . . . . . . . . . : xuxp
Primary Dns Suffix . . . . . . . : digitalchina.com
Node Type . . . . . . . . . . . . : Broadcast
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
Ethernet adapter 本地连接:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Intel(R) PRO/100 VE Network Connecti
on
Physical Address. . . . . . . . . : 00-A0-D1-D1-07-FF
Dhcp Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Autoconfiguration IP Address. . . : 169.254.27.232
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :
C:\>
我们得到了PC1主机的mac地址为:00-A0-D1-D1-07-FF。
第二步:交换机全部恢复出厂设置,配置交换机的IP地址
switch(Config)#interface vlan 1
switch(Config-If-Vlan1)#ip address 192.168.1.11 255.255.255.0
switch(Config-If-Vlan1)#no shut
switch(Config-If-Vlan1)#exit
switch(Config)#
第三步:使能端口的MAC地址绑定功能
switch(Config)#interface ethernet 0/0/1
switch(Config-Ethernet0/0/1)#switchport port-security
switch(Config-Ethernet0/0/1)#
第四步:添加端口静态安全MAC地址,缺省端口最大安全MAC地址数为1
switch(Config-Ethernet0/0/1)#switchport port-security mac-address
00-a0-d1-d1-07-ff
验证配置:
switch#show port-security
Security Port MaxSecurityAddr CurrentAddr Security Action
(count) (count)
---------------------------------------------------------------------------
Ethernet0/0/1 1 1 Protect
---------------------------------------------------------------------------
Max Addresses limit per port :128
Total Addresses in System :1
switch#
switch#show port-security address
Security Mac Address Table
---------------------------------------------------------------------------
Vlan Mac Address Type Ports
1 00-a0-d1-d1-07-ff SecurityConfigured Ethernet0/0/1
---------------------------------------------------------------------------
Total Addresses in System :1
Max Addresses limit in System :128
switch#
第五步:使用ping命令验证
PC 端口 Ping 结果 原因
PC1 0/0/1 192.168.1.11 通
PC1 0/0/7 192.168.1.11 不通
PC2 0/0/1 192.168.1.11 通
PC2 0/0/7 192.168.1.11 通
第六步:在一个以太口上静态捆绑多个MAC
Switch(Config-Ethernet0/0/1)#switchport port-security maximum 4
Switch(Config-Ethernet0/0/1)#switchport port-security mac-address
aa-aa-aa-aa-aa-aa
Switch(Config-Ethernet0/0/1)#switchport port-security mac-address
aa-aa-aa-bb-bb-bb
Switch(Config-Ethernet0/0/1)#switchport port-security mac-address
aa-aa-aa-cc-cc-cc
验证配置:
switch#show port-security
Security Port MaxSecurityAddr CurrentAddr Security Action
(count) (count)
---------------------------------------------------------------------------
Ethernet0/0/1 4 4 Protect
---------------------------------------------------------------------------
Max Addresses limit per port :128
Total Addresses in System :4
switch#show port-security address
Security Mac Address Table
---------------------------------------------------------------------------
Vlan Mac Address Type Ports
1 00-a0-d1-d1-07-ff SecurityConfigured Ethernet0/0/1
1 aa-aa-aa-aa-aa-aa SecurityConfigured Ethernet0/0/1
1 aa-aa-aa-bb-bb-bb SecurityConfigured Ethernet0/0/1
1 aa-aa-aa-cc-cc-cc SecurityConfigured Ethernet0/0/1
---------------------------------------------------------------------------
Total Addresses in System :4
Max Addresses limit in System :128
switch#
上面使用的都是静态捆绑MAC的方法,下面介绍动态mac地址绑定的基本方法,首先清空
刚才做过的捆绑。
第七步:清空端口与MAC绑定
switch(Config)#
switch(Config)#int ethernet 0/0/1
switch(Config-Ethernet0/0/1)#no switchport port-security
switch(Config-Ethernet0/0/1)#exit
switch(Config)#exit
验证配置:
switch#show port-security
Security Port MaxSecurityAddr CurrentAddr Security Action
(count) (count)
---------------------------------------------------------------------------
Max Addresses limit per port :128
Total Addresses in System :0
第八步:使能端口的MAC地址绑定功能,动态学习MAC并转换
switch(Config)#interface ethernet 0/0/1
switch(Config-Ethernet0/0/1)#switchport port-security
switch(Config-Ethernet0/0/1)#switchport port-security lock
switch(Config-Ethernet0/0/1)#switchport port-security convert
switch(Config-Ethernet0/0/1)#exit
验证配置:
switch#show port-security address
Security Mac Address Table
---------------------------------------------------------------------------
Vlan Mac Address Type Ports
1 00-a0-d1-d1-07-ff SecurityConfigured Ethernet0/0/1
---------------------------------------------------------------------------
Total Addresses in System :1
Max Addresses limit in System :128
switch#
第九步:使用ping命令验证
PC 端口 Ping 结果 原因
PC1 0/0/1 192.168.1.11 通
PC1 0/0/7 192.168.1.11 不通
PC2 0/0/1 192.168.1.11 不通
PC2 0/0/7 192.168.1.11 通
七、 注意事项和排错
1、如果出现端口无法配置MAC地址绑定功能的情况,请检查交换机的端口是否运行了Spanning-tree,802.1x,端口汇聚或者端口已经配置为Trunk端口。MAC绑定在端口
上与这些配置是互斥的,如果该端口要打开MAC地址绑定功能,就必须首先确认端
口下的上述功能已经被关闭。
2、当动态学习MAC时,无法执行“convert”命令时,请检查PC机网卡是否和该端口正确连接。
3、端口Lock之后,该端口MAC地址学习功能被关闭,不允许其他的MAC进入该端口。
八、 配置序列

九、 课后练习
1、使用三台 PC 测试端口与MAC 绑定功能。
2、实现多个端口统一绑定。
十、 相关配置命令详解
switchport port-security
命令:switchport port-security
no switchport port-security
功能:使能端口MAC 地址绑定功能;本命令的no 操作为关闭端口MAC 地址绑定功能。
命令模式:端口配置模式
缺省情况:交换机端口不打开MAC 地址绑定功能。
使用指南:MAC 地址绑定功能与802.1x、Spanning Tree、端口汇聚功能存在互斥关系,因此如果要打开端口的MAC 地址绑定功能,就必须关闭端口上的802.1x、Spanning Tree、端口汇聚功能,且打开MAC 地址绑定功能的端口不能是Trunk 口。
举例:使能端口1 的MAC 地址绑定功能。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)#switchport port-security
switchport port-security convert
命令:switchport port-security convert
功能:将端口学习到的动态MAC 地址转化为静态安全MAC 地址。
命令模式:端口配置模式
使用指南:必须在安全端口锁定之后才能执行端口动态MAC 地址转化命令。执行此命令之后,端口学习到的动态MAC 地址将转化为静态安全MAC 地址。该命令没有配置保留。
举例:将端口1 的MAC 地址转化为静态安全MAC 地址。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)#switchport port-security convert
switchport port-security lock
命令:switchport port-security lock
no switchport port-security lock
功能:锁定端口。端口被锁定之后,端口的MAC地址学习功能将被关闭;本命令的no操作为恢复端口的MAC地址学习功能。
命令模式:端口配置模式
缺省情况:端口未锁定
使用指南:端口必须使能MAC 地址绑定功能之后才能执行端口锁定命令。执行端口锁定命令之后,端口将关闭动态MAC 学习功能。
举例:锁定端口1。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)#switchport port-security lock
switchport port-security timeout
命令:switchport port-security timeout <value>
no switchport port-security timeout
功能:设置端口锁定的定时器;本命令的no 操作为恢复缺省值。
参数:<value> 锁定时器时间间隔,取值范围为0~300s。
命令模式:端口配置模式
缺省情况:端口未打开端口锁定的定时器。
使用指南:端口锁定定时器功能是一种动态MAC 地址锁定功能,锁定定时器超时就执行MAC 地址锁定操作及将动态MAC 转换为安全MAC 地址的操作。端口必须先开启MAC 地址绑定功能后才能使用此命令。
举例:设置端口1 的锁定时器为30 秒。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)# switchport port-security timeout 30
switchport port-security mac-address
命令:switchport port-security mac-address <mac-address>
no switchport port-security mac-address <mac-address>
功能:添加静态安全MAC 地址;本命令的no 操作为删除静态安全MAC 地址。
命令模式:端口配置模式
参数:<mac-address>为添加/删除的MAC 地址。
使用指南:端口必须使能MAC 地址绑定功能之后才能添加端口静态安全MAC 地址。
举例:添加MAC 00-03-0F-FE-2E-D3 到端口1。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)#switchport port-security mac-address 00-03-0F-FE-2E-D3
clear port-security dynamic
命令:clear port-security dynamic [address <mac-addr> | interface <interface-id> ]
功能:清除指定端口的动态MAC 地址。
命令模式:特权配置模式
参数:<mac-addr>为MAC 地址;<interface-id>为指定的端口号。
使用指南:必须在安全端口锁定之后之后才能执行指定端口的动态MAC 清除操作。如果不指定端口、MAC 地址,则清除所有锁定的安全端口的动态MAC;如果仅指定端口,不指定MAC 地址,则清除指定端口的所有动态MAC 地址。
举例:删除端口1 动态MAC。
Switch#clear port-security dynamic interface Ethernet 0/0/1
switchport port-security maximum
命令:switchport port-security maximum <value>
no switchport port-security maximum
功能:设置端口最大安全MAC 地址数;本命令的no 操作为恢复最大安全地址数为1。
命令模式:端口配置模式
参数:<value> 端口静态安全MAC 地址上限,取值范围1~128。
缺省情况:端口最大安全MAC 地址数为1。
使用指南:端口必须使能MAC 地址绑定功能之后才能设置端口安全MAC 地址上限。如果端口静态安全MAC 地址数大于设置的最大安全MAC 地址数,则设置失败;必须删除端口的静态安全MAC 地址,直到端口静态安全MAC 地址数不大于设置的最大安全MAC 地址数,设置才会成功。
举例:设置端口1 安全MAC 地址上限为4。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)#switchport port-security maximum 4
switchport port-security violation
命令:switchport port-security violation {protect | shutdown}
no switchport port-security violation
功能:设置端口违背模式;本命令的no 操作为恢复违背模式为protect。
命令模式:端口配置模式
参数:protect 为保护模式;shutdown 为关闭模式。
缺省情况:端口违背模式为缺省为protect。
使用指南:端口必须使能MAC 地址绑定功能之后才能设置端口违背模式。如果端口违背模式设置为protect,那么当端口安全MAC 地址超过设置的端口安全MAC 上限的时候,端口仅仅关闭动态MAC 地址学习功能;如果端口违背模式设置为shutdown,那么当端口安全MAC地址超过设置的端口安全MAC上限的时候,端口将被关闭,用户可以通过no shutdown命令手工打开该端口。
举例:设置端口1 的违背模式为shutdown。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)#switchport port-security violation shutdown
show port-security
命令:show port-security
功能:显示全局安全端口配置情况。
命令模式:特权配置模式
缺省情况:交换机不显示安全端口配置情况。
使用指南:本命令显示交换机当前已经配置为安全端口的端口信息。
举例:
Switch#show port-security
Security Port MaxSecurityAddr CurrentAddr Security Action
(count) (count)
-----------------------------------------------------------------
Ethernet0/0/3 1 1 Protect
Ethernet0/0/4 10 1 Protect
Ethernet0/0/5 1 0 Protect
-----------------------------------------------------------------
Total Addresses in System :2
Max Addresses limit in System :128
显示信息 解释
Security Port 配置为安全端口的端口名
MaxSecurityAddr 安全端口设置的最大安全MAC 地址数
CurrentAddr 安全端口当前安全MAC 地址数
Security Action 端口设置的违背模式
Total Addresses in System 系统中当前安全MAC 地址数
Max Addresses limit in System 系统中最大安全MAC 地址数
show port-security interface
命令:show port-security interface <interface-id>
功能:显示安全端口配置情况。
命令模式:特权配置模式
参数:<interface-id> 指定的显示端口。
缺省情况:交换机不显示安全端口配置情况。
使用指南:本命令显示交换机安全端口的详细配置信息。
举例:
Switch#show port-security interface ethernet 0/0/1
Port Security :Enabled
Port status :Security Up
Violation mode :Protect
Maximum MAC Addresses :1
Total MAC Addresses :1
Configured MAC Addresses :1
Lock Timer is ShutDown
Mac-Learning function is : Opened
显示信息解释
Port Security : 端口是否使能为安全端口
Port status : 端口安全状态
Violation mode : 端口设置的违背模式
Maximum MAC Addresses : 端口设置的安全MAC 地址上限
Total MAC Addresses : 端口当前安全MAC 地址数
Configured MAC Addresses : 端口静态配置的安全MAC 地址数
Lock Timer 端口是否开启锁定的定时器(定时器时间)
Mac-Learning function 端口MAC 地址学习功能是否打开
show port-security address
命令:show port-security address [interface <interface-id>]
功能:显示端口安全MAC 地址。
命令模式:特权配置模式
参数:<interface-id> 指定的显示端口。
使用指南:本命令显示端口安全MAC 地址信息,如果不指定端口则显示所有端口安全MAC地址。显示内容举例如下:
Switch#show port-security address interface ethernet 0/0/1
Security Mac Address Table
------------------------------------------------------------------------
Vlan Mac Address Type Ports
1 0000.0000.1111 SecureConfigured Ethernet0/0/3
------------------------------------------------------------------------
Total Addresses :1
显示信息 解释
Vlan 安全MAC 地址的VLAN ID
Mac Address 安全MAC 地址
Type 安全MAC 地址类型
Ports 安全MAC 地址所属端口
Total Addresses 系统中当前安全MAC 地址数 

 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

实验十八、交换机端口与MAC 绑定
一、 实验目的
1、了解什么是交换机的 MAC 绑定功能;
2、熟练掌握 MAC 与端口绑定的静态、动态方式。
二、 应用环境
1、当网络中某机器由于中毒进而引发大量的广播数据包在网络中洪泛时,网络管理员的唯一想法就是尽快地找到根源主机并把它从网络中暂时隔离开。当网络的布置很随意时,任何用户只要插上网线,在任何位置都能够上网,这虽然使正常情况下的大多数用户很满意,但一旦发生网络故障,网管人员却很难快速准确定位根源主机,就更谈不上将它隔离了。端口与地址绑定技术使主机必须与某一端口进行绑定,也就是说,特定主机只有在某个特定端口下发出数据帧,才能被交换机接收并传输到网络上,如果这台主机移动到其他位置,则无法实现正常的连网。这样做看起来似乎对用户苛刻了一些,而且对于有大量使用便携机的员工的园区网并不适用,但基于安全管理的角度考虑,它却起到了至关重要的作用。
2、为了安全和便于管理,需要将 MAC 地址与端口进行绑定,即,MAC 地址与端口绑定后,该MAC 地址的数据流只能从绑定端口进入,不能从其他端口进入。该端口可以允许其他MAC 地址的数据流通过。但是如果绑定方式采用动态lock 的方式会使该端口的地址学习功能关闭,因此在取消lock 之前,其他MAC 的主机也不能从这个端口进入。
三、 实验设备
1、DCS-3926S 交换机1 台
2、PC 机2 台
3、Console 线1 根
4、直通网线 2 根
四、 实验拓扑
五、 实验要求
1、交换机 IP 地址为192.168.1.11/24,PC1 的地址为192.168.1.101/24;PC2 的地址为
192.168.1.102/24。
2、在交换机上作 MAC 与端口绑定;
3、PC1 在不同的端口上ping 交换机的IP,检验理论是否和实验一致。
4、PC2 在不同的端口上ping 交换机的IP,检验理论是否和实验一致。
六、 实验步骤
第一步:得到PC1主机的mac地址
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:\>ipconfig/all
Windows IP Configuration
Host Name . . . . . . . . . . . . : xuxp
Primary Dns Suffix . . . . . . . : digitalchina.com
Node Type . . . . . . . . . . . . : Broadcast
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
Ethernet adapter 本地连接:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Intel(R) PRO/100 VE Network Connecti
on
Physical Address. . . . . . . . . : 00-A0-D1-D1-07-FF
Dhcp Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Autoconfiguration IP Address. . . : 169.254.27.232
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :
C:\>
我们得到了PC1主机的mac地址为:00-A0-D1-D1-07-FF。
第二步:交换机全部恢复出厂设置,配置交换机的IP地址
switch(Config)#interface vlan 1
switch(Config-If-Vlan1)#ip address 192.168.1.11 255.255.255.0
switch(Config-If-Vlan1)#no shut
switch(Config-If-Vlan1)#exit
switch(Config)#
第三步:使能端口的MAC地址绑定功能
switch(Config)#interface ethernet 0/0/1
switch(Config-Ethernet0/0/1)#switchport port-security
switch(Config-Ethernet0/0/1)#
第四步:添加端口静态安全MAC地址,缺省端口最大安全MAC地址数为1
switch(Config-Ethernet0/0/1)#switchport port-security mac-address
00-a0-d1-d1-07-ff
验证配置:
switch#show port-security
Security Port MaxSecurityAddr CurrentAddr Security Action
(count) (count)
---------------------------------------------------------------------------
Ethernet0/0/1 1 1 Protect
---------------------------------------------------------------------------
Max Addresses limit per port :128
Total Addresses in System :1
switch#
switch#show port-security address
Security Mac Address Table
---------------------------------------------------------------------------
Vlan Mac Address Type Ports
1 00-a0-d1-d1-07-ff SecurityConfigured Ethernet0/0/1
---------------------------------------------------------------------------
Total Addresses in System :1
Max Addresses limit in System :128
switch#
第五步:使用ping命令验证
PC 端口 Ping 结果 原因
PC1 0/0/1 192.168.1.11 通
PC1 0/0/7 192.168.1.11 不通
PC2 0/0/1 192.168.1.11 通
PC2 0/0/7 192.168.1.11 通
第六步:在一个以太口上静态捆绑多个MAC
Switch(Config-Ethernet0/0/1)#switchport port-security maximum 4
Switch(Config-Ethernet0/0/1)#switchport port-security mac-address
aa-aa-aa-aa-aa-aa
Switch(Config-Ethernet0/0/1)#switchport port-security mac-address
aa-aa-aa-bb-bb-bb
Switch(Config-Ethernet0/0/1)#switchport port-security mac-address
aa-aa-aa-cc-cc-cc
验证配置:
switch#show port-security
Security Port MaxSecurityAddr CurrentAddr Security Action
(count) (count)
---------------------------------------------------------------------------
Ethernet0/0/1 4 4 Protect
---------------------------------------------------------------------------
Max Addresses limit per port :128
Total Addresses in System :4
switch#show port-security address
Security Mac Address Table
---------------------------------------------------------------------------
Vlan Mac Address Type Ports
1 00-a0-d1-d1-07-ff SecurityConfigured Ethernet0/0/1
1 aa-aa-aa-aa-aa-aa SecurityConfigured Ethernet0/0/1
1 aa-aa-aa-bb-bb-bb SecurityConfigured Ethernet0/0/1
1 aa-aa-aa-cc-cc-cc SecurityConfigured Ethernet0/0/1
---------------------------------------------------------------------------
Total Addresses in System :4
Max Addresses limit in System :128
switch#
上面使用的都是静态捆绑MAC的方法,下面介绍动态mac地址绑定的基本方法,首先清空刚才做过的捆绑。
第七步:清空端口与MAC绑定
switch(Config)#
switch(Config)#int ethernet 0/0/1
switch(Config-Ethernet0/0/1)#no switchport port-security
switch(Config-Ethernet0/0/1)#exit
switch(Config)#exit
验证配置:
switch#show port-security
Security Port MaxSecurityAddr CurrentAddr Security Action
(count) (count)
---------------------------------------------------------------------------
---------------------------------------------------------------------------
Max Addresses limit per port :128
Total Addresses in System :0
第八步:使能端口的MAC地址绑定功能,动态学习MAC并转换
switch(Config)#interface ethernet 0/0/1
switch(Config-Ethernet0/0/1)#switchport port-security
switch(Config-Ethernet0/0/1)#switchport port-security lock
switch(Config-Ethernet0/0/1)#switchport port-security convert
switch(Config-Ethernet0/0/1)#exit
验证配置:
switch#show port-security address
Security Mac Address Table
---------------------------------------------------------------------------
Vlan Mac Address Type Ports
1 00-a0-d1-d1-07-ff SecurityConfigured Ethernet0/0/1
---------------------------------------------------------------------------
Total Addresses in System :1
Max Addresses limit in System :128
switch#
第九步:使用ping命令验证
PC 端口 Ping 结果 原因
PC1 0/0/1 192.168.1.11 通
PC1 0/0/7 192.168.1.11 不通
PC2 0/0/1 192.168.1.11 不通
PC2 0/0/7 192.168.1.11 通
七、 注意事项和排错
1、如果出现端口无法配置MAC地址绑定功能的情况,请检查交换机的端口是否运行了Spanning-tree,802.1x,端口汇聚或者端口已经配置为Trunk端口。MAC绑定在端口
上与这些配置是互斥的,如果该端口要打开MAC地址绑定功能,就必须首先确认端
口下的上述功能已经被关闭。
2、当动态学习MAC时,无法执行“convert”命令时,请检查PC机网卡是否和该端口正确连接。
3、端口Lock之后,该端口MAC地址学习功能被关闭,不允许其他的MAC进入该端口。
八、 配置序列

九、 课后练习
1、使用三台 PC 测试端口与MAC 绑定功能。
2、实现多个端口统一绑定。
十、 相关配置命令详解
switchport port-security
命令:switchport port-security
no switchport port-security
功能:使能端口MAC 地址绑定功能;本命令的no 操作为关闭端口MAC 地址绑定功能。
命令模式:端口配置模式
缺省情况:交换机端口不打开MAC 地址绑定功能。
使用指南:MAC 地址绑定功能与802.1x、Spanning Tree、端口汇聚功能存在互斥关系,因此如果要打开端口的MAC 地址绑定功能,就必须关闭端口上的802.1x、Spanning Tree、端口汇聚功能,且打开MAC 地址绑定功能的端口不能是Trunk 口。
举例:使能端口1 的MAC 地址绑定功能。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)#switchport port-security
switchport port-security convert
命令:switchport port-security convert
功能:将端口学习到的动态MAC 地址转化为静态安全MAC 地址。
命令模式:端口配置模式
使用指南:必须在安全端口锁定之后才能执行端口动态MAC 地址转化命令。执行此命令之后,端口学习到的动态MAC 地址将转化为静态安全MAC 地址。该命令没有配置保留。
举例:将端口1 的MAC 地址转化为静态安全MAC 地址。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)#switchport port-security convert
switchport port-security lock
命令:switchport port-security lock
no switchport port-security lock
功能:锁定端口。端口被锁定之后,端口的MAC地址学习功能将被关闭;本命令的no操作为恢复端口的MAC地址学习功能。
命令模式:端口配置模式
缺省情况:端口未锁定
使用指南:端口必须使能MAC 地址绑定功能之后才能执行端口锁定命令。执行端口锁定命令之后,端口将关闭动态MAC 学习功能。
举例:锁定端口1。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)#switchport port-security lock
switchport port-security timeout
命令:switchport port-security timeout <value>
no switchport port-security timeout
功能:设置端口锁定的定时器;本命令的no 操作为恢复缺省值。
参数:<value> 锁定时器时间间隔,取值范围为0~300s。
命令模式:端口配置模式
缺省情况:端口未打开端口锁定的定时器。
使用指南:端口锁定定时器功能是一种动态MAC 地址锁定功能,锁定定时器超时就执行MAC 地址锁定操作及将动态MAC 转换为安全MAC 地址的操作。端口必须先开启MAC 地址绑定功能后才能使用此命令。
举例:设置端口1 的锁定时器为30 秒。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)# switchport port-security timeout 30
switchport port-security mac-address
命令:switchport port-security mac-address <mac-address>
no switchport port-security mac-address <mac-address>
功能:添加静态安全MAC 地址;本命令的no 操作为删除静态安全MAC 地址。
命令模式:端口配置模式参数:<mac-address>为添加/删除的MAC 地址。
使用指南:端口必须使能MAC 地址绑定功能之后才能添加端口静态安全MAC 地址。
举例:添加MAC 00-03-0F-FE-2E-D3 到端口1。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)#switchport port-security mac-address 00-03-0F-FE-2E-D3
clear port-security dynamic
命令:clear port-security dynamic [address <mac-addr> | interface <interface-id> ]
功能:清除指定端口的动态MAC 地址。
命令模式:特权配置模式
参数:<mac-addr>为MAC 地址;<interface-id>为指定的端口号。
使用指南:必须在安全端口锁定之后之后才能执行指定端口的动态MAC 清除操作。如果不指定端口、MAC 地址,则清除所有锁定的安全端口的动态MAC;如果仅指定端口,不指定MAC 地址,则清除指定端口的所有动态MAC 地址。
举例:删除端口1 动态MAC。
Switch#clear port-security dynamic interface Ethernet 0/0/1
switchport port-security maximum
命令:switchport port-security maximum <value>
no switchport port-security maximum
功能:设置端口最大安全MAC 地址数;本命令的no 操作为恢复最大安全地址数为1。
命令模式:端口配置模式
参数:<value> 端口静态安全MAC 地址上限,取值范围1~128。
缺省情况:端口最大安全MAC 地址数为1。
使用指南:端口必须使能MAC 地址绑定功能之后才能设置端口安全MAC 地址上限。如果端口静态安全MAC 地址数大于设置的最大安全MAC 地址数,则设置失败;必须删除端口的静态安全MAC 地址,直到端口静态安全MAC 地址数不大于设置的最大安全MAC 地址数,设置才会成功。
举例:设置端口1 安全MAC 地址上限为4。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)#switchport port-security maximum 4
switchport port-security violation
命令:switchport port-security violation {protect | shutdown}
no switchport port-security violation
功能:设置端口违背模式;本命令的no 操作为恢复违背模式为protect。
命令模式:端口配置模式
参数:protect 为保护模式;shutdown 为关闭模式。
缺省情况:端口违背模式为缺省为protect。
使用指南:端口必须使能MAC 地址绑定功能之后才能设置端口违背模式。如果端口违背模式设置为protect,那么当端口安全MAC 地址超过设置的端口安全MAC 上限的时候,端口仅仅关闭动态MAC 地址学习功能;如果端口违背模式设置为shutdown,那么当端口安全MAC地址超过设置的端口安全MAC上限的时候,端口将被关闭,用户可以通过no shutdown命令手工打开该端口。
举例:设置端口1 的违背模式为shutdown。
Switch(Config)#interface Ethernet 0/0/1
Switch(Config-Ethernet0/0/1)#switchport port-security violation shutdown
show port-security
命令:show port-security
功能:显示全局安全端口配置情况。
命令模式:特权配置模式
缺省情况:交换机不显示安全端口配置情况。
使用指南:本命令显示交换机当前已经配置为安全端口的端口信息。
举例:
Switch#show port-security
Security Port MaxSecurityAddr CurrentAddr Security Action
(count) (count)
-----------------------------------------------------------------
Ethernet0/0/3 1 1 Protect
Ethernet0/0/4 10 1 Protect
Ethernet0/0/5 1 0 Protect
-----------------------------------------------------------------
Total Addresses in System :2
Max Addresses limit in System :128
显示信息 解释
Security Port 配置为安全端口的端口名
MaxSecurityAddr 安全端口设置的最大安全MAC 地址数
CurrentAddr 安全端口当前安全MAC 地址数
Security Action 端口设置的违背模式
Total Addresses in System 系统中当前安全MAC 地址数
Max Addresses limit in System 系统中最大安全MAC 地址数
show port-security interface
命令:show port-security interface <interface-id>
功能:显示安全端口配置情况。
命令模式:特权配置模式
参数:<interface-id> 指定的显示端口。
缺省情况:交换机不显示安全端口配置情况。
使用指南:本命令显示交换机安全端口的详细配置信息。
举例:
Switch#show port-security interface ethernet 0/0/1
Port Security :Enabled
Port status :Security Up
Violation mode :Protect
Maximum MAC Addresses :1
Total MAC Addresses :1
Configured MAC Addresses :1
Lock Timer is ShutDown
Mac-Learning function is : Opened
显示信息解释
Port Security : 端口是否使能为安全端口
Port status : 端口安全状态
Violation mode : 端口设置的违背模式
Maximum MAC Addresses : 端口设置的安全MAC 地址上限
Total MAC Addresses : 端口当前安全MAC 地址数
Configured MAC Addresses : 端口静态配置的安全MAC 地址数
Lock Timer 端口是否开启锁定的定时器(定时器时间)
Mac-Learning function 端口MAC 地址学习功能是否打开
show port-security address
命令:show port-security address [interface <interface-id>]
功能:显示端口安全MAC 地址。
命令模式:特权配置模式
参数:<interface-id> 指定的显示端口。
使用指南:本命令显示端口安全MAC 地址信息,如果不指定端口则显示所有端口安全MAC地址。显示内容举例如下:
Switch#show port-security address interface ethernet 0/0/1
Security Mac Address Table
------------------------------------------------------------------------
Vlan Mac Address Type Ports
1 0000.0000.1111 SecureConfigured Ethernet0/0/3
------------------------------------------------------------------------
Total Addresses :1
显示信息 解释
Vlan 安全MAC 地址的VLAN ID
Mac Address 安全MAC 地址
Type 安全MAC 地址类型
Ports 安全MAC 地址所属端口
Total Addresses 系统中当前安全MAC 地址数