beginner

VLANs and Trunking Fundamentals

A VLAN splits one physical switch into several separate broadcast domains. Learn why we use VLANs, access and trunk ports, 802.1Q tagging and the native VLAN, how traffic moves between VLANs, voice VLANs, and common mistakes. Then simulate which ports a frame floods to on a VLAN-aware switch and build an 802.1Q tag.

50 min

1. Why VLANs exist

Without VLANs, every device connected to a switch (and to switches linked to it) shares one broadcast domain. In a growing office that means more noise, weaker security (everyone can reach everyone at layer 2) and no way to separate departments, guests, phones and servers except by buying more switches.

A VLAN (virtual LAN) assigns switch ports to separate logical networks. VLAN 10 might be staff, VLAN 20 servers, VLAN 30 voice, VLAN 99 management. Devices in different VLANs do not see each other’s broadcasts and cannot communicate at layer 2.

Benefits:

  • Smaller broadcast domains: less wasted traffic.
  • Security and policy: put guests on a VLAN that can only reach the internet; filter traffic between VLANs at a router or firewall.
  • Flexibility: group users by function, not by where they sit.
  • Easier management: one physical switch serves many logical networks.

By convention each VLAN maps to one IP subnet (VLAN 10 uses 10.1.10.0/24, and so on).

2. Access ports and trunk ports

  • Access port: belongs to exactly one VLAN. The attached device (PC, printer) knows nothing about VLANs; frames are sent and received untagged. The switch adds the VLAN identity internally.
  • Trunk port: carries many VLANs over one link between switches, or between a switch and a router, firewall, hypervisor or access point. Frames are tagged so the far end knows which VLAN they belong to.
Switch A                          Switch B
Fa0/1 [VLAN 10]  ==== Gi0/24 (trunk: 10, 20) ====  Gi0/24  Fa0/3 [VLAN 10]
Fa0/3 [VLAN 20]                                            Fa0/5 [VLAN 20]

PCs in VLAN 10 on both switches are in the same broadcast domain even though they are on different switches. PCs in VLAN 20 are in a different one.

3. 802.1Q tagging

The IEEE 802.1Q standard inserts a 4-byte tag into the Ethernet frame:

Field Bits Meaning
TPID 16 0x8100, marks the frame as tagged
PCP 3 Priority (class of service, 0 to 7)
DEI 1 Drop eligible
VLAN ID 12 The VLAN (1 to 4094 usable)

The tag raises the maximum frame size from 1518 to 1522 bytes; switches handle this automatically. The last 16 bits (PCP, DEI and VLAN ID) combine as (priority << 13) | vlan_id, which the lab calculates.

The native VLAN

On an 802.1Q trunk, frames of one VLAN, the native VLAN, can be sent untagged. Both ends must agree on which VLAN is native (the default is VLAN 1). A mismatch merges two VLANs by accident and causes warnings. For security, do not use VLAN 1 for user traffic, set an unused VLAN as the native VLAN on trunks, and tag the native VLAN where supported.

4. Typical VLAN plan

VLAN Name Subnet Purpose
10 Staff 10.1.10.0/24 Employee computers
20 Servers 10.1.20.0/24 Internal servers
30 Voice 10.1.30.0/24 IP phones (QoS priority)
40 Guest 10.1.40.0/24 Internet only
99 Management 10.1.99.0/24 Switch and AP management
999 Parking none Unused ports, shut down

A voice VLAN lets a phone and the PC plugged into it share one switch port: the phone’s frames are tagged with the voice VLAN, the PC’s frames stay untagged in the access VLAN, and QoS marks the voice traffic.

5. Moving between VLANs

VLANs are separate layer 2 networks, so moving between them needs routing:

  • Router on a stick: one trunk link to a router with a subinterface per VLAN (interface Gi0/0.10, encapsulation dot1Q 10, ip address 10.1.10.1 255.255.255.0). Simple, limited by the single link.
  • Layer 3 switch: creates a switch virtual interface (SVI) per VLAN (interface vlan 10, ip address 10.1.10.1 ...) and routes at hardware speed. The standard design.
  • Firewall: when traffic between zones must be inspected.

Each host uses the interface address on its VLAN as the default gateway. Apply ACLs on the routing device to control which VLANs can talk (for example, guests may not reach servers).

6. Basic configuration (Cisco IOS style)

vlan 10
 name STAFF
vlan 20
 name SERVERS
!
interface FastEthernet0/1
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet0/24
 switchport trunk encapsulation dot1q   ! only on platforms that support ISL too
 switchport mode trunk
 switchport trunk allowed vlan 10,20
 switchport trunk native vlan 999

Verify with show vlan brief, show interfaces trunk and show interfaces status. Other vendors use different words (tagged and untagged membership, or “hybrid” ports) for the same ideas.

7. Common mistakes and security

  • VLAN exists on one switch but not the other, or is not allowed on the trunk: devices cannot talk across switches.
  • Native VLAN mismatch between trunk ends.
  • Wrong access VLAN on a port: the PC gets an address from the wrong DHCP scope.
  • Missing gateway/SVI: devices communicate within the VLAN but not outside.
  • Allowing all VLANs on every trunk: prune trunks to only the VLANs needed.
  • Unused ports left active in the default VLAN: shut them down and park them in an unused VLAN.
  • VLAN hopping attacks (switch spoofing and double tagging): disable dynamic trunk negotiation (switchport nonegotiate), set access ports explicitly, and avoid using the native VLAN for users.
  • VLANs are not a complete security boundary by themselves: pair them with ACLs, firewalls and 802.1X.

8. Hands-on: VLAN-aware forwarding

You are given a switch as a dictionary: access ports have {"mode": "access", "vlan": N}, trunk ports have {"mode": "trunk", "allowed": [..], "native": N}.

  • egress_ports(ports, in_port, vlan=None) returns the sorted list of ports a flooded frame leaves through. A frame from an access port belongs to that port’s VLAN. A frame from a trunk carries the tag you pass in vlan (untagged frames use the trunk’s native VLAN); if that VLAN is not allowed on the trunk, the frame is dropped (empty list). The frame goes to access ports in the same VLAN and to other trunks that allow the VLAN, never back out of the ingress port.
  • vlan_tag_bits(vlan_id, priority=0) returns the 16 low bits of the tag (priority << 13) | vlan_id and raises ValueError if the VLAN is outside 1 to 4094.
  • usable_vlans() returns 4094.
VLAN-aware flooding and the 802.1Q tag — Python 3
YOUR CODE (edit here, then press Run)
Ctrl/⌘ + Enter
OUTPUT
PCs in VLAN 10 on two different switches cannot reach each other, although both switches have VLAN 10 and the ports are correct. What do you check?

The trunk between the switches: whether it is up, whether VLAN 10 is created on both switches and allowed (not pruned) on the trunk in both directions, and whether the native VLAN matches. Use show interfaces trunk and show vlan brief on both ends. Also check that the trunk is in the forwarding state for VLAN 10 under spanning tree.

Can You Explain It?

Close the lesson and answer these without looking back. The goal isn't the wording — it's whether you've formed a usable mental model.

  • Explain access ports, trunk ports and 802.1Q tagging to a new colleague.
  • Describe how two PCs in different VLANs communicate, and where you would apply security policy.
  • List four common VLAN configuration mistakes and how to detect them.

9. Key takeaways

  • A VLAN is a separate logical broadcast domain, normally one IP subnet each.
  • Access ports carry one VLAN untagged; trunks carry many VLANs tagged with 802.1Q (12-bit ID, 4094 usable).
  • Traffic between VLANs must be routed (router on a stick, layer 3 switch SVIs or a firewall), which is where policy is enforced.
  • Set an unused native VLAN, prune trunks, disable dynamic trunking and park unused ports.
  • Document the VLAN plan and verify with show vlan brief and show interfaces trunk.

🧪 Want to practise this on your own computer? See how to set up your lab →

🤖

Need clarification? Ask GT

Hi, I'm GT — your AI tutor for VLANs and Trunking Fundamentals. What would you like to know?
📝

Knowledge Check

1 of 4

What does a VLAN do?

Report a problem

About: /lessons/vlans-and-trunking-fundamentals