10

Is there a straightforward way to find the VNC screen (i.e. port number minus 5900) onto which a KVM guest is bound?

My guests are all configured to run with VNC enabled, but the order in which they occupy the ports is random.

Alternately, is there a way to assign them in the configuration (of guest or host), so that each respective guest will occupy a predefined port?!

0xC0000022L
  • 1,456
  • 2
  • 20
  • 41

3 Answers3

18

Since you're using libvirt, you can just ask it!

root@onhost1:~# virsh list
 Id Name                 State
----------------------------------
  1 one-34               running
  2 one-36               running
  3 one-38               running

root@onhost1:~# virsh vncdisplay one-34
:34

root@onhost1:~# virsh vncdisplay 1
:34

(my particular correlation of name to VNC display port is due to the use of Open Nebula)

EDIT: Nowadays, you can use domdisplay to get the URI of the display whether it's VNC or Spice:

○ → virsh domdisplay win-michael
vnc://127.0.0.1:0

○ → for dom in $(virsh list --name); do echo -n "$dom: "; virsh domdisplay $dom; done
win-michael-m: vnc://127.0.0.1:0


Here's a one-liner to execute this for all running guests at once:
for i in $(virsh -q list|awk '{print $2}'|sort); do
  echo -e "\033[01;31m$i\033[00m -> $(virsh vncdisplay $i)"
done

Also made it into a function that sorts output by port number:

function vnc-list
{
  for i in $(virsh -q list|awk '{print $2}'|sort); do
    PORTNUM=$(virsh vncdisplay $i|cut -f 2 -d ':')
    printf "% 2d: \033[01;32m%.20s\033[00m\n" "$PORTNUM" "$i";
  done | sort -n
}
MikeyB
  • 38,725
  • 10
  • 102
  • 186
  • Wow, thanks a bunch for that. It's spot-on what I was looking for. I'm going to edit a Bash one-liner into your answer for completeness. – 0xC0000022L Feb 09 '12 at 14:44
  • Hmm, well the on-liner I edited in apparently got edited into a three-liner. Makes me look stupid as if I didn't know what the difference between 1 and 3 is, but I promise that the version I had was cramming this onto one single line ;) ... don't have edit-rights yet, so I'm at the mercy of others with this. – 0xC0000022L Feb 09 '12 at 16:36
  • I edited it into a three-liner for readability and so it doesn't require scrolling to view. – MikeyB Feb 09 '12 at 16:41
  • Due to some reason in my install virsh vncdisplay does not show up the VNC port. "#virsh vncdisplay vm01 returns nothing". I have tigetvnc installed. And running Centos 6 – chandank Jan 15 '13 at 17:22
  • 1
    @chandank you probably have a spice display - use the `domdisplay` command to get the more general URI – MikeyB Aug 25 '15 at 13:22
3

I would run :

ps aux | grep "VM name/config"

Note the process ID and then

netstat -apn | grep "process ID"

This should show you are port open by that process.

Andrey
  • 548
  • 2
  • 8
2

To set the display, simply explicitly provide the -vnc <ip:display> option to qemu-kvm. See the man page to qemu-kvm, especially the -vnc parameter section for details.

the-wabbit
  • 40,319
  • 13
  • 105
  • 169