Layer 2 Switching — How Switches Forward Frames and Learn MAC Addresses
Layer 2 switching forwards frames using MAC addresses. Here are the three switch functions, the CAM table, and…

Port security restricts which MAC addresses are allowed on a switch port and how many. If an unexpected device connects, the switch takes an action you define — drop its traffic, log it, or shut the port down.
It defends against two things specifically: someone unplugging an authorised device and connecting their own, and MAC flooding, where an attacker fills the switch’s MAC address table so it starts flooding all traffic out of every port and can be captured.
The port must be a static access or trunk port first — port security cannot be enabled on a port left in dynamic (DTP-negotiating) mode.
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)# switchport port-security aging time 60
Switch(config-if)# switchport port-security aging type inactivity| Command | What it does |
|---|---|
switchport port-security | Enables the feature. Default: maximum 1, violation shutdown. |
maximum 2 | Allows two MAC addresses. Use 2 where an IP phone has a PC behind it. |
mac-address sticky | Learns addresses dynamically then writes them into the running config as if they were static. |
mac-address 0011.2233.4455 | Pins one specific address manually. |
violation restrict | Sets what happens on a violation — see below. |
aging time 60 | Removes learned addresses after 60 minutes. |
aging type inactivity | Ages only when the address is idle, rather than on a fixed timer. |
Sticky learning is the practical option for a real deployment. Manually entering hundreds of MAC addresses does not scale; sticky learns whatever is plugged in now and locks the port to it. Remember to write memory — sticky addresses go into the running config, and are lost on reload if not saved.
| Mode | Traffic from the offending MAC | Port state | Syslog | SNMP trap | Violation counter |
|---|---|---|---|---|---|
| protect | Dropped | Stays up | No | No | No |
| restrict | Dropped | Stays up | Yes | Yes | Yes |
| shutdown (default) | All traffic stops | err-disabled | Yes | Yes | Yes |
protect is almost never the right choice — it fails silently, so you find out about the problem from a user rather than from monitoring.
restrict is usually the best balance: the unauthorised device is blocked, the authorised one keeps working, and you get an alert.
shutdown is the default and the strictest. It also means anyone who plugs the wrong laptop into a wall port takes that port offline until someone re-enables it — which generates a lot of helpdesk work unless you configure automatic recovery.
When a port goes err-disabled, it stays down until manually cleared:
Switch(config)# interface GigabitEthernet0/5
Switch(config-if)# shutdown
Switch(config-if)# no shutdownOr configure the switch to recover on its own:
Switch(config)# errdisable recovery cause psecure-violation
Switch(config)# errdisable recovery interval 300The port comes back automatically after 300 seconds. If the offending device is still connected, it will trip again — which is the correct behaviour.
Switch# show port-security
Switch# show port-security interface GigabitEthernet0/5
Switch# show port-security address
Switch# show interfaces status err-disabled
Switch# show errdisable recoveryshow port-security interface is the one to run first — it shows the violation mode, maximum, current count, last violating MAC address, and the violation counter.
A phone with a PC daisy-chained behind it puts two MAC addresses on one port, across two VLANs:
Switch(config-if)# switchport mode access
Switch(config-if)# switchport access vlan 10
Switch(config-if)# switchport voice vlan 20
Switch(config-if)# switchport port-security maximum 3
Switch(config-if)# switchport port-security maximum 2 vlan access
Switch(config-if)# switchport port-security maximum 1 vlan voice
Switch(config-if)# switchport port-security violation restrict
Switch(config-if)# switchport port-security mac-address stickySetting a global maximum of 1 on a voice port is a common mistake — the phone alone consumes the entry and the PC behind it is blocked.
| Port security | 802.1X | |
|---|---|---|
| Identifies devices by | MAC address | Credentials or a certificate |
| Can be defeated by | MAC spoofing — trivially | Requires stealing credentials or a certificate |
| Needs a RADIUS server | No | Yes |
| Dynamic VLAN assignment | No | Yes |
| Effort to deploy | Low | High |
Be honest about what port security achieves. A MAC address is visible to anyone on the segment and can be changed with one command on any operating system. Port security stops casual unauthorised connections and MAC flooding; it does not stop a motivated attacker. 802.1X is the real access control, and port security is the pragmatic option when you do not have RADIUS.
violation restrict, not shutdown. Watch the logs for a week before tightening.maximum realistically — 2 or 3, not 1, since docking stations, hypervisors and virtual machines all present extra MAC addresses.errdisable recovery so a mistake does not require a site visit.These are complementary; deployed together on access ports they close most of the Layer 2 attack surface. See the full switching guide.
Once switchport port-security is entered: maximum 1 MAC address, violation mode shutdown, dynamic learning with no ageing. The very first device to connect claims the port.
A violation occurred — more MAC addresses than the maximum, or an address not in the allowed list. Run show port-security interface to see the last violating MAC, then shutdown / no shutdown to recover.
Technically yes, but it is a bad idea. A trunk to another switch carries every MAC address behind it, so any sensible maximum is either useless or will shut the link down.
No. An attacker who observes an authorised MAC address can set their own interface to match it and the switch will accept it. Port security raises the effort slightly; 802.1X is the actual control.
Static addresses are typed in by hand. Sticky addresses are learned automatically and then written into the running configuration, giving the same result with far less work — provided you save the config.
Platform dependent, commonly up to 132 or more per port, with a switch-wide limit set by the SDM template in use. For access ports the practical answer is 2 or 3.