Port Security – Concepts & Overview
1. What Is Port Security and Why Does It Matter?
Port security is a Layer 2 feature on Cisco switches that restricts which devices are allowed to communicate through a switch port, based on their MAC address. It limits the number of MAC addresses that can be learned on a port, and optionally hard-codes which specific MAC addresses are permitted. When an unauthorised device is detected, the switch takes a configurable action — from silently discarding frames to completely shutting down the port.
Without port security, any device plugged into a switch port immediately gains network access. This opens the door to several Layer 2 attacks including MAC flooding (CAM table overflow), rogue device attachment, and unauthorised network access from unmanaged hubs or switches connected to a single access port.
| Threat | How Port Security Mitigates It |
|---|---|
| MAC flooding / CAM table overflow | Limits the number of MAC addresses learnable on a port. A flooding tool sending thousands of fake MACs hits the limit and triggers a violation action before the CAM table is overwhelmed |
| Rogue device attachment | A specific (sticky or static) MAC binding ensures only the authorised device's MAC is accepted on a port — any other MAC triggers a violation |
| Unauthorised hub/switch connection | Setting maximum MAC count to 1 prevents a user from plugging in a hub or unmanaged switch, which would introduce multiple MACs |
| Unauthorised VLAN access | Combined with sticky MAC, port security ensures a port only ever serves the specific workstation assigned to it |
Related pages: Violation Modes – Full Guide | MAC Addresses | MAC Address Table | show mac-address-table | VLANs Overview | DHCP Snooping | DHCP Snooping & DAI Lab | Port Security & Sticky MAC Lab
2. How MAC Address Learning Works on a Switch
Before understanding port security, it is essential to understand how a switch normally learns MAC addresses. Every switch maintains a MAC address table (also called the CAM table — Content Addressable Memory) that maps MAC addresses to the port on which they were last seen.
Normal MAC learning process (no port security):
Step 1 — PC-A (MAC: AA:AA:AA:AA:AA:AA) sends a frame out of port Fa0/1.
Step 2 — Switch reads the SOURCE MAC from the incoming frame.
Step 3 — Switch records: MAC AA:AA:AA:AA:AA:AA → Port Fa0/1 in the CAM table.
Step 4 — Entry ages out after the aging timer (default: 300 seconds) if no
frames are received from that MAC.
Switch# show mac address-table
Vlan Mac Address Type Ports
---- ----------- -------- -----
10 aa:aa:aa:aa:aa:aa DYNAMIC Fa0/1
10 bb:bb:bb:bb:bb:bb DYNAMIC Fa0/2
Without port security:
- Any MAC address is learned dynamically on any port
- No limit on how many MACs can be learned per port
- Any device plugged in immediately gets Layer 2 forwarding
The MAC Flooding Attack
Attack scenario — without port security: Attacker runs a tool (e.g., macof) that generates thousands of frames with random fake source MAC addresses per second. Switch CAM table fills up: ┌────────────────────────────────────────────────────────┐ │ CAM Table (e.g., 8,192 entry limit) │ │ 11:22:33:44:55:66 → Fa0/1 (real entry) │ │ AA:BB:CC:DD:EE:01 → Fa0/1 (fake) │ │ AA:BB:CC:DD:EE:02 → Fa0/1 (fake) │ │ ... 8,190 more fake entries ... │ │ TABLE FULL — legitimate MACs cannot be added │ └────────────────────────────────────────────────────────┘ Once full: switch cannot look up legitimate destination MACs. Unknown unicast frames are FLOODED to all ports (like a hub). Attacker captures all traffic on the segment — full Layer 2 eavesdrop. Port security fix: limit Fa0/1 to maximum 1 MAC address. Any frame after the first unique MAC triggers a violation.
3. Port Security Prerequisites and Restrictions
Port security has specific requirements that must be met before it can be enabled. Attempting to configure port security on an ineligible port will produce an error.
| Requirement / Restriction | Detail |
|---|---|
| Access port only | Port security can only be enabled on access ports
(switchport mode access). It cannot be configured on
trunk ports, routed ports, or EtherChannel member ports |
| Static access mode required | The port must be manually set to access mode. Dynamic ports
(DTP negotiating) do not support port security — disable DTP with
switchport nonegotiate or set mode explicitly |
| Not on SPAN destination ports | Ports configured as SPAN (port mirroring) destinations cannot use port security |
| Not on voice VLAN without care | When a voice VLAN is configured alongside port security, the maximum MAC count must account for both the phone MAC and PC MAC (minimum 2 — typically set to 2 or 3) |
! Correct sequence — set access mode first, then enable port security: Switch(config)# interface fastEthernet 0/1 Switch(config-if)# switchport mode access Switch(config-if)# switchport access vlan 10 Switch(config-if)# switchport port-security ! enables port security with defaults ! Attempting to enable port security on a trunk port produces: % Port security is not supported on a trunk port.
4. Types of Secure MAC Addresses
Once port security is enabled, MAC addresses on that port are classified as one of three types. Understanding the difference between them is essential for both configuration and the CCNA exam.
| Type | How It Is Added | Survives Reboot? | Appears In |
|---|---|---|---|
| Static secure MAC | Manually configured by the administrator using
switchport port-security mac-address <mac> |
Yes — stored in running-config (must be saved to startup-config
with write memory) |
Running-config and MAC address table as STATIC |
| Dynamic secure MAC | Learned automatically from incoming frames when port security is enabled (default behaviour) | No — lost on reload or port shutdown/no shutdown | MAC address table only — not in running-config |
| Sticky secure MAC | Learned dynamically then automatically written into the
running-config as if statically configured —
enabled with switchport port-security mac-address sticky |
Yes — if running-config is saved to startup-config | Running-config and MAC address table as STATIC |
Which Type Should You Use?
| Scenario | Recommended Type | Reason |
|---|---|---|
| Small network, devices rarely move, high security needed | Static | Explicit control — only the exact specified MAC is ever permitted |
| Medium network, devices are mostly fixed, want automated learning | Sticky | Learns MAC automatically on first connection then locks it in; saves admin time vs manually typing each MAC |
| Basic limiting — prevent MAC flooding but allow any device | Dynamic | Simplest to configure; just set a maximum count; no MAC pinning |
5. Maximum MAC Address Limit
The maximum MAC address limit controls how many unique source MAC addresses the switch will accept on a port before treating any additional MAC as a violation. The default value is 1 on most Cisco platforms.
Default behaviour (maximum = 1): - First MAC address seen on the port → accepted, learned as secure MAC - Second (different) MAC address seen on the port → VIOLATION triggered Configuring the maximum MAC limit: Switch(config-if)# switchport port-security maximum <count> Examples: switchport port-security maximum 1 ! one device only (default) switchport port-security maximum 2 ! PC + IP phone on same port switchport port-security maximum 5 ! small shared segment (not recommended) ! The maximum can be set from 1 up to the platform's CAM table limit per port. ! Typical exam value: maximum 1 (one authorised device per access port)
maximum 2 minimum to
avoid triggering a violation when both devices connect. Some deployments
use maximum 3 to allow for the phone's own internal switch
port MAC as well.
6. Sticky MAC — Automatic Secure Learning
Sticky MAC is the most commonly deployed form of port security in enterprise networks. When sticky learning is enabled, the switch dynamically learns the source MAC address of the first device that connects and automatically adds it to the running-configuration as a static secure MAC entry. The port is then "locked" to that device.
How Sticky Learning Works
Step 1 — Enable sticky MAC on a port: Switch(config-if)# switchport port-security mac-address sticky Step 2 — PC with MAC AA:BB:CC:DD:EE:FF connects to the port and sends a frame. Step 3 — Switch automatically adds to running-config: switchport port-security mac-address sticky AA:BB:CC:DD:EE:FF Step 4 — Verify: Switch# show port-security interface fastEthernet 0/1 Port Security : Enabled Port Status : Secure-up Violation Mode : Shutdown Maximum MAC Addresses : 1 Total MAC Addresses : 1 Configured MAC Addresses : 0 Sticky MAC Addresses : 1 Last Source Address:Vlan : AA:BB:CC:DD:EE:FF:10 Step 5 — Save the running-config to persist the sticky entry across reboots: Switch# write memory (or: copy running-config startup-config)
Sticky MAC — What Happens When the Device Changes
Scenario: sticky MAC AA:BB:CC:DD:EE:FF is locked to Fa0/1.
A different PC (MAC: 11:22:33:44:55:66) is plugged in.
Switch receives a frame with source MAC 11:22:33:44:55:66 on Fa0/1.
This MAC does not match the secured sticky entry AA:BB:CC:DD:EE:FF.
→ VIOLATION detected → action depends on configured violation mode.
To authorise the new device:
1. Remove the old sticky entry manually:
Switch(config-if)# no switchport port-security mac-address sticky AA:BB:CC:DD:EE:FF
2. Or disable and re-enable port security to clear and re-learn.
3. Or perform an errdisable recovery (if violation mode = shutdown).
copy running-config startup-config. Otherwise, after a reboot,
the port re-learns the MAC of whatever device connects first — which might
not be the authorised one.
7. Violation Modes
A violation occurs when a frame arrives on a port-security-enabled port and one of the following is true:
Violation conditions:
Condition 1: The source MAC address of the incoming frame is NOT in the
secure MAC list for that port, AND the maximum MAC count
has already been reached.
Condition 2: The source MAC address appears on a different port that
already has it as a secure MAC (MAC move violation).
When a violation is detected, the switch takes action according to the configured violation mode. There are three modes:
7.1 Shutdown Mode (Default)
switchport port-security violation shutdown When a violation occurs: ┌───────────────────────────────────────────────────────────────────┐ │ 1. Port is immediately placed into err-disabled state │ │ 2. Port LED turns amber (off on some platforms) │ │ 3. A syslog message is generated: │ │ %PORT_SECURITY-2-PSECURE_VIOLATION: Security violation on │ │ FastEthernet0/1, address AA:BB:CC:DD:EE:FF │ │ 4. An SNMP trap is sent (if SNMP is configured) │ │ 5. Violation counter increments by 1 │ │ 6. ALL traffic (including authorised MACs) is dropped │ └───────────────────────────────────────────────────────────────────┘ Recovery (manual): Switch(config)# interface fastEthernet 0/1 Switch(config-if)# shutdown Switch(config-if)# no shutdown Recovery (automatic errdisable recovery): Switch(config)# errdisable recovery cause psecure-violation Switch(config)# errdisable recovery interval 300 ! seconds
7.2 Restrict Mode
switchport port-security violation restrict When a violation occurs: ┌───────────────────────────────────────────────────────────────────┐ │ 1. Offending frames (from the unknown MAC) are DROPPED │ │ 2. Port remains UP and operational │ │ 3. Authorised MAC traffic continues to flow normally │ │ 4. A syslog message is generated for each violation │ │ 5. An SNMP trap is sent (if configured) │ │ 6. Violation counter increments for each offending frame │ └───────────────────────────────────────────────────────────────────┘ Key difference from shutdown: the port stays up. Legitimate traffic is unaffected. Only the unauthorised frames are dropped. Useful when you want to log and alert without disrupting the authorised user.
7.3 Protect Mode
switchport port-security violation protect When a violation occurs: ┌───────────────────────────────────────────────────────────────────┐ │ 1. Offending frames (from the unknown MAC) are DROPPED silently │ │ 2. Port remains UP and operational │ │ 3. Authorised MAC traffic continues to flow normally │ │ 4. NO syslog message generated │ │ 5. NO SNMP trap sent │ │ 6. Violation counter does NOT increment │ └───────────────────────────────────────────────────────────────────┘ Key difference from restrict: completely silent — no logging, no alerts. The violation is invisible unless you specifically poll port-security counters. Least commonly used in production — violations go undetected.
Violation Mode Comparison
| Feature | Shutdown (default) | Restrict | Protect |
|---|---|---|---|
| Offending frames dropped | Yes | Yes | Yes |
| Port disabled (err-disabled) | Yes | No | No |
| Authorised traffic affected | Yes — all traffic stops | No | No |
| Syslog message generated | Yes | Yes | No |
| SNMP trap sent | Yes | Yes | No |
| Violation counter increments | Yes (once — port shuts down) | Yes (per frame) | No |
| Manual recovery required | Yes (or errdisable recovery) | No | No |
| Security impact | Strongest — disruptive | Medium — logs and alerts | Weakest — silent drop |
See full guide: Violation Modes – Full Guide
8. Full Port Security Configuration
The following shows a complete port security configuration combining all key parameters on a single access port.
8.1 Minimum Configuration (defaults used)
Switch(config)# interface fastEthernet 0/1 Switch(config-if)# switchport mode access Switch(config-if)# switchport port-security ! Defaults applied: ! maximum = 1 ! violation mode = shutdown ! MAC type = dynamic (no sticky, no static)
8.2 Full Configuration with Sticky MAC and Custom Violation Mode
Switch(config)# interface fastEthernet 0/1 Switch(config-if)# switchport mode access Switch(config-if)# switchport access vlan 10 Switch(config-if)# switchport port-security ! enable port security Switch(config-if)# switchport port-security maximum 2 ! allow PC + IP phone Switch(config-if)# switchport port-security mac-address sticky ! sticky learning Switch(config-if)# switchport port-security violation restrict ! log but stay up ! Save so sticky entries survive reboot: Switch# copy running-config startup-config
8.3 Configuration with a Static MAC Address
Switch(config)# interface fastEthernet 0/2 Switch(config-if)# switchport mode access Switch(config-if)# switchport access vlan 20 Switch(config-if)# switchport port-security Switch(config-if)# switchport port-security maximum 1 Switch(config-if)# switchport port-security mac-address AA:BB:CC:DD:EE:FF ! static Switch(config-if)# switchport port-security violation shutdown
8.4 Configuring Multiple Ports at Once (interface range)
Switch(config)# interface range fastEthernet 0/1 - 24 Switch(config-if-range)# switchport mode access Switch(config-if-range)# switchport port-security Switch(config-if-range)# switchport port-security maximum 1 Switch(config-if-range)# switchport port-security mac-address sticky Switch(config-if-range)# switchport port-security violation shutdown
9. Err-Disabled State and Recovery
When violation mode is set to shutdown and a violation occurs, the port enters err-disabled state. This is a Cisco-specific state indicating that the interface has been administratively disabled by the switch itself due to an error condition. The port will not pass any traffic until it is recovered.
Identifying an err-disabled port: Switch# show interfaces fastEthernet 0/1 FastEthernet0/1 is down, line protocol is down (err-disabled) Switch# show interfaces status Port Name Status Vlan ... Fa0/1 err-disabled 10 Switch# show port-security interface fastEthernet 0/1 Port Security : Enabled Port Status : Secure-shutdown ← err-disabled due to port security Violation Mode : Shutdown Last Source Address:Vlan : 11:22:33:44:55:66:10 ← the offending MAC
Manual Recovery
! Remove the offending device first, then: Switch(config)# interface fastEthernet 0/1 Switch(config-if)# shutdown Switch(config-if)# no shutdown ! Port returns to Secure-up state (if the violation MAC is gone)
Automatic Errdisable Recovery
! Configure the switch to automatically recover err-disabled ports after a timer: Switch(config)# errdisable recovery cause psecure-violation Switch(config)# errdisable recovery interval 300 ! recover after 300 seconds (5 min) ! Verify errdisable recovery settings: Switch# show errdisable recovery ErrDisable Reason Timer Status Timer Interval ----------------- -------------- -------------- psecure-violation Enabled 300 Note: Automatic recovery should be used carefully. If the offending device is still connected, the port will shut down again immediately after recovery, causing a repeated cycle. Remove the cause first.
show interfaces. An
administratively shut-down port shows "administratively down." These are
different states — err-disabled is triggered by the switch; administratively
down is triggered by a shutdown command.
10. Verification and Monitoring Commands
! Summary of port security status on ALL ports:
Switch# show port-security
Secure Port MaxSecureAddr CurrentAddr SecurityViolation Security Action
----------- ------------- ----------- ----------------- ---------------
Fa0/1 1 1 0 Shutdown
Fa0/2 2 2 0 Restrict
! Detailed status for a specific port:
Switch# show port-security interface fastEthernet 0/1
Port Security : Enabled
Port Status : Secure-up
Violation Mode : Shutdown
Aging Time : 0 mins
Aging Type : Absolute
SecureStatic Address Aging : Disabled
Maximum MAC Addresses : 1
Total MAC Addresses : 1
Configured MAC Addresses : 0
Sticky MAC Addresses : 1
Last Source Address:Vlan : AA:BB:CC:DD:EE:FF:10
Security Violation Count : 0
! Show all secure MAC addresses (static, dynamic, sticky):
Switch# show port-security address
Secure Mac Address Table
-----------------------------------------------------------------------
Vlan Mac Address Type Ports Remaining Age (mins)
---- ----------- ---- ----- --------------------
10 AA:BB:CC:DD:EE:FF SecureSticky Fa0/1 -
! Show MAC address table entries for a specific port:
Switch# show mac address-table interface fastEthernet 0/1
Key Output Fields to Know
| Field | What It Means |
|---|---|
| Port Status: Secure-up | Port security enabled and port is operational — no violation |
| Port Status: Secure-shutdown | Port is err-disabled due to a security violation (shutdown mode) |
| Port Status: Secure-down | Port security enabled but port is physically down (no link) |
| Sticky MAC Addresses | Number of MAC addresses learned via sticky; these appear in running-config |
| Configured MAC Addresses | Number of statically configured MAC addresses on this port |
| Security Violation Count | Total number of violation events recorded on this port; protect mode does not increment this counter |
| Last Source Address:Vlan | The MAC address and VLAN that triggered the last violation — critical for identifying the offending device |
11. Securing Unused Ports
Port security addresses active ports, but unused switch ports are also a security concern — an attacker can simply plug into an unused port and gain Layer 2 network access. Best practice is to apply a combination of measures to all unused ports.
Best practice configuration for unused ports: Switch(config)# interface range fastEthernet 0/10 - 24 ! unused ports Switch(config-if-range)# switchport mode access Switch(config-if-range)# switchport access vlan 999 ! isolated "black hole" VLAN Switch(config-if-range)# switchport port-security Switch(config-if-range)# switchport port-security maximum 1 Switch(config-if-range)# switchport port-security violation shutdown Switch(config-if-range)# shutdown ! administratively disable ! Also create VLAN 999 and assign no routing to it: Switch(config)# vlan 999 Switch(config-vlan)# name UNUSED_PORTS Switch(config-vlan)# exit
| Measure | Purpose |
|---|---|
switchport access vlan 999 |
Places unused ports in an isolated VLAN with no routing — even if someone connects, they cannot reach production networks |
switchport port-security violation shutdown |
If an admin forgets to shut the port, any MAC detected causes an immediate err-disable with a syslog alert |
shutdown |
Primary defence — physically disables the port; no traffic passes until explicitly re-enabled |
12. Port Security MAC Aging
By default, secure MAC addresses do not age out — they are permanent until manually removed or the port is reset. MAC aging can be configured to automatically remove secure MAC entries after a set time, useful in environments where devices frequently change (e.g., shared desk spaces or training rooms).
MAC aging types: ┌──────────────┬────────────────────────────────────────────────────────────┐ │ Absolute │ The MAC entry is removed exactly <time> minutes after it │ │ │ was first learned, regardless of activity on the port. │ ├──────────────┼────────────────────────────────────────────────────────────┤ │ Inactivity │ The MAC entry is removed if no frames from that MAC have │ │ │ been seen for <time> minutes (idle timer resets on each │ │ │ received frame). │ └──────────────┴────────────────────────────────────────────────────────────┘ Configure aging: Switch(config-if)# switchport port-security aging time 60 ! 60 minutes Switch(config-if)# switchport port-security aging type inactivity ! Allow static MACs to age (disabled by default): Switch(config-if)# switchport port-security aging static ! Verify: Switch# show port-security interface fastEthernet 0/1 Aging Time : 60 mins Aging Type : Inactivity
aging static is also configured. This is important
to remember — sticky without aging static effectively makes the
sticky entry permanent for the lifetime of the running-config.
13. Port Security Summary — Key Facts
| Topic | Key Fact |
|---|---|
| Supported port types | Access ports only — not trunk, routed, or EtherChannel ports |
| Enable command | switchport port-security (on an access mode interface) |
| Default maximum MACs | 1 — only one MAC address permitted per port by default |
| Default violation mode | Shutdown — port enters err-disabled on violation |
| Static MAC | Manually configured; in running-config; persists across reboot if saved |
| Dynamic MAC | Auto-learned; NOT in running-config; lost on reload |
| Sticky MAC | Auto-learned then written to running-config; survives reboot if saved |
| Shutdown mode | Err-disables port; generates syslog + SNMP; all traffic stops; manual recovery needed |
| Restrict mode | Drops offending frames; port stays up; generates syslog + SNMP; counter increments |
| Protect mode | Drops offending frames silently; port stays up; NO logging; counter does NOT increment |
| Err-disabled recovery | shutdown / no shutdown on the port,
or errdisable recovery cause psecure-violation |
| Verify command | show port-security interface <port> and
show port-security address |