🌐 VLANs

Networking 101: split one switch into separate networks, carry them over a trunk, and route between them.

Video: VLANs (Networking video)
Prefer to watch? This short video runs this page's main commands on two real Linux switches, four hosts and an FRR router in ContainerLab, with on-screen captions and a synthetic voiceover (an AI voice trained on the author's own recording). Click to play, or watch it on YouTube.
Networking 101: VLANs

What you will learn:
   why different subnets on one switch still hear each other's broadcasts
   what a VLAN is: one switch split into separate broadcast domains
   access ports (one VLAN, no tags) and trunk ports (many VLANs, tagged)
   the 802.1Q tag, seen with tcpdump on the cable between two switches
   the MAC address table, kept per VLAN
   router-on-a-stick: one router port, one subinterface per VLAN
   two classic mistakes: a VLAN missing on the trunk, and a port in the wrong VLAN

Every command and every line of output on this page was run for real on 2026-11-23. The lab is built with ContainerLab from one file: two switches (Linux bridges on Ubuntu 24.04), four hosts, a router running FRRouting (FRR) 10.7.1 and a server. It continues the address plan from Subnetting (Networking 101). Where Cisco IOS types something differently, the Cisco command is shown next to it (Cisco lines are standard IOS syntax for reference; they were not run here).

The lab: two switches, four hosts, a router

h1 and h2 plug into sw1, h3 and h4 into sw2. A cable joins the two switches, and the router r1 has one port on sw1 and the server h5 behind its second port:

   h1 (VLAN 10)  h2 (VLAN 20)              h3 (VLAN 10)  h4 (VLAN 20)
        |eth1        |eth2                      |eth1        |eth2
     +-----------------------+   trunk    +-----------------------+
     |          sw1          |eth4----eth4|          sw2          |
     +-----------------------+            +-----------------------+
                 |eth3 trunk
            eth1 r1 eth2 --- h5 (server)

   VLAN 10 = 10.10.0.0/26    VLAN 20 = 10.10.0.64/26    server = 10.10.0.128/26

The hosts already have the addresses from the Subnetting lesson: h1 10.10.0.10 and h3 10.10.0.20 in the first /26, h2 10.10.0.70 and h4 10.10.0.80 in the second. The VLAN labels are the plan; at the start the switches are plain bridges with no VLANs. The lab file:
name: net101v
mgmt:                       # own management network (the default 172.20.20.0/24 clashed with another network here)
  network: clab-tc
  ipv4-subnet: 172.31.250.0/24
  ipv6-subnet: 3fff:172:31:250::/64
topology:
  defaults:
    exec:
      - sh -c 'ip route del default || true'     # no way out except what we build
  nodes:
    sw1:
      kind: linux
      image: net101-host:24.04
      exec:
        - ip link add br0 type bridge
        - ip link set br0 up
        - bash -c 'for i in 1 2 3 4; do ip link set eth$i master br0; ip link set eth$i up; done'
    sw2:
      kind: linux
      image: net101-host:24.04
      exec:
        - ip link add br0 type bridge
        - ip link set br0 up
        - bash -c 'for i in 1 2 4; do ip link set eth$i master br0; ip link set eth$i up; done'
    h1:
      kind: linux
      image: net101-host:24.04
      exec: ["ip addr add 10.10.0.10/26 dev eth1", "ip route add default via 10.10.0.1"]
    h2:
      kind: linux
      image: net101-host:24.04
      exec: ["ip addr add 10.10.0.70/26 dev eth1", "ip route add default via 10.10.0.65"]
    h3:
      kind: linux
      image: net101-host:24.04
      exec: ["ip addr add 10.10.0.20/26 dev eth1", "ip route add default via 10.10.0.1"]
    h4:
      kind: linux
      image: net101-host:24.04
      exec: ["ip addr add 10.10.0.80/26 dev eth1", "ip route add default via 10.10.0.65"]
    h5:
      kind: linux
      image: net101-host:24.04
      exec: ["ip addr add 10.10.0.130/26 dev eth1", "ip route add default via 10.10.0.129"]
    r1:
      kind: linux
      image: quay.io/frrouting/frr:10.7.1
      exec:
        - sh -c 'ip route del default || true'
        - touch /etc/frr/vtysh.conf
        - sysctl -w net.ipv4.conf.all.send_redirects=0
  links:
    - endpoints: ["h1:eth1", "sw1:eth1"]
    - endpoints: ["h2:eth1", "sw1:eth2"]
    - endpoints: ["r1:eth1", "sw1:eth3"]
    - endpoints: ["sw1:eth4", "sw2:eth4"]
    - endpoints: ["h3:eth1", "sw2:eth1"]
    - endpoints: ["h4:eth1", "sw2:eth2"]
    - endpoints: ["r1:eth2", "h5:eth1"]

$ containerlab deploy -t vlan.clab.yml

The switches use the same Ubuntu image as the hosts because it has the bridge command (from iproute2); the small Alpine image's busybox ip cannot show VLANs. A new bridge already has VLANs inside: every port is in VLAN 1, untagged:
sw1$ bridge vlan show
port              vlan-id
eth1              1 PVID Egress Untagged
eth2              1 PVID Egress Untagged
eth3              1 PVID Egress Untagged
eth4              1 PVID Egress Untagged
br0               1 PVID Egress Untagged

Step 1: one switch, one broadcast domain

Two subnets are not two networks on the switch. tcpdump on h2 (10.10.0.70, the second /26) while h1 asks for h3:
lab$ ./listen.sh h2 arp -- h1 ping -c1 -W1 10.10.0.20
aa:c1:ab:79:5a:e3 > ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42: Request who-has 10.10.0.20 tell 10.10.0.10, length 28
(h2 heard the packets above, if any)

listen.sh is a small helper in the lab folder: it runs tcpdump -e on one box for 4 seconds while another box runs a command. The ARP request goes to ff:ff:ff:ff:ff:ff, the broadcast MAC, and a switch floods broadcasts out of every port. So h2 hears it, and so does h4 on the other switch. With 40 hosts that is noise; with 400 it is a real load, and every host sees what the others ask for. A VLAN draws a line through the switch: broadcasts stay inside their VLAN.

Step 2: access ports on sw1

Turn on VLAN filtering, then put each host port in its VLAN. An access port belongs to one VLAN and sends frames untagged, so the host never knows:
sw1$ ip link set br0 type bridge vlan_filtering 1
sw1$ bridge vlan add dev eth1 vid 10 pvid untagged; bridge vlan del dev eth1 vid 1
sw1$ bridge vlan add dev eth2 vid 20 pvid untagged; bridge vlan del dev eth2 vid 1
sw1$ bridge vlan show
port              vlan-id
eth1              10 PVID Egress Untagged
eth2              20 PVID Egress Untagged
eth3              1 PVID Egress Untagged
eth4              1 PVID Egress Untagged
br0               1 PVID Egress Untagged

PVID: untagged frames that come in on this port belong to VLAN 10. Egress Untagged: frames for VLAN 10 leave without a tag.
Cisco IOS: interface g0/1, switchport mode access, switchport access vlan 10.

Now h1 cannot reach h3 any more, although they are in the same subnet:
h1$ ping -c2 -W1 10.10.0.20
2 packets transmitted, 0 received, 100% packet loss, time 1025ms
h1 is in VLAN 10, but the cable to sw2 (eth4) still only carries VLAN 1. VLAN 10 ends at sw1.

Step 3: a trunk between the switches

A trunk port carries many VLANs over one cable. Each frame gets a tag that says which VLAN it belongs to. Add VLANs 10 and 20 to eth4 on both switches (without pvid untagged, so they are tagged), and set sw2's access ports:
sw1$ bridge vlan add dev eth4 vid 10; bridge vlan add dev eth4 vid 20
sw2$ ip link set br0 type bridge vlan_filtering 1
sw2$ bridge vlan add dev eth1 vid 10 pvid untagged; bridge vlan del dev eth1 vid 1
sw2$ bridge vlan add dev eth2 vid 20 pvid untagged; bridge vlan del dev eth2 vid 1
sw2$ bridge vlan add dev eth4 vid 10; bridge vlan add dev eth4 vid 20
sw2$ bridge vlan show
port              vlan-id
eth4              1 PVID Egress Untagged
                  10
                  20
eth1              10 PVID Egress Untagged
eth2              20 PVID Egress Untagged
br0               1 PVID Egress Untagged

h1$ ping -c2 -W1 10.10.0.20
64 bytes from 10.10.0.20: icmp_seq=1 ttl=64 time=0.121 ms
64 bytes from 10.10.0.20: icmp_seq=2 ttl=64 time=0.054 ms
2 packets transmitted, 2 received, 0% packet loss, time 1030ms
h2$ ping -c2 -W1 10.10.0.80
64 bytes from 10.10.0.80: icmp_seq=1 ttl=64 time=0.175 ms
64 bytes from 10.10.0.80: icmp_seq=2 ttl=64 time=0.108 ms
2 packets transmitted, 2 received, 0% packet loss, time 1021ms

VLAN 1 stays on the trunk untagged: that is the native VLAN. Cisco uses VLAN 1 as the native VLAN too.
Cisco IOS: interface g0/24, switchport mode trunk, switchport trunk allowed vlan 10,20.

Step 4: the broadcasts are split

Same test as step 1 (h1 first forgets h3's MAC, so it has to ask again):
h1$ ip neigh flush dev eth1
lab$ ./listen.sh h2 arp -- h1 ping -c1 -W1 10.10.0.20
(h2 heard the packets above, if any)
Nothing. h1's ARP request still went to every port, but only the ports in VLAN 10: h3's, across the trunk. h2 is in VLAN 20 and never sees it.

Step 5: the 802.1Q tag on the trunk

Listen on the cable between the switches (sw1's eth4) while h1 pings h3, then while h2 pings h4:
lab$ ./listen.sh sw1:eth4 icmp -- h1 ping -c1 -W1 10.10.0.20
aa:c1:ab:79:5a:e3 > aa:c1:ab:0a:30:d5, ethertype 802.1Q (0x8100), length 102: vlan 10, p 0, ethertype IPv4 (0x0800), 10.10.0.10 > 10.10.0.20: ICMP echo request, id 38004, seq 1, length 64
aa:c1:ab:0a:30:d5 > aa:c1:ab:79:5a:e3, ethertype 802.1Q (0x8100), length 102: vlan 10, p 0, ethertype IPv4 (0x0800), 10.10.0.20 > 10.10.0.10: ICMP echo reply, id 38004, seq 1, length 64
lab$ ./listen.sh sw1:eth4 icmp -- h2 ping -c1 -W1 10.10.0.80
aa:c1:ab:9c:13:37 > aa:c1:ab:a3:d8:2e, ethertype 802.1Q (0x8100), length 102: vlan 20, p 0, ethertype IPv4 (0x0800), 10.10.0.70 > 10.10.0.80: ICMP echo request, id 3328, seq 1, length 64
aa:c1:ab:a3:d8:2e > aa:c1:ab:9c:13:37, ethertype 802.1Q (0x8100), length 102: vlan 20, p 0, ethertype IPv4 (0x0800), 10.10.0.80 > 10.10.0.70: ICMP echo reply, id 3328, seq 1, length 64

ethertype 802.1Q (0x8100) is the tag: 4 extra bytes after the MAC addresses, holding the VLAN number (12 bits, so 1 to 4094) and a priority (p 0). The receiving switch reads the tag, removes it, and sends the frame out of an access port in that VLAN. The hosts never see a tag.

Step 6: the MAC table, per VLAN

A switch learns which MAC address lives behind which port. With VLANs it keeps that table per VLAN:
sw1$ bridge fdb show br br0 | grep 'vlan [0-9]* master' | grep -v ' vlan 1 '
aa:c1:ab:9c:13:37 dev eth2 vlan 20 master br0
aa:c1:ab:21:4e:c6 dev eth2 vlan 20 master br0 permanent
aa:c1:ab:a3:d8:2e dev eth4 vlan 20 master br0
aa:c1:ab:0a:30:d5 dev eth4 vlan 10 master br0
aa:c1:ab:76:8e:57 dev eth4 vlan 20 master br0 permanent
aa:c1:ab:76:8e:57 dev eth4 vlan 10 master br0 permanent
aa:c1:ab:79:5a:e3 dev eth1 vlan 10 master br0
aa:c1:ab:ba:d4:fd dev eth1 vlan 10 master br0 permanent

Read it: h1 (…79:5a:e3) is on eth1 in VLAN 10, h2 (…9c:13:37) on eth2 in VLAN 20, and h3 and h4 (…0a:30:d5, …a3:d8:2e) are both behind eth4, the trunk, each in its own VLAN. The permanent lines are the switch's own port addresses.
Cisco IOS: show mac address-table.

Step 7: route between VLANs (router-on-a-stick)

VLAN 10 and VLAN 20 are now separate networks, so h1 cannot reach h2 at all:
h1$ ping -c2 -W1 10.10.0.70
2 packets transmitted, 0 received, 100% packet loss, time 1041ms

A router joins them. It does not need a port per VLAN: make sw1's eth3 a trunk too, and give r1 one subinterface per VLAN on its single cable. Each subinterface sends and receives frames with its own tag:
sw1$ bridge vlan add dev eth3 vid 10; bridge vlan add dev eth3 vid 20; bridge vlan del dev eth3 vid 1
r1$ ip link add link eth1 name eth1.10 type vlan id 10; ip link add link eth1 name eth1.20 type vlan id 20; ip link set eth1.10 up; ip link set eth1.20 up
r1# configure terminal
r1(config)# interface eth1.10
r1(config-if)# ip address 10.10.0.1/26
r1(config-if)# interface eth1.20
r1(config-if)# ip address 10.10.0.65/26
r1(config-if)# interface eth2
r1(config-if)# ip address 10.10.0.129/26
r1(config-if)# end
r1# show ip route connected
C>* 10.10.0.0/26 is directly connected, eth1.10, weight 1, 00:00:01
C>* 10.10.0.64/26 is directly connected, eth1.20, weight 1, 00:00:01
C>* 10.10.0.128/26 is directly connected, eth2, weight 1, 00:00:01
C>* 172.31.250.0/24 is directly connected, eth0, weight 1, 00:00:30
(the 172.31.250.0/24 line is ContainerLab's management network; ignore it.)

FRR addresses interfaces but does not create them, so the subinterfaces are made with ip link first.
Cisco IOS: interface g0/0.10, encapsulation dot1Q 10, ip address 10.10.0.1 255.255.255.192 (and g0/0.20 the same way).

h1$ ping -c2 -W1 10.10.0.70
64 bytes from 10.10.0.70: icmp_seq=1 ttl=63 time=0.181 ms
64 bytes from 10.10.0.70: icmp_seq=2 ttl=63 time=0.134 ms
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
h1$ traceroute -n -w1 -q1 10.10.0.80
 1  10.10.0.1  1.149 ms
 2  10.10.0.80  1.049 ms
h3$ ping -c2 -W1 10.10.0.130
64 bytes from 10.10.0.130: icmp_seq=1 ttl=63 time=0.161 ms
64 bytes from 10.10.0.130: icmp_seq=2 ttl=63 time=0.143 ms

Watch the router's cable while h1 pings h2. The same packet crosses it twice, first tagged VLAN 10 (in to r1), then VLAN 20 (out again):
lab$ ./listen.sh sw1:eth3 icmp -- h1 ping -c1 -W1 10.10.0.70
aa:c1:ab:79:5a:e3 > aa:c1:ab:e2:cc:e2, ethertype 802.1Q (0x8100), length 102: vlan 10, p 0, ethertype IPv4 (0x0800), 10.10.0.10 > 10.10.0.70: ICMP echo request, id 38007, seq 1, length 64
aa:c1:ab:e2:cc:e2 > aa:c1:ab:9c:13:37, ethertype 802.1Q (0x8100), length 102: vlan 20, p 0, ethertype IPv4 (0x0800), 10.10.0.10 > 10.10.0.70: ICMP echo request, id 38007, seq 1, length 64
aa:c1:ab:9c:13:37 > aa:c1:ab:e2:cc:e2, ethertype 802.1Q (0x8100), length 102: vlan 20, p 0, ethertype IPv4 (0x0800), 10.10.0.70 > 10.10.0.10: ICMP echo reply, id 38007, seq 1, length 64
aa:c1:ab:e2:cc:e2 > aa:c1:ab:79:5a:e3, ethertype 802.1Q (0x8100), length 102: vlan 10, p 0, ethertype IPv4 (0x0800), 10.10.0.70 > 10.10.0.10: ICMP echo reply, id 38007, seq 1, length 64
That is why it is called router-on-a-stick, and also its limit: all traffic between VLANs shares one cable. Bigger networks use a layer 3 switch that routes inside the switch.

Mistake 1: a VLAN missing on the trunk

Someone removes VLAN 20 from the trunk on sw2 (on Cisco, a switchport trunk allowed vlan 10 that should have been "add 10"):
sw2$ bridge vlan del dev eth4 vid 20
h1$ ping -c2 -W1 10.10.0.20
2 packets transmitted, 2 received, 0% packet loss, time 1035ms
h2$ ping -c2 -W1 10.10.0.80
2 packets transmitted, 0 received, 100% packet loss, time 1012ms
h4$ ping -c2 -W1 10.10.0.65
2 packets transmitted, 0 received, 100% packet loss, time 1039ms

VLAN 10 works, VLAN 20 is dead on sw2, gateway included. The cable is fine, the link is up: only the VLAN list tells you.
sw2$ bridge vlan show dev eth4
port              vlan-id
eth4              1 PVID Egress Untagged
                  10
sw2$ bridge vlan add dev eth4 vid 20
h2$ ping -c2 -W1 10.10.0.80
2 packets transmitted, 2 received, 0% packet loss, time 1036ms
Cisco IOS: show interfaces trunk lists the allowed and active VLANs on each trunk. Use switchport trunk allowed vlan add 20 to add one; without add, the list is replaced.

Mistake 2: a port in the wrong VLAN

h3's port on sw2 ends up in VLAN 20. h3 still has its VLAN 10 address:
sw2$ bridge vlan add dev eth1 vid 20 pvid untagged; bridge vlan del dev eth1 vid 10
h1$ ip neigh flush dev eth1
h1$ ping -c3 10.10.0.20
From 10.10.0.10 icmp_seq=1 Destination Host Unreachable
From 10.10.0.10 icmp_seq=2 Destination Host Unreachable
From 10.10.0.10 icmp_seq=3 Destination Host Unreachable
3 packets transmitted, 0 received, +3 errors, 100% packet loss, time 2049ms
h1$ ip neigh show 10.10.0.20
10.10.0.20 dev eth1 FAILED

Same subnet, same mask, cable plugged in, and still no answer: h1's ARP request goes out in VLAN 10 and h3 is now in VLAN 20. The VLAN and the subnet must match. The port tells you:
sw2$ bridge vlan show dev eth1
port              vlan-id
eth1              20 PVID Egress Untagged
sw2$ bridge vlan add dev eth1 vid 10 pvid untagged; bridge vlan del dev eth1 vid 20
h1$ ping -c2 -W1 10.10.0.20
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
Cisco IOS: show vlan brief lists every VLAN and its access ports.

Save it

The router keeps its addresses with write memory:
r1# write memory
Note: this version of vtysh never writes vtysh.conf
Building Configuration...
Configuration saved to /etc/frr//mgmtd.conf
Configuration saved to /etc/frr/zebra.conf
Configuration saved to /etc/frr/staticd.conf
r1$ grep -v ^! /etc/frr/mgmtd.conf
frr version 10.7.1_git
frr defaults traditional
hostname r1
interface eth1.10
 ip address 10.10.0.1/26
exit
interface eth1.20
 ip address 10.10.0.65/26
exit
interface eth2
 ip address 10.10.0.129/26
exit

The saved file has the addresses, not the subinterfaces themselves: eth1.10 and eth1.20 are Linux interfaces made with ip link, and they must exist again after a reboot (on a real Ubuntu router, in netplan under vlans:). We deleted eth1.10 and created it again: FRR put the address back by itself as soon as the interface came up.
The switch settings typed with bridge are lost on a restart too; in this lab the file rebuilds everything. Cisco IOS: copy running-config startup-config (the VLANs themselves are kept in vlan.dat on many Catalyst switches).
A lab warning: do not restart FRR inside the container (/usr/lib/frr/frrinit.sh restart). The FRR daemons are the container's main process, so Docker restarts the whole container in a fresh network namespace, and ContainerLab's cables to it are gone. Redeploy the lab instead.

Build the same lab yourself

You need Docker and ContainerLab. Build the image (the same one as in the Subnetting lesson; it has bridge, tcpdump and ping):
FROM ubuntu:24.04
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends iproute2 iputils-ping ipcalc tcpdump traceroute python3-minimal && rm -rf /var/lib/apt/lists/*
CMD ["sleep","infinity"]
$ docker build -t net101-host:24.04 .
$ containerlab deploy -t vlan.clab.yml
$ docker exec -it clab-net101v-sw1 bash        (a shell on the switch)
$ docker exec -it clab-net101v-r1 vtysh         (the router's Cisco-like shell)
$ containerlab destroy -t vlan.clab.yml         (remove everything)
The listen helper used above:
#!/bin/bash
# listen.sh BOX FILTER -- BOX2 CMD...   capture on BOX's eth1 (or IFACE via BOX:IFACE) for 4 s while BOX2 runs CMD
b=${1%%:*}; i=${1#*:}; [ "$i" = "$1" ] && i=eth1; f=$2; shift 3
docker exec clab-net101v-$b timeout 4 tcpdump -lenni $i -c 4 $f 2>/dev/null | sed -E 's/^[0-9:.]+ //' &
sleep 1; t=$1; shift; docker exec clab-net101v-$t "$@" >/dev/null 2>&1; wait
echo "($b heard the packets above, if any)"
(Save it as listen.sh next to vlan.clab.yml and make it executable with chmod +x listen.sh.)

Cheat sheet: Linux bridge and Cisco IOS

Task          Linux bridge (this lab)                         Cisco IOS
VLANs on      ip link set br0 type bridge vlan_filtering 1    (always on)
Access port   bridge vlan add dev eth1 vid 10 pvid untagged   switchport mode access
              bridge vlan del dev eth1 vid 1                  switchport access vlan 10
Trunk         bridge vlan add dev eth4 vid 10                 switchport mode trunk
              bridge vlan add dev eth4 vid 20                 switchport trunk allowed vlan 10,20
Show VLANs    bridge vlan show                                show vlan brief
                                                              show interfaces trunk
MAC table     bridge fdb show br br0                          show mac address-table
Subinterface  ip link add link eth1 name eth1.10 \            interface g0/0.10
                type vlan id 10                                encapsulation dot1Q 10
(Cisco column: standard IOS syntax, for reference; not run in this lab.)

Common problems

Same subnet, no ping, ARP FAILED: the two hosts are in different VLANs. Check the access port with bridge vlan show (Cisco: show vlan brief).
One VLAN works across two switches, another does not: the VLAN is missing on the trunk. bridge vlan show dev eth4 (Cisco: show interfaces trunk).
Nothing works right after vlan_filtering 1: every port is still only in VLAN 1. Add the VLANs to the access and trunk ports.
Hosts in a VLAN reach each other but not other VLANs: no router (or no subinterface for that VLAN), or a wrong default gateway.
The router's subinterface is missing after a reboot: FRR saves the address but does not create the interface. Define the VLAN interface in the OS (netplan vlans:).
Hosts see each other's broadcasts: the ports are still in the same VLAN (often all in VLAN 1).

What's next

Next in Networking 101: ARP and the MAC table up close, on the way to a CCNA study series.
Missed the start? Networking 101: Subnetting builds the address plan used here. The Routing series moves packets between networks: Routing 1: Static Routes, Routing 2: OSPF, Routing 3: BGP.