On this page
Port security is a switch feature that limits which source MAC addresses, and how many, may send frames into a single access port. When a frame arrives from an address outside that allowance, the switch applies a violation action: it drops the frame quietly, drops it and raises an alarm, or shuts the port down.
The feature lives entirely in the switch's address-learning logic. It does not authenticate anyone and it does not read anything above Layer 2. That makes it cheap to run on every edge port and easy to get wrong, because the switch is enforcing a rule about MAC addresses, which any host can change. This page explains what the switch actually checks, how the three violation modes differ, how sticky learning behaves across a reload, and how to roll it out on a floor of access ports without locking out half the office. It sits in the switching section of FoundryNet beside the pages on VLANs and 802.1X.
01 What does port security actually check?
Every Ethernet switch learns. When a frame enters a port, the switch records the frame's source MAC address against that port and VLAN in its forwarding table, the filtering database in IEEE 802.1Q terms (often called the CAM table on Cisco gear). Port security adds a gate in front of that learning step. For each protected port the switch holds a list of secure addresses and a maximum count. A frame whose source address is already on the list is forwarded as normal. A frame from a new address is accepted, and the address added, only while the list is below the maximum. Once the list is full, a frame from any other address is a violation.
Secure addresses come from three places:
- Static secure addresses, typed into the configuration by hand. They survive reloads because they are part of the saved configuration.
- Dynamic secure addresses, learned from traffic up to the maximum. They are held in memory only and disappear when the port goes down or the switch restarts.
- Sticky secure addresses, learned dynamically and then written into the running configuration. They behave like static entries once the configuration is saved.
Juniper's documentation lists the same idea under two names. MAC limiting caps the number of addresses an interface may learn and is described as protection against flooding of the Ethernet switching table; persistent MAC learning, also called sticky MAC, keeps learned addresses across switch reboots. Both are described in Juniper's Junos port security feature overview, alongside DHCP snooping and dynamic ARP inspection, which protect other parts of the access layer.
02 Which attacks does it stop, and which does it miss?
Port security was built for two problems. The first is MAC flooding: a tool on one host sends frames from thousands of random source addresses, filling the forwarding table. A switch with a full table cannot learn new legitimate addresses, so it floods unknown unicast traffic out of every port in the VLAN, and the attacker's port starts receiving traffic meant for other hosts. A maximum of one or two addresses per port caps the damage at that port. The second problem is the unauthorised device: someone unplugs the desk phone or printer and plugs in their own switch, laptop or wireless router. If the port only accepts the printer's address, the new device gets nothing.
What it does not stop is just as important. A MAC address is a field in a frame that any operating system lets the user set. A visitor who reads the label on the printer, or captures one frame from it, can clone the address and pass the check, with the printer unplugged so the duplicate does not trip anything. Port security says nothing about who is behind the address. If identity matters, the control you want is 802.1X authentication on the access port, which asks the device for credentials or a certificate before the port forwards anything. Port security still has a place next to 802.1X: it caps how many addresses an authenticated port may carry, which stops a single authenticated laptop from bridging a whole unmanaged switch onto the network.
03 How do the three violation modes differ?
On Cisco IOS and IOS XE the violation action is set per port. The names are the same on most Catalyst platforms; other vendors use their own words for the same behaviour.
| Mode (IOS) | Offending frames | Log and counter | Port state | Closest Junos action |
|---|---|---|---|---|
protect | Dropped | No syslog, no violation count | Stays up | drop |
restrict | Dropped | Syslog, SNMP trap, violation counter increments | Stays up | drop-and-log |
shutdown (default) | All traffic stops | Syslog, SNMP trap, counter increments | Error-disabled until recovered | shutdown |
The default on Cisco is shutdown with a maximum of one address, which is the strictest pairing possible. It is the right default for a lab and the wrong one for an office floor on day one, because the first docking station with its own MAC address takes a desk offline. restrict is the useful middle ground for a rollout: the unknown device gets no service, the legitimate device keeps working, and the log tells you which ports would have shut. protect gives you the blocking without the record, which makes it hard to troubleshoot and is rarely the right choice.
04 How does sticky learning behave across a reload?
Sticky learning is the feature most people mean when they say port security. On IOS, switchport port-security mac-address sticky converts every address the port learns, up to the maximum, into a line in the running configuration of the form switchport port-security mac-address sticky 0000.5e00.5301. Two consequences follow. First, the entries persist only if someone saves the configuration; after a reload without a save the port learns afresh, from whatever is plugged in at that moment. Second, once saved, the entry stays until someone removes it, so a desk move or a replaced phone produces a violation on the old port and a fresh learn on the new one.
Junos takes a different route to the same result. The persistent-learning statement, documented in the Junos persistent-learning statement reference, makes learned MAC addresses persist on an interface across switch restarts and link-down conditions, without writing them into the configuration text. Stale entries are removed with clear ethernet-switching table persistent-learning for the interface or the address.
Aging is the other half of the story. IOS can age secure addresses out with switchport port-security aging time (in minutes) and aging type absolute or inactivity. Aging applies to dynamic and, where configured, static secure addresses; sticky addresses do not age. A hot-desk floor where people move every day is better served by dynamic addresses with inactivity aging than by sticky entries that pile up.
05 How to configure port security on an access port
The order below suits a floor of existing access ports. It assumes the ports are already statically in access mode; on Cisco, port security refuses to enable on a port left in dynamic trunk negotiation.
- Inventory each port. Pull the forwarding table for the access ports (
show mac address-table interface Gi1/0/5on IOS,show ethernet-switching table interface ge-0/0/5on Junos) and note how many addresses each one carries today. A port with a phone and a PC shows two or three; a port with a virtualisation host can show dozens. - Fix the port mode. Set every user port to access mode explicitly and turn off trunk negotiation. A port that can become a trunk is outside the scope of this feature.
- Choose a maximum per port class. One for a printer or camera, two or three for a phone with a PC behind it (the phone's address may be learned on both the voice and the data VLAN), more for a conference room dock. Write the classes down so the next engineer knows why port 31 allows eight.
- Enable with a soft action. Turn on port security with the
restrictaction (Junosdrop-and-log) and sticky or dynamic learning as your policy dictates. - Watch the log for a week. Every violation message is either an attack, a device you did not count, or a maximum that is too low. Adjust the classes.
- Tighten the action. Move the ports to
shutdownwhere a locked port is acceptable, and enable automatic recovery so a tripped port comes back without a ticket. - Save the configuration if you use sticky learning, then confirm the sticky lines are in the startup configuration.
On Cisco IOS the end state for a phone-and-PC port looks like this. The addresses use the IANA documentation range for MAC addresses.
interface GigabitEthernet1/0/5
description desk 3-114, phone + PC
switchport mode access
switchport access vlan 20
switchport voice vlan 120
switchport nonegotiate
switchport port-security
switchport port-security maximum 3
switchport port-security violation restrict
switchport port-security mac-address sticky
!
errdisable recovery cause psecure-violation
errdisable recovery interval 300
The Junos equivalent on a switch with the Enhanced Layer 2 Software configuration style caps the interface and makes the learned entries persistent:
set interfaces ge-0/0/5 unit 0 family ethernet-switching interface-mode access
set interfaces ge-0/0/5 unit 0 family ethernet-switching vlan members data-20
set switch-options interface ge-0/0/5.0 interface-mac-limit 3 packet-action drop-and-log
set switch-options interface ge-0/0/5 persistent-learning
Keep the VLAN design itself in the VLAN reference for access and voice VLANs; port security only decides which addresses enter whichever VLAN the port already belongs to.
06 How do you check it worked?
Three checks cover it. First, the per-port summary: show port-security interface Gi1/0/5 on IOS reports the port status (Secure-up or Secure-shutdown), the violation mode, the maximum, the current address count, the sticky count and the security violation counter. Second, the address list: show port-security address lists every secure address with its type and remaining age. Third, a controlled violation: plug a spare laptop into a test port set to restrict with a maximum of one, confirm the laptop gets no DHCP lease, and watch the violation counter rise and a PSECURE_VIOLATION message reach the syslog server.
For ports set to shutdown, show interfaces status err-disabled lists the ports currently disabled and the reason. If you rely on automatic recovery, check show errdisable recovery to confirm the psecure-violation cause is enabled and the interval is what you expect. On Junos, show ethernet-switching table shows learned and persistent entries per interface, and the system log records the MAC limit being exceeded.
07 Common mistakes with port security
- Applying it to uplinks. A trunk to another switch carries every address behind that switch. Port security belongs on edge ports; protect the uplinks with spanning tree guard features such as root guard and with the link design, not with a MAC count.
- Forgetting the phone. A port with a voice VLAN learns the phone's address too. A maximum of one on such a port shuts the desk the first time the phone boots.
- Sticky without a save. Sticky entries live in the running configuration. A power cut before
copy running-config startup-configloses them, and the port relearns from whatever is plugged in when it comes back. - Virtual machines and docking stations. A laptop dock has its own MAC address; a laptop running a virtual machine in bridged mode presents a second address. Both look like an extra device.
- Recovery that hides the problem. An error-disable recovery interval of 300 seconds is convenient, but a port that trips every five minutes all day is telling you something. Alert on repeated violations from the same port.
- Treating it as identity. A cloned address passes. If the requirement is "only company laptops", the answer is 802.1X, with port security as the cap on top.
08 Where port security fits with the rest of the access layer
An access port in a well-run network carries several protections, each answering a different question. Port security answers "how many devices, and which addresses". 802.1X answers "who is this device". DHCP snooping answers "who may hand out addresses", and dynamic ARP inspection, built on the snooping table, answers "is this ARP reply telling the truth". BPDU guard answers "has someone plugged a switch into a user port". Storm control answers "is this port flooding the VLAN". None of them replaces another, and on most platforms they are configured on the same interface stanza, so a port template that sets them all at once is the practical way to keep a floor consistent.
09 Questions
What is the default port security violation mode on Cisco switches?
Shutdown. When a frame arrives from an address beyond the maximum, the port is error-disabled and stays down until an administrator bounces it or errdisable recovery for the psecure-violation cause brings it back. The default maximum is one secure address.
What is the difference between restrict and protect?
Both drop frames from addresses beyond the allowance and keep the port up. Restrict also sends a syslog message and SNMP trap and increments the violation counter, so you can see the event. Protect drops silently, which makes troubleshooting harder.
Do sticky MAC addresses survive a reload?
On Cisco IOS, only if the running configuration was saved, because sticky entries are written as configuration lines. On Junos, persistent-learning keeps learned addresses across restarts and link-down events without adding configuration lines.
Can port security stop MAC spoofing?
No. It checks the source address field, which any host can set. A device that copies an allowed address passes. Pair port security with 802.1X when the requirement is to admit only known devices.
Should port security be enabled on trunk ports?
Rarely. A trunk to another switch carries many addresses, and the count changes as hosts come and go. Use it on edge access ports, and protect switch-to-switch links with spanning tree guards and link design.
What maximum should I set for a port with an IP phone?
At least two, and often three, because the phone's address can be learned on both the voice and the data VLAN in addition to the PC behind it. Check the platform documentation and confirm with the forwarding table before enforcing.