VIRSH:KVM – Запуск удаленного доступа VNC для гостевых операционных систем

Method # 1: Command Line Option

Normally, QEMU (/usr/libexec/qemu-kvm) uses SDL to display the VGA output. With the -vnc option option, you can have QEMU listen on VNC display display and redirect the VGA display over the VNC session. When using the VNC display, you must use the -k parameter to set the keyboard layout if you are not using en-us. Valid syntax for the display is as follows:

1
2
3
4
5
6
-vnc :0
-vnc 192.168.1.5:0
-vnc 0.0.0.0:5
-vnc 0.0.0.0:1 -k en-us
#### Require that password based authentication is used for client connections ####
-vnc 0.0.0.0:1,password -k en-us

In the following example start vm0 guest vm using vnc

1
/usr/libexec/qemu-kvm -S -M rhel5.4.0 -m 1024 -smp 1 -vnc 0.0.0.0:1 -k en-us -name vm0 -monitor pty -boot c -drive file=/var/lib/libvirt/images/vm0.img

Method # 2: qemu-kvm VM Config File (Recommended)

You need to edit your VM config file which is in XML format. The config file is located at /etc/libvirt/qemu directory.Append the following line before final </devices>:

1
<graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/>

Here is my sample config file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<domain type='kvm'>
  <name>vm0</name>
  <uuid>88d067cf-e5f7-7229-f35f-472a9c884864</uuid>
  <memory>1048576</memory>
  <currentMemory>1048576</currentMemory>
  <vcpu>1</vcpu>
  <os>
    <type arch='x86_64' machine='rhel5.4.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' cache='none'/>
      <source file='/images/vm0.img'/>
      <target dev='vda' bus='virtio'/>
    </disk>
    <interface type='bridge'>
      <mac address='xx:yy:zz:ee:f4:63'/>
      <source bridge='br0'/>
      <model type='virtio'/>
    </interface>
    <interface type='bridge'>
      <mac address='54:52:xx:yy:zz:ee'/>
      <source bridge='br1'/>
      <model type='virtio'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target port='0'/>
    </console>
    <graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/>
  </devices>
</domain>

Finally, restart your libvirtd:

1
2
3
# systemctl restart libvirtd
# virsh shutdown vm0
# virsh start vm0

How Do I Find Out Current VNC Setting For Any Given Domain / VM?

Type the following command:

1
2
3
# virsh vncdisplay domainName
# virsh vncdisplay 3
# virsh vncdisplay vm0

How Do I Password Protect My VNC Session?

The passwd attribute provides a VNC password in clear text (so make sure your xml config file is only readable by root user). Edit vm0.xml file as follows:

1
<graphics type='vnc' port='-1' autoport='yes' passwd='YOUR-PASSWORD-HERE' keymap='en-us'/>

OR

1
<graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1' passwd='YOUR-PASSWORD-HERE' keymap='en-us'/>
1
2
3
<graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1' passwd='YOUR-PASSWORD-HERE' keymap='en-us' sharePolicy='force-shared'>
  <listen type='address' address='127.0.0.1'/>
</graphics>

Where,

  • type=’vnc’: The graphics element has a mandatory type attribute which takes the value “sdl”, “vnc”, “rdp” or “desktop”. In this case it is set to VNC for remote access.
  • autoport=’yes’: The autoport attribute is the new preferred syntax for indicating autoallocation of the TCP port to use.
  • passwd=’YOUR-PASSWORD-HERE’: The passwd attribute provides a VNC password in clear text.
  • keymap=en-us;: The keymap attribute specifies the keymap to use.
  • listen=127.0.0.1: The listen attribute is an IP address for the server to listen on.

Save and close the file. Restart services as follows:

1
2
# systemctl restart libvirtd
# virsh start vm0
Scroll to top