Skip to content
Visual explainer 3h to learn 4 steps

VLANs & trunking

Segment a switch into VLANs and carry them across trunks with 802.1Q tags.

By Invicly Tech, , in Computer Networking. About 10 minutes to read.

Topic 7 of 51

In short

VLANs split one physical switch into several logical networks, each its own broadcast domain. Access ports belong to a single VLAN, while trunk ports carry many VLANs between switches by adding an 802.1Q tag to each frame. Traffic between VLANs must go through a router or layer 3 switch.

Explained simply

A VLAN (virtual LAN) lets you split one physical switch into several separate networks. Ports in VLAN 10 can only talk to other ports in VLAN 10, as if they were on their own switch. Staff, guests and phones can share the same hardware but stay apart.

The main reason is broadcasts and security. Every broadcast reaches every device in its VLAN, so smaller VLANs mean less noise. And a guest laptop in VLAN 20 cannot directly reach the finance server in VLAN 10, which limits what an attacker or a virus can touch.

An access port belongs to one VLAN and connects one end device, like a PC or a printer. The device does not know VLANs exist. A trunk port connects switches (or a switch and a router) and carries many VLANs over one cable. To keep them apart, each frame on a trunk gets a small tag with its VLAN number.

Devices in different VLANs need a router to talk, exactly like devices on different networks. The router can be a separate box connected with one trunk cable (router-on-a-stick) or a layer 3 switch that routes between VLANs internally.

Press Play on a diagram to walk through it one step at a time.

MapOne switch, three VLANs
Access switch
  • VLAN 10 Staff
    • Ports 1-12
    • 10.0.10.0/24
  • VLAN 20 Guests
    • Ports 13-20
    • 10.0.20.0/24
  • VLAN 30 Voice
    • Ports 21-24
    • 10.0.30.0/24
  • Trunk port 24
    • Tagged 10, 20, 30
    • To router or core
CompareTwo ways to route between VLANs
Router-on-a-stick
  • One trunk to a router
  • Subinterfaces: g0/0.10, g0/0.20
  • Cheap, small sites
  • All inter-VLAN traffic shares one link
Layer 3 switch (SVIs)
  • interface vlan 10 with an IP
  • Needs ip routing enabled
  • Routes in hardware at port speed
  • Standard in office cores

In depth

The 802.1Q tag, bit by bit

On a trunk, the switch inserts a 4-byte tag into each frame right after the source MAC address. The first 2 bytes are the TPID, always 0x8100, which tells the receiver a tag follows. The next 2 bytes, the TCI, hold three fields: a 3-bit PCP (priority, 0 to 7, used for quality of service such as voice), a 1-bit DEI (drop eligible) and the 12-bit VLAN ID. Because the frame changed, the FCS is recalculated.

Twelve bits give 4,096 values, but 0 and 4095 are reserved, so usable VLANs are 1 to 4094. VLAN 1 is the default on every port. On Cisco, VLANs 1002 to 1005 are reserved for old Token Ring and FDDI, and 1006 to 4094 is called the extended range. The tag makes a full frame 1522 bytes, which every trunk device must accept.

FlowA tagged frame on a trunk
  1. 1
    Dst and src MAC

    6 + 6 bytes, unchanged

  2. 2
    TPID 0x8100

    2 bytes: a VLAN tag follows

  3. 3
    PCP, DEI, VID

    3 + 1 + 12 bits, e.g. VLAN 20

  4. 4
    EtherType

    The original type, e.g. 0x0800

  5. 5
    Payload and new FCS

    FCS recalculated over the tagged frame

Access, trunk and the native VLAN

An access port sends and receives untagged frames, and the switch puts everything from it into one VLAN. Many switches also allow one extra voice VLAN on an access port, so an IP phone can tag its own voice traffic while the PC behind it stays untagged. A trunk port carries tagged frames for its allowed VLANs.

One VLAN per trunk, the native VLAN (VLAN 1 by default), is sent untagged. If the two ends of a trunk disagree on the native VLAN, untagged frames from VLAN 1 on one side land in, say, VLAN 99 on the other, silently joining two networks. Cisco logs this as a native VLAN mismatch via CDP. A common hardening step is to set the native VLAN to an unused ID on both ends, or to tag it as well.

  • Access: untagged, one data VLAN (plus optional voice VLAN)
  • Trunk: tagged, many VLANs
  • Native VLAN: untagged on a trunk, must match both ends
  • show interfaces trunk lists mode, native and allowed VLANs

Routing between VLANs

With router-on-a-stick, one router port connects to a switch trunk. The router has one subinterface per VLAN, each with encapsulation dot1Q and an IP address that hosts use as their gateway. A frame from VLAN 10 arrives tagged 10, is routed, and leaves tagged 20 on the same cable. It is cheap, but all inter-VLAN traffic shares one link in both directions.

A layer 3 switch does the same job internally with switched virtual interfaces (SVIs): interface vlan 10 with an IP address, plus ip routing turned on. Routing happens in hardware at full port speed, which is why most offices route between VLANs on the core switch and only send internet traffic to a router or firewall.

SequenceRouter-on-a-stick: PC in VLAN 10 to server in VLAN 20
PC 10.0.10.5
Switch
Router g0/0.10 and .20
Server 10.0.20.8
  1. 1Untagged frame to gateway MAC
    PC 10.0.10.5 to Switch: Untagged frame to gateway MAC
  2. 2Trunk: tagged VLAN 10
    Switch to Router g0/0.10 and .20: Trunk: tagged VLAN 10
  3. 3Routed, sent back tagged VLAN 20
    Router g0/0.10 and .20 to Switch: Routed, sent back tagged VLAN 20
  4. 4Tag removed, out access port
    Switch to Server 10.0.20.8: Tag removed, out access port
  5. 5Reply, enters VLAN 20
    Server 10.0.20.8 to Switch: Reply, enters VLAN 20
  6. 6Tagged 20, routed back to 10
    Switch to Router g0/0.10 and .20: Tagged 20, routed back to 10

VLAN security and common mistakes

Two VLAN hopping attacks are worth knowing. In switch spoofing, an attacker's PC pretends to be a switch and uses Cisco's DTP to negotiate a trunk, gaining every VLAN. Stop it by setting user ports to switchport mode access and switchport nonegotiate. In double tagging, an attacker in the native VLAN sends a frame with two tags; the first switch strips the outer one and the next switch forwards it into the inner VLAN. Stop it by using an unused native VLAN that no host sits in.

The most common real mistake is simpler. switchport trunk allowed vlan 40 replaces the whole allowed list with only VLAN 40, cutting off every other VLAN on that trunk. To add one, use switchport trunk allowed vlan add 40.

  • User ports: switchport mode access
  • Native VLAN: unused ID, e.g. 999, on both ends
  • Use allowed vlan add, never a bare allowed vlan, to add one
  • Shut unused ports and put them in a parking VLAN

Learn it in 4 short steps

About 45m each. Track them with a timer in your plan.

  1. 1
    Explain broadcast domains and VLANs~45m
  2. 2
    Configure access ports~45m
  3. 3
    Configure 802.1Q trunk ports~45m
  4. 4
    Route between VLANs with router-on-a-stick~45m

Key points

  • One VLAN usually maps to one IP subnet.
  • 802.1Q inserts a 4-byte tag holding a 12-bit VLAN ID (1 to 4094).
  • The native VLAN is sent untagged on a trunk; keep it consistent on both ends.
  • Router-on-a-stick routes between VLANs using subinterfaces on one trunk.

Key terms

Tap a card to flip it. 0/5 known

Examples

text
vlan 10
 name STAFF
interface fa0/1
 switchport mode access
 switchport access vlan 10
interface gi0/1
 switchport mode trunk
 switchport trunk allowed vlan 10,20,30

Cisco IOS switch configuration.

Router-on-a-stick for two VLANs

The switch has PCs in VLAN 10 and VLAN 20 and a trunk on Gi0/1 to the router. Step 1: on the router, enable the physical port with no IP. Step 2: create a subinterface per VLAN with encapsulation dot1Q and the gateway address. Step 3: on the switch, make Gi0/1 a trunk that allows 10 and 20. Step 4: set each PC's gateway to its subinterface address (10.0.10.1 or 10.0.20.1). Step 5: ping across; show ip route on the router lists both networks as connected.

text
R1(config)# interface g0/0
R1(config-if)# no shutdown
R1(config)# interface g0/0.10
R1(config-subif)# encapsulation dot1Q 10
R1(config-subif)# ip address 10.0.10.1 255.255.255.0
R1(config)# interface g0/0.20
R1(config-subif)# encapsulation dot1Q 20
R1(config-subif)# ip address 10.0.20.1 255.255.255.0
SW1(config)# interface g0/1
SW1(config-if)# switchport mode trunk
SW1(config-if)# switchport trunk allowed vlan 10,20

A VLAN interface on a Linux server

A server must join VLAN 30 over a trunked switch port. Step 1: create a VLAN subinterface on top of eth0 with ID 30. Step 2: give it an address in the VLAN 30 subnet and bring it up. Step 3: the kernel now tags outgoing frames with VLAN 30 and accepts tagged frames for it. Step 4: confirm with ip -d link, which shows vlan protocol 802.1Q id 30. For a permanent setup, put the same settings in netplan or NetworkManager.

bash
sudo ip link add link eth0 name eth0.30 type vlan id 30
sudo ip addr add 10.0.30.20/24 dev eth0.30
sudo ip link set eth0.30 up
ip -d link show eth0.30 | grep vlan
sudo tcpdump -i eth0 -e -nn vlan 30 -c 3

Quick reference

  • 802.1Q tag: 4 bytes, TPID 0x8100, 3-bit PCP, 1-bit DEI, 12-bit VLAN ID
  • Usable VLAN IDs: 1 to 4094; VLAN 1 is the default
  • show vlan brief, show interfaces trunk, show interfaces g0/1 switchport
  • switchport trunk allowed vlan add 40 adds; without add it replaces the list
  • Native VLAN must match on both trunk ends; use an unused ID
  • L3 switch: ip routing + interface vlan 10 + ip address = inter-VLAN routing
  • Linux: ip link add link eth0 name eth0.30 type vlan id 30

Common mistakes

  • Native VLAN mismatch between trunk ends, leaking traffic between VLANs.
  • Forgetting to allow a new VLAN on the trunk.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1How many bits hold the VLAN ID in an 802.1Q tag?

Question 2

2An admin types switchport trunk allowed vlan 40 on a trunk that carried 10, 20 and 30. What happens?

Question 3

3What is sent untagged on an 802.1Q trunk?

Question 4

4A PC in VLAN 10 cannot reach a PC in VLAN 20 on the same switch. What is missing?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Create VLANs 10 (STAFF) and 20 (GUEST) on a switch and put Fa0/1-12 in VLAN 10 and Fa0/13-20 in VLAN 20.

  2. Exercise 2medium

    Configure a trunk between two switches on Gi0/1 that carries VLANs 10, 20 and 30, uses native VLAN 999, and does not negotiate with DTP.

  3. Exercise 3hard

    Replace a router-on-a-stick with inter-VLAN routing on a layer 3 switch for VLANs 10 and 20, with a default route to a firewall at 10.0.0.2 on a routed port.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

How many VLANs should a small office have?
Usually a handful: staff, guests, voice, printers or IoT, and management. Each one maps to its own IP subnet and gets firewall rules between them.
Is a VLAN a security boundary?
It is a useful one when the switches are configured properly, because hosts cannot reach other VLANs without a router. Real security comes from the firewall rules or ACLs on that router.
Why does my old switch need switchport trunk encapsulation dot1q first?
Some older Cisco switches also supported Cisco's own ISL trunking, so you must choose 802.1Q before setting mode trunk. Newer switches only support 802.1Q and do not have the command.
Does the PC know which VLAN it is in?
Not on an access port: it sends normal untagged frames and the switch assigns the VLAN. Only devices on trunks, like servers, hypervisors and phones, deal with tags themselves.
ShareXLinkedInWhatsApp