Port Security & Sticky MAC Configuration
By default, a Cisco switch access port will learn and forward traffic from any device plugged into it — there is no restriction on how many devices can connect or which MAC addresses are allowed. In environments where physical access to switch ports cannot be fully controlled, this openness is a security risk. An employee could plug in a personal hub or switch, connecting multiple unauthorized devices to the corporate network.
Port Security addresses this by allowing you to limit the number of MAC addresses permitted on a port and define what happens when a violation occurs. Sticky MAC extends this by automatically learning the first MAC address that connects and locking it in — without requiring manual configuration of every device's MAC address. Together they provide a practical layer of access control without the complexity of 802.1X authentication.
Before starting, complete Assigning VLANs to Switch Ports and PortFast & BPDU Guard Configuration. Port security only works on access ports and must be configured in access mode.
1. Port Security — Core Concepts
Secure MAC Address Types
Port security works by building a table of secure MAC addresses for each port. There are three ways to populate this table:
| Type | How It Is Added | Survives Reload? | Use Case |
|---|---|---|---|
| Static | Manually configured: switchport port-security mac-address [MAC] |
✅ Yes — in running-config | Known, fixed devices (servers, printers) |
| Dynamic | Learned automatically from traffic — not saved | ❌ No — cleared on reload or port shutdown | Basic learning — rarely used alone |
| Sticky | Learned automatically from traffic and saved to running-config | ✅ Yes — if wr is run after learning. See Saving Cisco Configurations. |
✅ Best practice — automatic learning with persistence |
Violation Modes — What Happens When a Violation Occurs
A violation occurs when a frame arrives from a MAC address that is not in the secure MAC table AND the maximum MAC count has already been reached. The switch responds based on the configured violation mode:
| Mode | Drops Violating Frames? | Sends Syslog? | Increments Violation Counter? | Shuts Port Down? |
|---|---|---|---|---|
| shutdown | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes — err-disabled immediately |
| restrict | ✅ Yes | ✅ Yes | ✅ Yes | ❌ No — port stays up |
| protect | ✅ Yes | ❌ No | ❌ No | ❌ No — port stays up, silent drop |
- shutdown — most secure, default mode. Use when unauthorized devices must be actively blocked and administrators notified. Requires manual recovery.
- restrict — port stays up for legitimate devices while blocking and logging violations. Good when some authorized devices share the port.
- protect — silently drops violations with no logging. Least visible — not recommended for production security monitoring.
2. Lab Scenario
NetsTuts_SW1 has the following port security requirements. See VLAN Creation and Management for creating the VLANs referenced below, and Assigning VLANs to Switch Ports for setting access mode on each port:
| Port | VLAN | Max MACs | Learning | Violation Mode | Purpose |
|---|---|---|---|---|---|
| Fa0/1 | 10 | 1 | Sticky | shutdown | Single workstation — lock to first device connected |
| Fa0/2 | 20 | 2 | Sticky | restrict | Hot-desk — up to 2 devices, log violations but stay up |
| Fa0/3 | 30 | 1 | Static | shutdown | Server — specific MAC hardcoded, strict enforcement |
3. Step 1 — Sticky MAC with Shutdown Violation (Fa0/1)
This is the most common port security configuration in production. The port learns the first MAC address automatically, locks it in, and shuts down if any other device attempts to connect.
NetsTuts_SW1>en NetsTuts_SW1#conf t Enter configuration commands, one per line. End with CNTL/Z. NetsTuts_SW1(config)#interface FastEthernet0/1 NetsTuts_SW1(config-if)#description Workstation-VLAN10 NetsTuts_SW1(config-if)#switchport mode access NetsTuts_SW1(config-if)#switchport access vlan 10 NetsTuts_SW1(config-if)#switchport port-security NetsTuts_SW1(config-if)#switchport port-security maximum 1 NetsTuts_SW1(config-if)#switchport port-security mac-address sticky NetsTuts_SW1(config-if)#switchport port-security violation shutdown NetsTuts_SW1(config-if)#exit
switchport mode access before enabling port security.
Command Breakdown
| Command | What It Does | Notes |
|---|---|---|
switchport port-security |
Enables port security on the interface | Must be the first port-security command. Port must be in access mode. |
switchport port-security maximum 1 |
Limits the port to 1 secure MAC address | Default maximum is 1. Range: 1–3072 depending on platform. |
switchport port-security mac-address sticky |
Enables sticky learning — first MAC learned is written to running-config | Once learned, the sticky MAC appears as a static entry in running-config. See show mac address-table. |
switchport port-security violation shutdown |
Places port in err-disabled state on violation | Default violation mode — most secure response |
What the Running-Config Shows After Sticky Learning
NetsTuts_SW1#show running-config interface FastEthernet0/1 ! interface FastEthernet0/1 description Workstation-VLAN10 switchport access vlan 10 switchport mode access switchport port-security switchport port-security mac-address sticky switchport port-security mac-address sticky 0050.7966.0001 !
switchport port-security mac-address sticky
0050.7966.0001 to the running-config after the first frame is received.
Run wr to save this to startup-config so it persists after reload.
See Saving and Managing Cisco Configurations.
4. Step 2 — Sticky MAC with Restrict Violation (Fa0/2)
Restrict mode allows the port to stay up for authorized devices while dropping and logging frames from unauthorized ones — without shutting the port down. This is useful when you cannot afford the disruption of a port shutdown.
NetsTuts_SW1(config)#interface FastEthernet0/2 NetsTuts_SW1(config-if)#description HotDesk-VLAN20 NetsTuts_SW1(config-if)#switchport mode access NetsTuts_SW1(config-if)#switchport access vlan 20 NetsTuts_SW1(config-if)#switchport port-security NetsTuts_SW1(config-if)#switchport port-security maximum 2 NetsTuts_SW1(config-if)#switchport port-security mac-address sticky NetsTuts_SW1(config-if)#switchport port-security violation restrict NetsTuts_SW1(config-if)#exit
%PORT_SECURITY-2-PSECURE_VIOLATION messages.
5. Step 3 — Static MAC with Shutdown Violation (Fa0/3)
For devices with fixed, known MAC addresses (servers, printers, IP cameras), configuring the MAC address statically provides the strongest security — only that exact device is ever permitted on the port.
NetsTuts_SW1(config)#interface FastEthernet0/3 NetsTuts_SW1(config-if)#description FileServer-VLAN30 NetsTuts_SW1(config-if)#switchport mode access NetsTuts_SW1(config-if)#switchport access vlan 30 NetsTuts_SW1(config-if)#switchport port-security NetsTuts_SW1(config-if)#switchport port-security maximum 1 NetsTuts_SW1(config-if)#switchport port-security mac-address 0050.7966.0099 NetsTuts_SW1(config-if)#switchport port-security violation shutdown NetsTuts_SW1(config-if)#exit NetsTuts_SW1(config)#end NetsTuts_SW1#wr Building configuration... [OK] NetsTuts_SW1#
0050.7966.0099 is hard-coded.
Only frames from this exact MAC address are accepted on Fa0/3.
Any other device immediately triggers a port shutdown.
Verify the VLAN 30 assignment with
show vlan brief.
6. What a Violation Looks Like
When an unauthorized device connects to a port with shutdown violation mode, the following sequence of syslog messages appears:
%PORT_SECURITY-2-PSECURE_VIOLATION: Security violation occurred, caused by MAC address 0050.7966.ABCD on port FastEthernet0/1. %PM-4-ERR_DISABLE: psecure-violation error detected on Fa0/1, putting Fa0/1 in err-disable state %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to down %LINK-3-UPDOWN: Interface FastEthernet0/1, changed state to down
show logging
to review historical violation events. Forward these to a central
syslog server for security monitoring.
Recovering an err-disabled Port After a Violation
! ── 1. Disconnect the unauthorized device ──────────────── ! ── 2. Manually recover the port ───────────────────────── NetsTuts_SW1#conf t NetsTuts_SW1(config)#interface FastEthernet0/1 NetsTuts_SW1(config-if)#shutdown NetsTuts_SW1(config-if)#no shutdown NetsTuts_SW1(config-if)#end NetsTuts_SW1# %LINK-3-UPDOWN: Interface FastEthernet0/1, changed state to up %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to up
show interfaces FastEthernet0/1 status.
Clearing Sticky MAC Addresses
If you need to re-learn a new device (for example, replacing a workstation), clear the sticky MACs before connecting the new device:
NetsTuts_SW1#clear port-security sticky interface FastEthernet0/1 NetsTuts_SW1#
wr after the new device connects to save the
updated sticky MAC to startup-config. See
Saving and Managing Cisco Configurations.
7. Verification
show port-security
Summary of all ports with port security enabled across the switch:
NetsTuts_SW1#show port-security
Secure Port MaxSecureAddr CurrentAddr SecurityViolation Security Action
(Count) (Count) (Count)
---------------------------------------------------------------------------
Fa0/1 1 1 0 Shutdown
Fa0/2 2 1 0 Restrict
Fa0/3 1 1 0 Shutdown
---------------------------------------------------------------------------
Total Addresses in System (excluding one mac per port) : 0
Max Addresses limit in System (excluding one mac per port) : 4096
show port-security interface FastEthernet0/1
NetsTuts_SW1#show port-security interface FastEthernet0/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 : 0050.7966.0001:10 Security Violation Count : 0
show mac address-table
to see all learned MAC addresses on the switch.
Port Status Values Explained
| Port Status | Meaning |
|---|---|
| Secure-up | Port security is active and the port is forwarding normally — no violation |
| Secure-shutdown | Port has been err-disabled due to a security violation (shutdown mode triggered). Verify with show interfaces status. |
| Secure-down | Port security is configured but the port is physically down (no cable or device) |
show port-security address
NetsTuts_SW1#show port-security address
Secure Mac Address Table
-----------------------------------------------------------------------------
Vlan Mac Address Type Ports Remaining Age
(mins)
---- ----------- ---- ----- -------------
10 0050.7966.0001 SecureSticky Fa0/1 -
20 0050.7966.0002 SecureSticky Fa0/2 -
30 0050.7966.0099 SecureConfigured Fa0/3 -
-----------------------------------------------------------------------------
Total Addresses in System (excluding one mac per port) : 2
Maximum Addresses limit in System (excluding one mac per port) : 4096
show mac address-table
to see the full dynamic MAC table alongside secure entries.
Verification Command Summary
| Command | What It Shows |
|---|---|
show port-security |
Summary of all secured ports — max/current MACs, violation count, violation mode |
show port-security interface [int] |
Detailed per-port status — secure MAC counts, port status, last source address |
show port-security address |
All secure MAC addresses on the switch — type (sticky/static), port, VLAN |
show running-config interface [int] |
Shows sticky MACs as they appear in configuration after learning |
show logging |
Violation events including the violating MAC address and timestamp |
show interfaces [int] status |
Shows "err-disabled" if the port is in violation shutdown state |
show mac address-table |
Full dynamic MAC table — complements port-security address output |
8. Troubleshooting Port Security Issues
| Problem | Symptom | Cause | Fix |
|---|---|---|---|
| Port security command rejected | IOS returns: "Port security is not supported on dynamic ports" | Port is in dynamic (DTP) mode — port security requires explicit access mode | Add switchport mode access before switchport port-security. See Assigning VLANs to Switch Ports. |
| Port immediately err-disabled after recovery | Port returns to err-disabled within seconds of no shutdown |
Unauthorized device is still connected — violation triggers again | Disconnect the offending device first, then recover with shutdown / no shutdown. Verify with show interfaces status. |
| Sticky MAC not saved after reload | Port re-learns MAC after every reload — learned MACs are lost | wr was not run after sticky MACs were learned — running-config not saved to startup-config |
After sticky MACs are learned, always run wr to save them to NVRAM. See Saving and Managing Cisco Configurations. |
| Wrong device learned as sticky MAC | A different device learned as sticky — correct device now violates | Wrong device connected first during initial learning phase | Clear the sticky entry: clear port-security sticky interface [int], then connect the correct device |
| Violations occurring but port stays up | Violation counter incrementing but port not shutting down | Violation mode is restrict or protect — these modes do not shut the port down |
Expected behavior for restrict/protect. Change to violation shutdown if a harder response is needed |
| No violation logging | Unauthorized device connecting but no syslog messages generated | Violation mode is protect — it silently drops frames with no logging |
Change to violation restrict for logging, or violation shutdown for strict enforcement. Forward logs to a syslog server for persistent storage. |
Key Points & Exam Tips
- Port security only works on access ports — the port must be explicitly set to
switchport mode accessbefore port security can be enabled. See Assigning VLANs to Switch Ports. - Sticky MAC is the most practical deployment — it auto-learns the first device and writes the MAC to running-config. Always run
wrafter learning to persist through reloads. See Saving Configurations. - The default maximum MAC addresses per port is 1. The default violation mode is shutdown.
- Shutdown mode: err-disables the port, sends syslog, increments violation counter. Requires manual recovery (
shutdown/no shutdown). Verify recovery withshow interfaces status. - Restrict mode: drops violating frames, sends syslog, increments counter — port stays up. Use when the port must remain available for authorized devices.
- Protect mode: silently drops violating frames — no syslog, no counter increment, port stays up. Least visible — not recommended for production security monitoring.
- A port in violation shutdown state shows Secure-shutdown in
show port-security interfaceand err-disabled inshow interfaces status. - To replace a locked device, clear sticky MACs with
clear port-security sticky interface [int], then connect the new device and runwr. show port-security addressshows the type of each secure entry: SecureSticky (auto-learned), SecureConfigured (static), or SecureDynamic (dynamic — not saved). Compare withshow mac address-table.- On the CCNA exam, know all three violation modes and their exact behaviors — especially that protect is the only mode that generates no syslog and increments no counter.
- Port security is a Layer 2 access control mechanism. For STP-related port hardening, see PortFast & BPDU Guard. For identity-based port access, see 802.1X Port Authentication. For DHCP and ARP protection, see DHCP Snooping & Dynamic ARP Inspection.