Spanning Tree (STP) Explained: Build a Switch Loop and Watch STP Fix It (Network+)
What you’ll know
Why two cables between switches can take a network down, how the Spanning Tree Protocol (STP) stops it by blocking one path, the port states you’ll see, and what Rapid STP changes. Then build a loop with two virtual switches and watch STP block it and recover when a cable is pulled.
Why it matters. Redundant links between switches are good: if one cable dies, traffic should take the other. But Ethernet has no time-to-live, so a broadcast caught in a loop goes round forever, multiplying until the network stops. STP lets you keep the spare cable without the meltdown.
How STP works, in four steps
- Elect a root bridge. Every switch has a bridge ID (a priority, 32768 by default, plus its MAC address). The lowest ID wins. Set the priority yourself so the root is a strong core switch, not a random old one.
- Each other switch finds its best way to the root, by lowest path cost. That port is its root port.
- Each link gets one designated port, the one closest to the root, which forwards for that link.
- Everything else is blocked. A blocked port still listens for STP messages (BPDUs), so if the active path fails it can take over.
| Port state (802.1D) | What it does |
|---|---|
| Blocking | Drops data frames, listens to BPDUs. This is what breaks the loop. |
| Listening | Taking part in the election, not learning MACs yet |
| Learning | Learning MAC addresses, not forwarding yet |
| Forwarding | Normal traffic |
| Disabled | Port is shut or unplugged |
Classic STP takes about 30 to 50 seconds to move a port to forwarding. Rapid STP (802.1w, RSTP) does the same job in a second or two and is what modern switches run. Related exam words: BPDU guard shuts a user port that suddenly sends STP messages, and PortFast (edge port) lets a port to a single PC skip the wait.
A broadcast storm is what happens when the network finds out about recursion. STP is the adult in the room. — Nyx
Try it: two virtual switches, two cables, one loop
In Ubuntu (WSL2) or any Linux box, as root. These are virtual interfaces only: nothing touches your real network, and the last step removes them.
$ sudo -i # ip link add br1 type bridge stp_state 1 # ip link add br2 type bridge stp_state 1 # ip link set br1 type bridge priority 4096 # ip link add a1 type veth peer name b1 # ip link add a2 type veth peer name b2 # ip link set a1 master br1; ip link set a2 master br1 # ip link set b1 master br2; ip link set b2 master br2 # for i in br1 br2 a1 a2 b1 b2; do ip link set $i up; done # bridge link b1@a1: ... master br2 state listening a1@b1: ... master br1 state listening b2@a2: ... master br2 state listening a2@b2: ... master br1 state listening
br1 and br2 are two switches. a1–b1 and a2–b2 are two cables between them: a loop. We set br1’s priority to 4096 so it wins the root election. Right away, every port is listening.
# bridge link b1@a1: ... master br2 state forwarding a1@b1: ... master br1 state forwarding b2@a2: ... master br2 state blocking a2@b2: ... master br1 state forwarding # cat /sys/class/net/br1/bridge/root_id /sys/class/net/br2/bridge/root_id 1000.0240683448b0 1000.0240683448b0
STP blocked one port on br2 (b2), so there is only one active path. Both switches agree on the root: 1000 is priority 4096 in hex, followed by br1’s MAC. Your MAC values will differ.
# ip link set a1 down # sleep 10; bridge link | grep b2 b2@a2: ... master br2 state listening # sleep 10; bridge link | grep b2 b2@a2: ... master br2 state learning # sleep 20; bridge link | grep b2 b2@a2: ... master br2 state forwarding
The first cable is gone, so the blocked port wakes up: listening, then learning, then forwarding, about 40 seconds in our test. Traffic now takes the spare cable. That wait is classic STP; RSTP would switch in a second or two.
# for i in br1 br2 a1 a2; do ip link del $i; done # exit
Deleting one end of each veth pair removes both ends.
On the exam and in real life
- A sudden, network-wide slowdown with switch lights flashing together: suspect a loop (someone patched two wall ports together).
- Set the root bridge on purpose with a low priority on your core switch.
- Use RSTP, PortFast on user ports, and BPDU guard so a desk switch can’t take over as root.
Where this fits
This is part of the free Network+ roadmap: every hands-on networking lesson on StashGrid, in order, tagged by exam domain.
NextNTP and time sync: why every log needs the right time→
Go deeper
- VLANs and 802.1Q lab — more layer 2 switching
- High availability and documentation — redundant links and failover
- The OSI model — why STP is a layer 2 job
Explainer, written by StashGrid with AI help and checked against the CompTIA Network+ (N10-009) exam objectives before publishing. Every command was run in Ubuntu 24.04 (WSL2) and Windows 11 on 1 Nov 2026, and the output shown is what we got. StashGrid isn’t affiliated with CompTIA. Spotted an error? Tell us.