Adjusting the VNC port on an existing KVM guest

By default, all new guests setup with VNC access listen on the local loopback (127.0.0.1) and on a seemingly random port that looks like 590x.

The port is not exactly random, it starts with 0 and counts up for each VNC server it needs in the order that the KVM guest are starting.

This can result in a random order if you have not specified startup orders.

This also can result in some chaotic situations if you have 20 guests all using VNC… how do you figure out which guest is where after a reboot?

I like to set my guests to use a port, forever.  So I can find them at a later date.

First off, have KVM dump the XML configuration for the guest in question:


[root]# virsh dumpxml sampleGuest > /tmp/sampleGuest.xml

 

 

Once you have the XML exported, edit the file with your choice of text editors.  Search for the word VNC and find a like similar to this:


<graphics type='vnc' port='-1' autoport='yes'>
  <listen type='address' address='127.0.0.1'/>
</graphics>

I adjusted mine to have the vnc server listen on any IP Address (0.0.0.0) and on a specifc port (5905).


<graphics type='vnc' port='5905' autoport='no'>
  <listen type='address' address='0.0.0.0'/>
</graphics>

I highly recommend you use a firewall to protect your VNC servers.

You can also set a password to access the server like this:

<graphics type='vnc' port='5905' autoport='no' password='secretpassword' >

Once you are satisfied with the changes, save the file and have virsh import your edited xml file.


virsh define /tmp/sampleGuest.xml

For the VNC change, you may need to reboot the guest.


virsh restart sampleGuest

Once done, you can see your port defined:


netstat -na | grep 5905