思科交换机 配置端口安全 命令与配置示例
限制终端接入口允许学习的 MAC 地址数量,并选择可观察、可恢复的违规处理方式,降低私接交换设备风险。
进阶
15 分钟
内容技术审核组 · 2026-08-01 已核验
正在读取学习状态...
配置目标
在办公接入口启用端口安全,把最大 MAC 数量限制为 2,并采用限制或丢弃而非立即关闭端口的初始策略。
应用场景
固定办公工位只应连接电脑和 IP 电话,需要阻止私接小交换机带来的大量未知终端,同时避免首次上线直接大面积关端口。
前置知识
- 接口为 Access 模式
- 已盘点合法终端 MAC 数量
- 具备告警与恢复流程
平台:IOS
版本:15.2 及以上
环境:IOS 15.2 及以上 课程规则引擎与目标平台命令参考复核环境
执行前检查
核对设备与配置基线确认目标设备、接口、业务范围、当前状态和可用回滚基线,再执行命令。
完整配置
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。
多厂商命令对比
按配置步骤横向查看不同厂商写法;空白项表示该厂商采用了不同的命令流程。
| 配置步骤 | 思科命令 | 华三命令 | 华为命令 | 锐捷命令 |
|---|---|---|---|---|
| 步骤 1 | configure terminal | system-view | system-view | configure terminal |
| 步骤 2 | interface GigabitEthernet0/1 | port-security enable | interface GigabitEthernet 0/0/1 | interface GigabitEthernet 0/1 |
| 步骤 3 | switchport mode access | interface GigabitEthernet 1/0/1 | port-security enable | switchport mode access |
| 步骤 4 | switchport port-security | port-security max-mac-count 2 | port-security max-mac-num 2 | switchport port-security |
| 步骤 5 | switchport port-security maximum 2 | port-security port-mode autolearn | port-security protect-action restrict | switchport port-security maximum 2 |
| 步骤 6 | switchport port-security violation restrict | 该厂商步骤不同 | 该厂商步骤不同 | switchport port-security violation restrict |
| 步骤 7 | switchport 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 观察。