思科交换机 配置端口安全 命令与配置示例

限制终端接入口允许学习的 MAC 地址数量,并选择可观察、可恢复的违规处理方式,降低私接交换设备风险。

进阶
15 分钟
内容技术审核组 · 2026-08-01 已核验
正在读取学习状态...

配置目标

在办公接入口启用端口安全,把最大 MAC 数量限制为 2,并采用限制或丢弃而非立即关闭端口的初始策略。

应用场景

固定办公工位只应连接电脑和 IP 电话,需要阻止私接小交换机带来的大量未知终端,同时避免首次上线直接大面积关端口。

前置知识

  • 接口为 Access 模式
  • 已盘点合法终端 MAC 数量
  • 具备告警与恢复流程
平台:IOS
版本:15.2 及以上
环境:IOS 15.2 及以上 课程规则引擎与目标平台命令参考复核环境
思科在静态 Access 接口启用 Port Security,限制数量并使用 sticky 学习。作者:命令内容组

执行前检查

核对设备与配置基线

确认目标设备、接口、业务范围、当前状态和可用回滚基线,再执行命令。

完整配置
configure terminal
interface GigabitEthernet0/1
switchport mode access
switchport port-security
switchport port-security maximum 2
switchport port-security violation restrict
switchport port-security mac-address sticky

逐行说明

configure terminal

进入全局配置模式。

interface GigabitEthernet0/1

进入办公终端接入口。

switchport mode access

把接口固定为 Access 模式。

switchport port-security

启用端口安全。

switchport port-security maximum 2

允许最多 2 个安全 MAC。

switchport port-security violation restrict

丢弃违规报文并增加违规计数。

switchport port-security mac-address sticky

把动态学习地址记录为 Sticky Secure MAC。

多厂商命令对比

按配置步骤横向查看不同厂商写法;空白项表示该厂商采用了不同的命令流程。

配置步骤思科命令华三命令华为命令锐捷命令
步骤 1configure terminalsystem-viewsystem-viewconfigure terminal
步骤 2interface GigabitEthernet0/1port-security enableinterface GigabitEthernet 0/0/1interface GigabitEthernet 0/1
步骤 3switchport mode accessinterface GigabitEthernet 1/0/1port-security enableswitchport mode access
步骤 4switchport port-securityport-security max-mac-count 2port-security max-mac-num 2switchport port-security
步骤 5switchport port-security maximum 2port-security port-mode autolearnport-security protect-action restrictswitchport port-security maximum 2
步骤 6switchport port-security violation restrict该厂商步骤不同该厂商步骤不同switchport port-security violation restrict
步骤 7switchport port-security mac-address sticky该厂商步骤不同该厂商步骤不同switchport port-security mac-address sticky

验证配置

show port-security interface GigabitEthernet0/1

核对状态、最大数量、当前数量和违规计数。

输出字段解读

show port-security interface GigabitEthernet0/1

关注该输出中与“核对状态、最大数量、当前数量和违规计数。”对应的状态、计数和异常字段,并与变更前基线比较。

回滚方法

no switchport port-security mac-address sticky

按变更前基线撤销本主题配置;执行后复核配置差异和业务路径。

常见错误、影响与恢复

采用 shutdown 动作后接口 err-disabled

影响:所有合法和违规终端都会失去网络连接。

恢复:先定位并移除违规源,再按批准流程恢复接口;初始上线优先使用 restrict 观察。

关联学习内容