Network Switch – MAC Learning, VLANs, STP, Port Security & Inter-VLAN Routing
1. What Is a Network Switch?
A network switch is an intelligent Layer 2 (and optionally Layer 3) device that interconnects devices within a LAN (Local Area Network). Unlike a hub — which blindly copies every incoming frame out every port, creating collisions — a switch learns which device is connected to each port and forwards frames only to the correct destination port.
Related pages: CAM Table | MAC Addresses | Access & Trunk Ports | VLANs | Trunking (802.1Q) | Root Bridge Election (STP) | Spanning Tree | show mac address-table | show vlan | show interfaces | EtherChannel | Routers | VLAN Configuration Lab | Inter-VLAN Routing Lab | Layer 2 Troubleshooting Lab
2. Switch vs Hub vs Router — OSI Layer Comparison
| Device | OSI Layer | Forwarding Basis | Collision Domain | Broadcast Domain | Intelligence |
|---|---|---|---|---|---|
| Hub | Layer 1 (Physical) | None — floods every port | One (all ports) | None — pure signal repeater | |
| Switch (L2) | Layer 2 (Data Link) | MAC addresses (CAM table) | One per port (full-duplex) | One per VLAN | Learns MAC-to-port mappings; forwards or floods |
| Switch (L3) | Layers 2 and 3 | MAC addresses AND IP addresses | One per port | One per VLAN; routes between VLANs | Full L2 switching + hardware-based IP routing. See Layer 3 Switch Routing. |
| Router | Layer 3 (Network) | IP addresses (routing table) | One per interface | One per interface (breaks broadcast domains) | Routing table; inter-network forwarding; NAT, ACLs |
3. Types of Switches
| Type | Description | Configurable? | Typical Use Case |
|---|---|---|---|
| Unmanaged | Plug-and-play; no configuration interface. Fixed functionality. | No | Small offices, home networks, simple device connectivity where VLANs and security are not needed |
| Managed | Full CLI/GUI/SNMP access. Supports VLANs, STP, QoS, port security, SNMP monitoring, ACLs. | Yes | Enterprise networks requiring segmentation, security, redundancy, and monitoring |
| Smart / Partially Managed | Web GUI only; limited feature set compared to fully managed. Typically supports basic VLANs and QoS. | Limited | Small-medium businesses needing basic VLAN support without enterprise complexity |
| Layer 2 (L2) | Forwards frames based on MAC addresses only. No IP routing. | Yes (managed) | Access layer — connecting end devices (PCs, phones, printers) to the network |
| Layer 3 (L3) | Performs both MAC-based switching AND IP-based routing (inter-VLAN routing) in hardware ASIC. Much faster than router-on-a-stick. | Yes | Distribution and core layers; inter-VLAN routing without a separate router (Cisco Catalyst 3850, 9300) |
| Stackable | Multiple physical chassis interconnected via a high-speed stacking bus and managed as a single logical device. Add/remove stack members without downtime. | Yes | Enterprise access layer needing scalability — add switches as port count grows; one management IP for entire stack |
| Modular / Chassis | Chassis-based with field-replaceable line cards (port modules), supervisor engines, and power supplies. Highly resilient. | Yes | Core and data centre — large port density, redundant supervisors (Cisco Catalyst 6500, 9400) |
| PoE Switch | Delivers 802.3af/at/bt power over Ethernet to connected devices alongside data. | Yes | Powering IP phones, wireless APs, IP cameras, and other PoE devices without separate power supplies |
4. MAC Address Learning and Frame Forwarding
The switch's entire intelligence rests on its CAM table (Content Addressable Memory table, also called the MAC address table). Every frame received triggers two actions: learn the source MAC and look up the destination MAC.
See CAM Table and show mac address-table
for full MAC learning and CAM table details.
Frame Switching Methods
| Method | How It Works | Latency | Error Checking | When Used |
|---|---|---|---|---|
| Store-and-Forward | Receives entire frame into buffer, checks FCS (CRC) for errors, THEN forwards | Higher (entire frame must arrive first) | Full — corrupted frames are dropped before forwarding | Default on most modern Cisco switches; required for QoS classification and different-speed port connections |
| Cut-Through | Forwards immediately after reading destination MAC (first 6 bytes of frame header) | Lower — starts forwarding in ~14 bytes | None — corrupted frames can propagate | Low-latency environments (HFT, storage networks); when all ports same speed |
| Fragment-Free | Reads first 64 bytes (minimum Ethernet frame) before forwarding; catches collision fragments | Moderate | Partial — catches runts (collision fragments under 64B) | Compromise between store-and-forward and cut-through; reduces collision fragment propagation |
5. VLANs — Virtual LANs
VLANs logically partition a single physical switch into multiple independent broadcast domains. Devices in different VLANs cannot communicate at Layer 2 even if physically connected to the same switch — a router or Layer 3 switch is required for inter-VLAN communication.
VLAN Configuration — Cisco IOS
! ── Create VLANs ────────────────────────────────────────────────────────── Switch(config)# vlan 10 Switch(config-vlan)# name Finance Switch(config-vlan)# exit Switch(config)# vlan 20 Switch(config-vlan)# name HR Switch(config-vlan)# exit ! ── Assign access port to VLAN ──────────────────────────────────────────── Switch(config)# interface GigabitEthernet0/1 Switch(config-if)# switchport mode access Switch(config-if)# switchport access vlan 10 Switch(config-if)# spanning-tree portfast ! For end-device ports Switch(config-if)# spanning-tree bpduguard enable ! ── Configure trunk port (between switches) ─────────────────────────────── Switch(config)# interface GigabitEthernet0/24 Switch(config-if)# switchport mode trunk Switch(config-if)# switchport trunk encapsulation dot1q ! Older IOS Switch(config-if)# switchport trunk allowed vlan 10,20,30 Switch(config-if)# switchport trunk native vlan 99 ! Change from default VLAN 1 ! ── Verify ──────────────────────────────────────────────────────────────── Switch# show vlan brief VLAN Name Status Ports ---- ---------------------- --------- ---------------------------------- 1 default active Gi0/2, Gi0/3, Gi0/4 10 Finance active Gi0/1 20 HR active Gi0/5, Gi0/6 30 IT active Gi0/7, Gi0/8 1002 fddi-default act/unsup ... Switch# show interfaces trunk Port Mode Encap Status Native vlan Gi0/24 on 802.1q trunking 99 Port Vlans allowed on trunk Gi0/24 10,20,30 Port Vlans allowed and active in management domain Gi0/24 10,20,30
See show vlan for detailed output annotation,
Access & Trunk Ports for the full port configuration reference,
and VLANs for the conceptual overview.
Hands-on practice: VLAN Configuration Lab |
Assigning VLANs to Switch Ports Lab.
802.1Q VLAN Tagging
See Trunking (802.1Q) for full 802.1Q trunk configuration detail.
6. Inter-VLAN Routing — Layer 3 Switch vs Router-on-a-Stick
Devices in different VLANs cannot communicate at Layer 2. To route between VLANs, two approaches exist: router-on-a-stick (single router interface with sub-interfaces) or a Layer 3 switch with SVIs (Switched Virtual Interfaces).
Method 1: Router-on-a-Stick
Topology: Switch --trunk--> Router Gi0/0 Router Gi0/0 has sub-interfaces for each VLAN. ! Switch side -- configure trunk to router: Switch(config)# interface Gi0/24 Switch(config-if)# switchport mode trunk ! Router side -- sub-interfaces: Router(config)# interface GigabitEthernet0/0.10 Router(config-subif)# encapsulation dot1Q 10 Router(config-subif)# ip address 192.168.10.1 255.255.255.0 Router(config)# interface GigabitEthernet0/0.20 Router(config-subif)# encapsulation dot1Q 20 Router(config-subif)# ip address 192.168.20.1 255.255.255.0 Router(config)# interface GigabitEthernet0/0.30 Router(config-subif)# encapsulation dot1Q 30 Router(config-subif)# ip address 192.168.30.1 255.255.255.0 Router(config)# interface GigabitEthernet0/0 Router(config-if)# no shutdown ! Bring up parent interface ! Default gateways: VLAN 10 hosts use 192.168.10.1, etc. ! Limitation: all inter-VLAN traffic passes through router -- single link bottleneck
Method 2: Layer 3 Switch with SVIs (Preferred)
! Enable IP routing on L3 switch: L3Switch(config)# ip routing ! Create SVIs (one per VLAN -- acts as default gateway): L3Switch(config)# interface vlan 10 L3Switch(config-if)# ip address 192.168.10.1 255.255.255.0 L3Switch(config-if)# no shutdown L3Switch(config-if)# description Gateway-for-VLAN10 L3Switch(config)# interface vlan 20 L3Switch(config-if)# ip address 192.168.20.1 255.255.255.0 L3Switch(config-if)# no shutdown ! Traffic between VLAN 10 and VLAN 20 routes in hardware ASIC -- ! no external router needed, wire-speed performance. ! Verify SVIs: L3Switch# show interfaces vlan 10 Vlan10 is up, line protocol is up Internet address is 192.168.10.1/24 L3Switch# show ip route C 192.168.10.0/24 is directly connected, Vlan10 C 192.168.20.0/24 is directly connected, Vlan20
See Inter-VLAN Routing Lab for hands-on practice with both methods.
7. Spanning Tree Protocol (STP)
STP (IEEE 802.1D) prevents Layer 2 loops in networks with redundant switch links. Without STP, a broadcast frame would loop indefinitely between switches, consuming all bandwidth within seconds.
! Verify STP root and port roles:
Switch# show spanning-tree vlan 10
VLAN0010
Root ID Priority 4097
Address 0011.1111.1111
This bridge is the root
...
Interface Role Sts Cost Prio.Nbr Type
Gi0/1 Desg FWD 4 128.1 P2p
Gi0/2 Desg FWD 4 128.2 P2p
Gi0/3 Altn BLK 4 128.3 P2p <-- blocked to prevent loop
! Configure root bridge (recommended over relying on MAC tiebreaker):
Switch(config)# spanning-tree vlan 10 root primary
Switch(config)# spanning-tree vlan 20 root secondary
See Root Bridge Election, STP Port Roles, Spanning Tree Protocol, and PVST+ for detailed STP coverage. PortFast & BPDU Guard should be configured on all access ports.
8. Switch Port Types and Configuration
| Port Type | Description | Configuration | Use Case |
|---|---|---|---|
| Access Port | Carries traffic for exactly one VLAN. Frames are NOT tagged (untagged). Connected device is unaware of VLANs. | switchport mode accessswitchport access vlan [id] |
PC, printer, IP phone, server connected to a specific VLAN |
| Trunk Port | Carries traffic for multiple VLANs. Frames are 802.1Q tagged with VLAN ID (except native VLAN frames). | switchport mode trunkswitchport trunk allowed vlan [list] |
Switch-to-switch links, switch-to-router (router-on-a-stick), switch-to-AP (multiple SSIDs as VLANs) |
| Routed Port (L3) | Behaves like a router interface on an L3 switch. Has an IP address; does not do Layer 2 switching. | no switchportip address [addr] [mask] |
Uplink from distribution L3 switch to core router or firewall |
| SVI (Switched Virtual Interface) | Virtual interface for a VLAN on an L3 switch. Acts as the default gateway for that VLAN. | interface vlan [id]ip address [addr] [mask] |
Inter-VLAN routing gateway; VLAN management access |
See Access & Trunk Ports for the complete port configuration guide.
9. Port Security
Port security restricts which MAC addresses can communicate through a switch port. It limits the maximum number of MACs and can lock a port to specific known MACs — preventing rogue device connections.
! Configure port security on an access port: Switch(config)# interface GigabitEthernet0/5 Switch(config-if)# switchport mode access Switch(config-if)# switchport access vlan 10 Switch(config-if)# switchport port-security ! Enable port security Switch(config-if)# switchport port-security maximum 1 ! Allow only 1 MAC Switch(config-if)# switchport port-security violation shutdown ! Err-disable on violation Switch(config-if)# switchport port-security mac-address sticky ! Auto-learn & lock MAC ! Violation modes: ! shutdown -- err-disables port (default); manual recovery needed ! restrict -- drops violating frames + syslog; port stays UP ! protect -- drops violating frames silently; NO syslog ! Verify port security: Switch# show port-security interface GigabitEthernet0/5 Port Security : Enabled Port Status : Secure-up Violation Mode : Shutdown Maximum MAC Addresses : 1 Sticky MAC Addresses : 1 Security Violation Count : 0 ! Recover an err-disabled port: Switch(config)# interface GigabitEthernet0/5 Switch(config-if)# shutdown Switch(config-if)# no shutdown
See Port Security Overview, Sticky MAC, Violation Modes, and Port Security & Sticky MAC Lab.
10. Layer 2 Security Features
| Feature | What It Prevents | How It Works | Key Command |
|---|---|---|---|
| Port Security | Rogue device connections; MAC flooding attacks | Limits MACs per port; violation triggers action (shutdown/restrict/protect) | switchport port-security |
| DHCP Snooping | Rogue DHCP servers assigning incorrect IP addresses to clients (starvation, spoofing) | Marks ports as trusted (uplinks, real DHCP servers) or untrusted (end-device ports). DHCP Offer/Ack only allowed from trusted ports. Builds binding table (MAC-IP-port-VLAN) | ip dhcp snoopingip dhcp snooping vlan [id] |
| Dynamic ARP Inspection (DAI) | ARP spoofing / ARP poisoning attacks (man-in-the-middle) | Validates ARP packets against the DHCP snooping binding table. Discards ARP with MAC-IP bindings not in the table. Requires DHCP Snooping to be configured first. | ip arp inspection vlan [id] |
| 802.1X Port Authentication | Unauthenticated device access to the network | Port blocked until device authenticates via RADIUS (EAP). Authentication before any Layer 2 frames pass. See AAA Authentication Methods. | dot1x system-auth-control |
| PortFast + BPDU Guard | Rogue switch connecting to access port; accidental STP topology changes from end-device ports | PortFast skips STP listening/learning on access ports. BPDU Guard err-disables port if any BPDU received. | spanning-tree portfastspanning-tree bpduguard enable |
| Storm Control | Broadcast/multicast/unicast storms degrading network | Monitors traffic rate per port; shuts down or drops when threshold exceeded (% bandwidth or pps) | storm-control broadcast level [%] |
DHCP Snooping and DAI Configuration
! ── DHCP Snooping ───────────────────────────────────────────────────────── Switch(config)# ip dhcp snooping Switch(config)# ip dhcp snooping vlan 10,20,30 ! Mark uplink/server ports as trusted: Switch(config)# interface GigabitEthernet0/24 Switch(config-if)# ip dhcp snooping trust ! All other (access) ports are untrusted by default. ! Verify: Switch# show ip dhcp snooping DHCP snooping is configured on the following VLANs: 10,20,30 Interface Trusted Rate limit (pps) Gi0/24 yes unlimited Gi0/1 no 15 Switch# show ip dhcp snooping binding MacAddress IpAddress Lease Type VLAN Interface 00:11:AA:BB:CC:01 192.168.10.50 86400 dhcp-sn 10 Gi0/1 ! ── Dynamic ARP Inspection ──────────────────────────────────────────────── Switch(config)# ip arp inspection vlan 10,20,30 ! Uplink ports must also be trusted for ARP inspection: Switch(config)# interface GigabitEthernet0/24 Switch(config-if)# ip arp inspection trust
See DHCP Snooping, Dynamic ARP Inspection, and DHCP Snooping & DAI Lab.
11. EtherChannel (Link Aggregation)
EtherChannel bundles multiple physical links between two switches into one logical link, providing higher bandwidth and redundancy. If one physical link fails, traffic continues on the remaining links with no STP reconvergence.
! Configure LACP EtherChannel (recommended -- IEEE standard):
Switch(config)# interface range GigabitEthernet0/1 - 2
Switch(config-if-range)# channel-group 1 mode active ! LACP active
Switch(config-if-range)# channel-protocol lacp
! Configure the Port-Channel interface:
Switch(config)# interface Port-channel1
Switch(config-if)# switchport mode trunk
Switch(config-if)# switchport trunk allowed vlan 10,20,30
! Alternative: PAgP (Cisco proprietary):
Switch(config-if-range)# channel-group 1 mode desirable ! PAgP
! Verify:
Switch# show etherchannel summary
Flags: D - down P - bundled in port-channel
U - in use S - Layer2
Group Port-channel Protocol Ports
------+-------------+-----------+-------------------------------------------
1 Po1(SU) LACP Gi0/1(P) Gi0/2(P)
See EtherChannel Load Balancing for hashing algorithms and load distribution details.
12. Switch Management
! ── Management VLAN and IP address ─────────────────────────────────────── Switch(config)# vlan 99 Switch(config-vlan)# name Management Switch(config)# interface vlan 99 Switch(config-if)# ip address 10.0.0.10 255.255.255.0 Switch(config-if)# no shutdown Switch(config)# ip default-gateway 10.0.0.1 ! ── SSH access (secure -- replace Telnet) ────────────────────────────── Switch(config)# hostname SW-Core-01 Switch(config)# ip domain-name netstuts.com Switch(config)# crypto key generate rsa modulus 2048 Switch(config)# ip ssh version 2 Switch(config)# line vty 0 15 Switch(config-line)# transport input ssh Switch(config-line)# login local Switch(config)# username admin privilege 15 secret AdminP@ss ! ── Disable Telnet on VTY ───────────────────────────────────────────── Switch(config)# line vty 0 15 Switch(config-line)# transport input ssh ! SSH only; reject Telnet ! ── Enable SNMP for monitoring ──────────────────────────────────────── Switch(config)# snmp-server community PUBLIC ro Switch(config)# snmp-server community PRIVATE rw Switch(config)# snmp-server host 10.0.0.100 version 2c PUBLIC ! ── Console password ────────────────────────────────────────────────── Switch(config)# line console 0 Switch(config-line)# password ConsoleP@ss Switch(config-line)# login Switch(config-line)# exec-timeout 5 0 ! Auto-logout after 5 minutes
See SSH Configuration for the full SSH setup guide,
SNMP for monitoring configuration,
and show running-config to verify the management configuration.
13. Switch Troubleshooting — Commands and Scenarios
| Symptom | Likely Cause | Diagnostic Command | Fix |
|---|---|---|---|
| PC cannot ping gateway (same VLAN) | Port not in correct VLAN; port in wrong mode (trunk instead of access) | show interfaces Gi0/x switchportshow vlan brief |
Verify switchport access vlan [id];
confirm switchport mode access |
| VLANs not passing between switches | Trunk not configured; VLAN not in allowed list; native VLAN mismatch | show interfaces trunk |
Verify trunk mode on both ends; check allowed VLAN list; align native VLAN on both switches. See Layer 2 Troubleshooting Lab. |
| Broadcast storm / MAC flapping | Layer 2 loop — STP not blocking redundant port; unmanaged switch connected without STP | show spanning-treeshow logging | include MACFLAP |
Verify STP running; check all ports for unexpected FWD state; enable STP on any unmanaged connections |
| Port in err-disabled state | Port security violation; BPDU Guard triggered; Loop detected by loop-guard | show interfaces status err-disabledshow port-security interface Gi0/x |
Investigate cause; remove offending device;
shutdown then no shutdown;
or configure errdisable auto-recovery |
| DHCP not working on a VLAN | No DHCP server for VLAN; DHCP Snooping blocking; missing ip helper-address on L3 SVI | show ip dhcp snoopingshow ip dhcp binding |
Check DHCP snooping trust on uplinks; add
ip helper-address on VLAN SVI if DHCP
server is on a different subnet |
Essential Troubleshooting Commands
Switch# show mac address-table ! CAM table entries and ports Switch# show interfaces status ! All ports: speed, duplex, VLAN, status Switch# show interfaces GigabitEthernet0/1 ! Detailed counters: errors, CRC, drops Switch# show vlan brief ! VLAN IDs, names, assigned ports Switch# show interfaces trunk ! Trunk ports, allowed VLANs, native VLAN Switch# show interfaces GigabitEthernet0/1 switchport ! Port mode, VLAN assignment Switch# show spanning-tree vlan 10 ! STP roles and states per VLAN Switch# show port-security interface Gi0/1 ! Port security config and violations Switch# show ip dhcp snooping binding ! DHCP snooping binding table Switch# show ip arp inspection vlan 10 ! DAI statistics Switch# show etherchannel summary ! EtherChannel port-channel status Switch# show version ! IOS version, platform, uptime
See show interfaces,
show vlan,
show mac address-table, and
ping for detailed command references.
For complex layer 2 issues see
Layer 2 Troubleshooting Lab.
14. Key Points & Exam Tips
- Switches operate at Layer 2 (MAC addresses). Layer 3 switches also perform IP routing. Hubs are Layer 1 (no intelligence). Routers are Layer 3 (IP addresses).
- MAC learning: Switch records source MAC + ingress port on every frame. Destination lookup: found = unicast forward; not found = flood (unknown unicast). Aging default = 300 seconds. See CAM Table.
- Store-and-forward = full error checking, higher latency. Cut-through = lowest latency, no error checking. Default on modern Cisco switches = store-and-forward.
- Access port = one VLAN, untagged frames, end devices. Trunk port = multiple VLANs, 802.1Q tagged, between switches or to routers/APs.
- Native VLAN on trunk ports sends frames untagged. Both sides must match or VLAN hopping is possible. Best practice: change from VLAN 1 to unused VLAN.
- Inter-VLAN routing: router-on-a-stick (router with
sub-interfaces, single trunk) or L3 switch with SVIs and
ip routing(faster, no external router needed). - STP prevents Layer 2 loops. Root Bridge = lowest BID. Root Port = best path to root. Designated Port = best for a segment. Blocked ports prevent loops. All root bridge ports = Designated. See Root Bridge Election.
- Port security violation modes: shutdown (err-disables, logs), restrict (drops + logs, port UP), protect (drops silently, no log).
- DHCP Snooping prevents rogue DHCP servers. Trusted ports (uplinks/servers) vs untrusted (access). Builds binding table used by DAI. DAI prevents ARP spoofing.
- EtherChannel bundles links for bandwidth + redundancy. LACP
(IEEE,
mode active/passive) or PAgP (Cisco,mode desirable/auto). STP treats bundle as one port. - Management: use SSH (not Telnet) on VTY lines;
configure a management VLAN SVI; monitor with SNMP.
Verify with
show running-config.
Related pages: CAM Table | Access & Trunk Ports | Root Bridge Election | STP Port Roles | Spanning Tree | PVST+ | show mac address-table | show vlan | show interfaces | EtherChannel | PortFast & BPDU Guard | Port Security | DHCP Snooping | Dynamic ARP Inspection | VLAN Configuration Lab | Inter-VLAN Routing Lab | Port Security Lab | DHCP Snooping & DAI Lab | Layer 2 Troubleshooting Lab
15. Network Switch Quiz
Related Topics & Step-by-Step Tutorials
Deepen your switching knowledge:
- Frame Forwarding — how switches decide where to send frames
- CAM Table – MAC Address Table Explained — MAC address table — learning, aging, flooding
- MAC Address – Format, Types, and Role in Layer 2 Net… — MAC address structure and types
- VLANs – Concepts & Overview — segmenting a switch with VLANs
- Access and Trunk Ports — access vs trunk port configuration
- Port Security – Concepts & Overview — locking ports to specific MAC addresses
- Spanning Tree Protocol – Concepts & Operation — Spanning Tree Protocol to prevent loops
- PVST and PVST+ — Per-VLAN Spanning Tree
- RSTP – Rapid Spanning Tree Protocol — Rapid Spanning Tree — faster convergence
- PortFast & BPDU Guard – STP Optimisation and Access-… — PortFast and BPDU Guard for access ports
- EtherChannel Configuration – Complete Guide — bundling links with EtherChannel / LACP
- VLAN Creation and Management (Step-by-Step)
- Trunk Port Configuration (802.1Q) (Step-by-Step)
- Port Security & Sticky MAC Configuration (Step-by-Step)
- Spanning Tree Protocol (STP) — Root Bridge Election (Step-by-Step)
- MAC Address Table Management (Step-by-Step)