How to Find and Assign VNC Display Ports for KVM Virtual Machines


2 views

To identify which VNC display port (5900 + display number) a KVM guest is using, you can use these methods:


# Method 1: Using virsh
virsh vncdisplay domain_name

# Example output: :2 (means port 5902)

# Method 2: Checking qemu process
ps aux | grep qemu | grep -E "vnc .*:"

# Sample output:
# -vnc 0.0.0.0:2,to=99

# Method 3: Network connections
ss -tulnp | grep 590

For persistent VNC port assignments, modify your domain XML configuration:


<domain type='kvm'>
  ...
  <devices>
    <graphics type='vnc' port='5901' autoport='no' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
  </devices>
</domain>

For environments with multiple VMs, consider this bash script to map all VMs to their VNC ports:


#!/bin/bash

for vm in $(virsh list --name); do
    port=$(virsh vncdisplay "$vm" | cut -d: -f2)
    echo "VM: $vm | VNC Port: 590$port"
done

If you encounter port conflicts or connection problems:

  • Verify the libvirtd service is running: systemctl status libvirtd
  • Check firewall rules for the VNC ports: iptables -L -n | grep 590
  • Ensure the VNC server is properly configured in the guest XML

For better performance than VNC, consider SPICE protocol with fixed ports:


<graphics type='spice' port='6001' autoport='no' listen='0.0.0.0'>
  <listen type='address' address='0.0.0.0'/>
</graphics>

When working with KVM virtualization, each guest's VNC interface typically binds to a port starting from 5900 (display :0) upwards. The actual port number is calculated as 5900 + display number. For example:

:0 → port 5900
:1 → port 5901
:2 → port 5902

To identify which guests are using which VNC ports, use these methods:

Method 1: Using virsh

virsh vncdisplay <domain_name>
# Example output: :1 (meaning port 5901)

Method 2: Checking QEMU Processes

ps aux | grep qemu | grep vnc
# Sample output:
# /usr/bin/qemu-system-x86_64 ... -vnc 0.0.0.0:1 ...

For persistent port assignments, modify your guest's XML configuration:

<domain type='kvm'>
  ...
  <devices>
    <graphics type='vnc' port='5901' autoport='no' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
  </devices>
  ...
</domain>

To apply changes:

virsh define /etc/libvirt/qemu/your_guest.xml
virsh shutdown your_guest
virsh start your_guest

Here's a bash script to list all running KVM guests with their VNC ports:

#!/bin/bash

for domain in $(virsh list --name); do
  port=$(virsh vncdisplay "$domain" | cut -d: -f2)
  echo "Guest: $domain | VNC Port: 590$port"
done

For more modern solutions, consider using SPICE protocol with fixed ports:

<graphics type='spice' port='5901' autoport='no' listen='0.0.0.0'>
  <listen type='address' address='0.0.0.0'/>
</graphics>