How to Map KVM/QEMU vnet Interfaces to Their Corresponding Virtual Machines


3 views

When working with KVM/QEMU virtualization using bridged networking, you'll notice the host creates multiple "vnetX" interfaces (like vnet0, vnet1, etc.) that don't have IP addresses assigned. These are tap interfaces connecting VMs to bridges, but their naming convention doesn't directly reveal which VM they belong to.

Here are several effective ways to map vnet interfaces to their respective VMs:


# Method 1: Using virsh dumpxml
for vm in $(virsh list --name); do
    echo "VM: $vm"
    virsh dumpxml $vm | grep -A1 "interface type='network'"
    echo
done

# Method 2: Checking /sys/class/net/ filesystem
for iface in /sys/class/net/vnet*; do
    iface_num=${iface#/sys/class/net/vnet}
    echo "vnet${iface_num} is connected to:"
    cat $iface/ifindex
done

# Method 3: Using tap device information
ip -d link show | grep -A1 vnet

You mentioned MAC addresses don't match - this happens because the MAC shown in the VM configuration is the guest-side MAC, while the host sees a different MAC on the vnet interface. These are actually different endpoints of the virtual network connection.

Here's how you might see the complete mapping in practice:


# List all VMs and their interfaces
virsh list --all
for vm in $(virsh list --name); do
    echo "--- $vm ---"
    virsh domiflist $vm
    echo
done

# Sample output:
# Domain 0 (running)
# Interface  Type       Source     Model       MAC
# --------------------------------------------------
# vnet0      network    default    virtio      52:54:00:12:34:56

For persistent tracking, you can implement libvirt hooks that log vnet interface assignments when VMs start:


#!/bin/bash
# /etc/libvirt/hooks/qemu
if [ "$1" = "$VM_NAME" ] && [ "$2" = "start" ]; then
    echo "$(date) - $1 connected to $(virsh domiflist $1 | grep vnet)" >> /var/log/libvirt_vnet_mapping.log
fi
  • Check /var/log/libvirt/qemu/ for VM-specific logs
  • Use brctl showmacs br0 to see MACs on bridge ports
  • The ip link command shows interface indexes that can be correlated

When working with KVM/QEMU virtual machines using bridged networking, you'll notice mysterious "vnetX" interfaces appearing on your host system. These tap interfaces connect VMs to physical networks but lack obvious identifiers tying them to specific guest instances.

Here are three proven approaches to map vnetX interfaces to their corresponding virtual machines:

Method 1: Using virsh dumpxml

The most accurate method combines virsh with interface inspection:


# List all running VMs
virsh list --all

# Get detailed interface info for a specific VM
virsh dumpxml vm_name | grep -A5 "interface type='bridge'"

Method 2: Checking /sys Filesystem

Linux exposes interface relationships through sysfs:


# Find the MAC address mapping
ls -l /sys/class/net/vnet*/address

# Cross-reference with VM configs
grep -r "mac address" /etc/libvirt/qemu/

Method 3: Using ip Utility

The modern ip command reveals interface relationships:


ip -d link show vnet0
# Look for "link/ether" MAC and compare with VM config

Here's a bash script that automates the mapping process:


#!/bin/bash

for vm in $(virsh list --name); do
    echo "VM: $vm"
    virsh domiflist "$vm" | awk '/bridge/ {print $1,$3,$5}'
    echo "------"
done

If MAC addresses don't match between host and guest:

  • Check for MAC address spoofing in VM configuration
  • Verify the interface is actually connected (check carrier status)
  • Confirm the bridge is properly configured with brctl show

For direct QEMU access without libvirt:


# Connect to QEMU monitor
telnet localhost 4444

# Query network devices
info network