Oamc

端口安全与防私接 · 修改记录

这是 network/port-security.md 的每次改动内容,按时间从新到旧。其他文章一起提交的部分不会出现在这里。

#1 2026-10-18 +82 −0 docs: 新增 23 篇网络/ Linux / Windows / 服务器文章 be5956e
 new file mode 100644
 @@ -0,0 +1,82 @@
+---
+tags: [端口安全, DHCP Snooping, 防私接]
+order: 70
+---
+
+# 端口安全与防私接
+
+## 场景
+
+办公区有人私接路由器、乱插交换机,导致地址冲突、DHCP 被抢、环路。
+要限制每个口能学几个 MAC,只信任上行的 DHCP。
+
+## 前置条件
+
+- 接入交换机支持端口安全
+- 上行口和对端设备确认好(哪个口是信任口)
+- 先摸清每个工位是单设备还是接了话机 + 电脑
+
+## 配置
+
+### H3C
+
+```text
+system-view
+port-security enable
+
+interface GigabitEthernet1/0/1
+ port-security max-mac-count 2
+ port-security port-mode autolearn
+ quit
+
+# 只信任上行口
+dhcp snooping enable
+interface GigabitEthernet1/0/24
+ dhcp snooping trust
+ quit
+```
+
+### 华为
+
+```text
+system-view
+port-security enable
+
+interface GigabitEthernet0/0/1
+ port-security max-mac-num 2
+ port-security protect-action restrict
+ quit
+
+dhcp snooping enable
+interface GigabitEthernet0/0/24
+ dhcp snooping trusted
+ quit
+```
+
+## 验证
+
+```text
+display port-security
+display port-security interface GigabitEthernet1/0/1
+display dhcp snooping
+```
+
+超过 MAC 上限时端口会进入 `restrict` 或 `shutdown` 状态,日志里有记录。
+
+## 回滚
+
+```text
+interface GigabitEthernet1/0/1
+ undo port-security max-mac-count
+ undo port-security port-mode
+```
+
+端口被 shutdown 的先用 `undo shutdown` 恢复,再查是谁插了多设备。
+
+## 坑
+
+- **上限设太小会把正常设备锁掉**:话机 + 电脑就是两个 MAC,还有 IP 电话串接。
+- `autolearn` 模式下换网卡要手动清 MAC 表,否则新设备上不了网。
+- DHCP snooping 的信任口配错方向,整个 VLAN 都拿不到地址。
+- 私接路由器如果是 NAT 模式,看不太出来,只能靠 DHCP snooping 挡。
+- 配完先在一个办公室试点,别一次推全网。