How to Set Up Port Security on Cisco Switches: MAC Address Limiting and Violation Actions

How to Set Up Port Security on Cisco Switches

How to Set Up Port Security on Cisco Switches

Port security was one of the first switch security features I ever configured, and it’s still one of the features I recommend to almost every team hardening an access layer, because it addresses a very simple but very real risk: unauthorized devices plugging into open network jacks. In this guide I’ll cover exactly how port security works, how to configure it for different real-world needs, and the mistakes that turn a good security control into a support headache.

Why Port Security Matters

Think about how many physical Ethernet jacks exist in a typical office — conference rooms, empty desks, under-desk cabling that’s easy to reach. Without any control at Layer 2, anyone who can physically access one of those jacks can plug in a laptop, a rogue access point, or a small switch to attach multiple unauthorized devices, and gain a foothold on your network. Port security addresses this by limiting which — and how many — MAC addresses are allowed to communicate through a given switchport, and defining exactly what happens when that limit is violated.

Networking Fundamentals Behind Port Security

Port security operates by inspecting the source MAC address of frames entering an interface and comparing it against a list of allowed addresses for that port. Key concepts:

Step 1: Basic Port Security with Sticky MAC Learning

This is the most common real-world configuration for standard end-user access ports.

Switch(config)# interface GigabitEthernet0/5
Switch(config-if)# switchport mode access
Switch(config-if)# switchport access vlan 10
Switch(config-if)# switchport port-security
Switch(config-if)# switchport port-security maximum 2
Switch(config-if)# switchport port-security mac-address sticky
Switch(config-if)# switchport port-security violation restrict
Switch(config-if)# exit

Note that port security requires the port to be explicitly configured as switchport mode access (or trunk, in less common designs) — it will reject configuration on a port still in dynamic negotiation mode. maximum 2 allows for common scenarios like a PC plus a VoIP phone sharing the same port through the phone’s built-in switch.

Step 2: Statically Defining Allowed MAC Addresses

For higher-security ports — server room connections, network device management ports — you may want to hard-code exactly which MAC addresses are permitted, with no learning at all.

Switch(config)# interface GigabitEthernet0/10
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 0011.2233.4455
Switch(config-if)# switchport port-security violation shutdown
Switch(config-if)# exit

Step 3: Configuring Aging for Dynamic/Sticky Entries

By default, secure MAC addresses don’t age out, which can be a problem in environments with frequent device turnover (hot-desking, shared workstations).

Switch(config)# interface GigabitEthernet0/5
Switch(config-if)# switchport port-security aging time 60
Switch(config-if)# switchport port-security aging type inactivity
Switch(config-if)# exit

aging type inactivity ages out an address only after it’s been inactive for the specified time (in minutes), rather than a flat absolute timer — generally the more sensible choice for user-facing ports.

Step 4: Understanding and Choosing the Right Violation Action

For most standard office access ports, I lean toward restrict rather than the default shutdown, because a single momentary violation (someone plugging in an unauthorized second device briefly) won’t take down connectivity for the legitimate device sharing that port, while still being logged for review. For genuinely sensitive ports — server rooms, network infrastructure — shutdown is often the right call, since you want a hard stop and clear incident visibility rather than quietly dropping and continuing.

Switch(config-if)# switchport port-security violation protect

is rarely the right choice in enterprise environments, since it gives you zero visibility that a violation even occurred — I’d reserve it for very specific edge cases only.

Step 5: Verifying Port Security Configuration

Switch# show port-security interface GigabitEthernet0/5

Expected output:

Port Security              : Enabled
Port Status                : Secure-up
Violation Mode             : Restrict
Aging Time                 : 60 mins
Aging Type                 : Inactivity
SecureStatic Address Aging : Disabled
Maximum MAC Addresses      : 2
Total MAC Addresses        : 1
Configured MAC Addresses   : 0
Sticky MAC Addresses       : 1
Last Source Address:Vlan   : 0011.2233.aabb:10
Security Violation Count   : 0

Check all secured MAC addresses switch-wide:

Switch# show port-security address
Secure Mac Address Table
-------------------------------------------------------------------
Vlan    Mac Address       Type                          Ports   Remaining Age (mins)
----    -----------       ----                          -----   -------------------
  10    0011.2233.aabb    SecureSticky                  Gi0/5        58
  20    0011.2233.4455    SecureConfigured              Gi0/10        -

If a port has been err-disabled due to a violation:

Switch# show interfaces GigabitEthernet0/5 status
Port      Name               Status       Vlan       Duplex  Speed Type
Gi0/5                        err-disabled 10         a-full  a-100 10/100/1000BaseTX

And confirm the specific violation with:

Switch# show port-security interface GigabitEthernet0/5

Look at the Security Violation Count and Last Source Address fields to identify the offending MAC.

Step 6: Configuring Automatic Err-Disable Recovery

Rather than requiring a manual shutdown / no shutdown every time a port trips into err-disabled state from a security violation, configure automatic recovery:

Switch(config)# errdisable recovery cause psecure-violation
Switch(config)# errdisable recovery interval 300

This automatically re-enables the port five minutes after a violation-triggered shutdown, while still preserving the log entry for review — a good middle ground between security enforcement and operational convenience.

Practical Lab: Simulating a Port Security Violation

  1. Configure GigabitEthernet0/5 with switchport port-security maximum 1, mac-address sticky, and violation shutdown.
  2. Connect Host A and let it establish connectivity; confirm with show port-security address that its MAC has been learned and stored as sticky.
  3. Disconnect Host A and connect Host B (a different device) to the same port.
  4. Observe the port transition to err-disabled and confirm via show interfaces status and the corresponding syslog message (%PM-4-ERR_DISABLE: psecure-violation error detected).
  5. Change the violation action to restrict, repeat the test, and observe that instead of shutting down, the port simply drops Host B’s traffic while logging a violation, and Host A (if reconnected) continues working normally.

This lab is a great way to internalize the practical behavioral difference between violation modes rather than just memorizing the definitions.

Real-World Enterprise Scenario

A university IT department dealing with constant dorm-room device turnover (students plugging in laptops, gaming consoles, and occasionally unauthorized personal routers/switches) rolled out port security across all dormitory access ports with maximum 2 (allowing a laptop plus one additional device like a gaming console), sticky learning, violation restrict, and a 4-hour inactivity aging timer to accommodate normal device turnover between semesters without requiring manual re-provisioning. Combined with 802.1X in a later phase, this dramatically reduced unauthorized personal switch/router usage that had previously caused DHCP exhaustion and occasional broadcast storm incidents on shared dorm VLANs.

Security Best Practices

Optimization and Performance Tuning

Troubleshooting and Common Mistakes

Frequently Asked Questions

What’s the difference between sticky and dynamic secure MAC addresses? Dynamic addresses are learned automatically but lost on reload; sticky addresses are also learned automatically but get written into the running configuration, surviving a reload if the config is saved.

What happens by default if I enable port security without specifying maximum or violation settings? The default maximum is 1 MAC address, and the default violation action is shutdown — meaning any second device on that port will err-disable it immediately.

Can port security be used on trunk ports? Yes, though it’s far less common than on access ports, and requires careful planning since a trunk can legitimately carry traffic from many MAC addresses across multiple VLANs.

Does port security protect against VLAN hopping or other Layer 2 attacks? No — port security specifically addresses unauthorized MAC addresses/device counts on a port. VLAN hopping, DHCP spoofing, and ARP spoofing require their own dedicated controls (trunk hardening, DHCP snooping, Dynamic ARP Inspection respectively).

Summary

Port security is a small, easy-to-configure feature that closes a very real physical-access gap in enterprise networks: unauthorized devices plugging into unmonitored jacks. The real design decisions come down to choosing sensible maximum values for realistic multi-device scenarios, picking a violation action that matches the sensitivity of each port, and pairing it with errdisable recovery so operational friction doesn’t outweigh the security benefit. Layer it alongside DHCP snooping, Dynamic ARP Inspection, and ideally 802.1X, and you’ve covered the most common and impactful access-layer security gaps with commodity Cisco IOS features.

References

Exit mobile version