会议室、前台、宿舍等边缘接入口需要限制私接交换机或随意换机;或者端口突然 err-disabled,日志提示 port-security violation。先分清是 MAC 超限、违规设备还是粘滞表项过期,再决定恢复方式。
端口安全主要用在 access 口;Cisco 也支持静态 trunk 口,但限制和平台差异较大,核心是“允许哪些 MAC、最多几个、超限怎么办”。Cisco 的 secure MAC 分为静态、动态和 sticky:sticky 会把学到的 MAC 写入运行配置,保存后重启仍在;动态表项重启后丢失。默认最大 MAC 数通常是 1,默认违例动作通常是 shutdown,具体以设备版本为准。
违例动作有三种语义:protect 静默丢弃新 MAC 的流量,不告警;restrict 丢弃并记录日志/SNMP;shutdown 把端口置为 err-disabled,业务完全中断。Huawei 对应 protect-action protect/restrict/shutdown,H3C 用 intrusion-mode 控制。
变更影响与回滚:启用端口安全或修改最大 MAC 数会立即限制新设备接入;shutdown 违例会让端口彻底不可用,替换终端后也可能触发。操作前确认端口下挂设备数量(含 IP 电话),保留 Console;回滚是删除端口安全配置、清除 sticky/动态表项并 no shutdown。
# Cisco IOS
interface GigabitEthernet1/0/1
switchport mode access
switchport port-security
switchport port-security maximum 2
switchport port-security mac-address sticky
switchport port-security violation restrict
show port-security interface GigabitEthernet1/0/1
show port-security address
clear port-security dynamic interface GigabitEthernet1/0/1
# Huawei VRP
port-security enable
interface GigabitEthernet0/0/1
port-security enable
port-security max-mac-num 2
port-security mac-address sticky
port-security protect-action restrict
display port-security
# H3C Comware
port-security enable
interface GigabitEthernet1/0/1
port-security max-mac-count 2
port-security port-mode autolearn
port-security intrusion-mode blockmac
display port-security
# Juniper Junos(EX,以设备版本为准)
set switch-options interface ge-0/0/1 interface-mac-limit 2 packet-action drop
Cisco 端口因违例 shutdown 后,先 shutdown 再 no shutdown,必要时清除动态/粘滞表项;也可以配置 errdisable recovery cause psecure-violation 自动恢复,但自动恢复会让问题端口反复抖动,生产环境慎用。
protect 静默丢包,没有日志,排障时容易误判为链路或 VLAN 问题。shutdown 违例后只清计数不恢复端口,业务仍然中断。
小程序版
微信扫码,随时查命令