Domain 5Security FundamentalsCORE

Port Security and Layer 2 Security

Configuring switch port security to prevent MAC flooding and unauthorized access, including violation modes and 802.1X basics.

Domain: Domain 5: Security Fundamentals•Updated: 2026-08-26
#Port Security#MAC Limit#Violation Modes#802.1X#Layer 2 Security

Layer 2 Security: Port Security

Core Concepts & Prerequisites

  • Concept: Port Security is a Layer 2 security feature on Cisco switches that controls which source MAC (Media Access Control) addresses and how many MAC addresses are allowed to enter a switch interface.
  • Details:
    • Configured on a per-interface basis.
    • Prevents unauthorized devices from accessing the network.
    • Defends against MAC flooding and DHCP starvation attacks (where attackers spoof thousands of MAC addresses to exhaust the DHCP pool or switch MAC address table).
  • Prerequisites:
    • Port Security can be enabled on Access or Trunk ports, but they must be statically configured.
    • Dynamic modes (dynamic auto or dynamic desirable via DTP - Dynamic Trunking Protocol) are not allowed.
! Check administrative mode (must not be dynamic auto/desirable)
SW1# show interfaces gigabitethernet0/1 switchport

! Statically configure the port as an access port
SW1(config)# interface gigabitethernet0/1
SW1(config-if)# switchport mode access

! Enable port security with default settings
SW1(config-if)# switchport port-security

Things to note: If you try to enable port security on a port with the default dynamic auto mode, the switch will reject the command and state the interface is a “dynamic port”.

Violation Modes

  • Concept: Determines the action the switch takes when an unauthorized frame (a frame with a source MAC not in the allowed list) enters the interface.
  • Details: There are three violation modes. shutdown is the default.
Feature Shutdown (Default) Restrict Protect
Port Status Err-disabled (secure-shutdown) Secure-up Secure-up
Unauthorized Traffic Dropped (Port goes down) Dropped Dropped
Authorized Traffic Stops (Port is down) Continues normally Continues normally
Syslog / SNMP Generates 1 message upon shutdown Generates message for every violation No messages generated
Violation Counter Set to 1 (resets on recovery) Incremented for every violation Not incremented
! Configure violation mode
SW1(config-if)# switchport port-security violation shutdown
SW1(config-if)# switchport port-security violation restrict
SW1(config-if)# switchport port-security violation protect

Things to note: In restrict mode, the switch sends an SNMP Trap (agent to manager), not an SNMP Get (manager to agent).

graph TD
    A[Frame Arrives on Port] --> B{Source MAC Allowed?}
    B -- Yes --> C[Forward Frame Normally]
    B -- No --> D{Violation Mode?}
    D -- Shutdown --> E[Err-Disable Port, Send 1 Syslog]
    D -- Restrict --> F[Drop Frame, Increment Counter, Send Syslog]
    D -- Protect --> G[Drop Frame Silently]

MAC Address Learning & Sticky MACs

  • Concept: Defines how the switch populates its list of allowed (secure) MAC addresses.
  • Details:
    • Default Maximum: 1 MAC address allowed per port.
    • Learning Methods: Can be manually configured (static), dynamically learned (first MAC to enter the port), or a combination of both.
    • Sticky MACs: Dynamically learned MAC addresses that are automatically converted to static entries in the running configuration.
! Set maximum allowed MAC addresses
SW1(config-if)# switchport port-security maximum 2

! Manually configure a static secure MAC address
SW1(config-if)# switchport port-security mac-address A.A.A.

! Enable sticky learning (converts dynamic MACs to sticky)
SW1(config-if)# switchport port-security mac-address sticky

Things to note:

  • Sticky MACs are added to the running-config automatically (e.g., switchport port-security mac-address sticky A.A.A.).
  • You must save the configuration (write memory or copy run start) to make sticky MACs permanent across reboots.
  • In the MAC address table, Sticky MACs appear with a type of STATIC, even though they were dynamically learned (Exam trap!).

Secure MAC Address Aging

  • Concept: Timers that automatically remove secure MAC addresses from the allowed list. By default, aging is disabled (0 minutes).
  • Details:
    • By default, only dynamically learned MAC addresses age out. Static MACs do not.
    • You can force static MACs to age out using the aging static command.
Aging Type Behavior
Absolute (Default) Timer starts when MAC is learned. MAC is removed when timer expires, even if traffic is continuously received from that MAC.
Inactivity Timer resets every time a frame is received from the MAC. MAC is only removed if no traffic is seen for the duration of the timer.
! Set aging timer to 30 minutes
SW1(config-if)# switchport port-security aging time 30

! Set aging type to inactivity
SW1(config-if)# switchport port-security aging type inactivity

! Enable aging for statically configured MAC addresses
SW1(config-if)# switchport port-security aging static

Recovering from Err-Disabled State

  • Concept: When a port enters the err-disabled state (due to a shutdown violation or other reasons), it must be recovered before it can pass traffic again.
  • Details:
    • CRITICAL STEP 1: Always physically disconnect the unauthorized device before recovering the port. If you don’t, the port will immediately shut down again upon receiving the next unauthorized frame, or the unauthorized MAC might become the new learned secure MAC.
    • Recovery Methods: Manual (CLI) or Automatic (ErrDisable Recovery).
! MANUAL RECOVERY: Must be done in interface configuration mode
SW1(config)# interface gigabitethernet0/1
SW1(config-if)# shutdown
SW1(config-if)# no shutdown

! AUTOMATIC RECOVERY: Enable recovery for port-security violations
SW1(config)# errdisable recovery cause psecure-violation

! OPTIONAL: Change the default recovery timer (Default is 300 seconds / 5 mins)
SW1(config)# errdisable recovery interval 180

! Verify errdisable recovery status
SW1# show errdisable recovery

Things to note: Simply unplugging the unauthorized device (without issuing shutdown/no shutdown or configuring auto-recovery) will not re-enable the interface.

Verification Commands

  • Concept: Commands used to verify Port Security status, violations, and MAC address tables.
! Detailed view of a specific interface's port security status
! Shows: Port Status, Violation Mode, MAC counts, Aging timers, Violation count
SW1# show port-security interface gigabitethernet0/1

! Summary view of all interfaces with port security enabled
! Shows: Max/Current MACs, Violation count, Security Action
SW1# show port-security

! View all secure MAC addresses in the switch's MAC address table
SW1# show mac address-table secure

Command Summary

! --- Prerequisites & Enabling ---
SW1(config-if)# switchport mode access
SW1(config-if)# switchport port-security

! --- MAC Address Limits & Learning ---
SW1(config-if)# switchport port-security maximum <1-8192>
SW1(config-if)# switchport port-security mac-address <mac_address>
SW1(config-if)# switchport port-security mac-address sticky

! --- Violation Modes ---
SW1(config-if)# switchport port-security violation {shutdown | restrict | protect}

! --- Aging Timers ---
SW1(config-if)# switchport port-security aging time <minutes>
SW1(config-if)# switchport port-security aging type {absolute | inactivity}
SW1(config-if)# switchport port-security aging static

! --- Err-Disable Recovery (Global Config) ---
SW1(config)# errdisable recovery cause psecure-violation
SW1(config)# errdisable recovery interval <seconds>

! --- Manual Recovery (Interface Config) ---
SW1(config-if)# shutdown
SW1(config-if)# no shutdown

! --- Verification ---
SW1# show interfaces <interface> switchport
SW1# show port-security
SW1# show port-security interface <interface>
SW1# show mac address-table secure
SW1# show errdisable recovery