Free tech learning for everyone, from basics to pro

Routing & SwitchingNetworking SecurityIntermediate

Port security in depth: maximum MACs, sticky learning, violation modes and recovery

In short: Port security lets a switch port accept frames only from a set number of MAC addresses, either typed in or learned. When an unknown MAC appears beyond that limit, the port can shut down, drop and log, or drop silently. Sticky learning saves learned addresses into the configuration, and err-disable recovery can bring ports back automatically.

What port security does

Out of the box, any device plugged into an access port can use the network. Port security adds a simple rule: only these MAC addresses, and no more than this many. It stops someone unplugging a desk PC and connecting their own laptop or a small home switch, and it limits MAC flooding attacks that try to fill the switch's MAC address table.

It only works on static access (or trunk) ports, not on ports set to dynamic desirable or auto, so set switchport mode access first.

Port security violation modesshutdown (default)Port goes err-disabledSyslog and SNMP trapViolation counter risesNeeds recoveryprotect / restrictPort stays upUnknown MACs droppedrestrict logs and countsprotect is silent
Figure 1. Shutdown is the safest default for user ports; restrict suits shared desks where a lockout would cause support calls.

A typical desk port

interface GigabitEthernet1/0/10
 description Desk 2.14
 switchport mode access
 switchport access vlan 20
 switchport voice vlan 120
 switchport port-security
 switchport port-security maximum 3
 switchport port-security maximum 1 vlan access
 switchport port-security mac-address sticky
 switchport port-security violation restrict
 switchport port-security aging time 1440
 switchport port-security aging type inactivity
 spanning-tree portfast

Understanding each line

  • maximum 3: an IP phone with a PC behind it needs more than one address. The phone often appears in both the voice and data VLANs (CDP or LLDP traffic), so 3 is a common safe value, with maximum 1 vlan access limiting the PC side to one device.
  • sticky: learned MAC addresses are written into the running configuration as switchport port-security mac-address sticky 0011.2233.4455. Save the configuration, or they are lost on reload.
  • violation: shutdown err-disables the port, restrict drops unknown MACs and logs them, protect drops them without logging.
  • aging: removes learned addresses after a period, so hot desks do not fill up permanently. inactivity only ages addresses that have stopped sending traffic.

Recovering err-disabled ports

With the default shutdown mode, a violation leaves the port in err-disabled state until someone fixes it. You can recover it by hand with shutdown then no shutdown, or let the switch try automatically:

errdisable recovery cause psecure-violation
errdisable recovery interval 600

Automatic recovery is convenient, but if the cause is still plugged in, the port will simply go down again every ten minutes. Monitor the logs.

Checking it

  • show port-security: a summary of all secured ports, their maximum, current count and violations.
  • show port-security interface g1/0/10: the status, violation mode, last source MAC and counters.
  • show port-security address: every secure MAC and its type (configured, sticky or dynamic).

Port security or 802.1X?

Port security trusts MAC addresses, which are easy to copy. A visitor who reads the label on a printer can set their laptop to the same MAC. For real identity checks use 802.1X, with MAC Authentication Bypass for devices that cannot do 802.1X. Port security is still useful as a cheap, extra limit on ports where 802.1X is not possible, and as a guard against MAC flooding.

Real-world example: the classroom switch

Students in a computer lab kept connecting a small travel router under the desks to share one cable. It caused IP conflicts and rogue DHCP. The school set port security with a maximum of one MAC and violation restrict on the lab ports. The travel routers stopped working, the logs showed which desk each attempt came from, and no ports were left down during lessons.

Practice

Which port security violation mode drops unknown MACs, increments the counter and logs a message, but leaves the port up?
restrict.
Why is a maximum of more than one MAC often needed on a desk port with an IP phone?
The phone and the PC both have MAC addresses, and the phone can appear in both the voice and data VLANs.
What happens to sticky MAC addresses after a reload if the configuration was not saved?
They are lost, because sticky addresses are stored in the running configuration.

Key takeaways

  • Port security limits how many and which MAC addresses can use a port.
  • Sticky learning writes learned MACs into the running configuration.
  • Violation modes are shutdown, restrict and protect.
  • MACs are easy to spoof; use 802.1X for real identity checks.
Tracks your learning on this device only. See your progress on the learning paths.
Verified author✓ Valid
Issued to
Mohamed Fayas
Role
Founder of FM TecHub
Credentials
CCNABTEC HND Network EngineeringBEng (Hons) Computer Networking & Cloud Security
Issuer
FM TecHub · fmtechub.com

He writes practical, plain-English guides on networking, security and cloud.

More about the author →

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Email updates: coming soon

New notes, tools and security news from FM TecHub by email, explained simply. Free. We're not collecting email addresses yet; see our privacy policy.

PortServiceStatus
SMTP/25Email updatesopening soon

Meanwhile, stay connected: