I‘m running a KVM virtual machine (FreeBSD 7 and Fedora Linux 13 desktop) under Redhat Enterprise Linux 5.5. However, I’m not getting the remote access via VNC (Virtual Network Computing) to access my Fedora Linux graphical desktop or FreeBSD console. How do I turn on VNC support without reinstalling both operating systems?

You need to attach your vncserver with the guest operating system console. This can be done using any one of the following method:

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:

-vnc :0
-vnc -k en-us
####  Require that password based authentication is used for client connections ####
-vnc,password -k en-us

In the following example start centos1 guest vm using vnc

 /usr/libexec/qemu-kvm -S -M rhel5.4.0 -m 1024 -smp 1 -vnc -k en-us -name centos1 -monitor pty -boot c -drive file=/var/lib/libvirt/images/centos1.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. In this example, edit centos1.xml as follows:
# vi /etc/libvirt/qemu/centos1.xml
Append the following line before final </devices>:

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

Here is my sample config file:

<domain type='kvm'>
    <type arch='x86_64' machine='rhel5.4.0'>hvm</type>
    <boot dev='hd'/>
  <clock offset='utc'/>
    <disk type='file' device='disk'>
      <driver name='qemu' cache='none'/>
      <source file='/emc/vms/images/host302.f02.dc05.corplan.nixcraft.net.in/c/centos1.img'/>
      <target dev='vda' bus='virtio'/>
    <interface type='bridge'>
      <mac address='xx:yy:zz:ee:f4:63'/>
      <source bridge='br0'/>
      <model type='virtio'/>
    <interface type='bridge'>
      <mac address='54:52:xx:yy:zz:ee'/>
      <source bridge='br1'/>
      <model type='virtio'/>
    <serial type='pty'>
      <target port='0'/>
    <console type='pty'>
      <target port='0'/>
    <graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/>

Finally, restart your libvirtd:
# /etc/init.d/libvirtd restart
# virsh shutdown centos1
# virsh start centos1

How Do I Use VNC Client?

Type your VNC (KVM) server ip and port:

Fig.01: Connecting to KVM Guest Using VNC Client

Fig.01: Connecting to KVM Guest Using VNC Client

Once connected you can view your desktop:
Fig.02: RHEL 6 Guest Beta Desktop Over VNC Session Running Under KVM

Fig.02: RHEL 6 Guest Beta Desktop Over VNC Session Running Under KVM

Make sure you browse the desktop using ssh tunnel or over some sort of VPN session.

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

Type the following command:
# virsh vncdisplay domainName
# virsh vncdisplay 3
# virsh vncdisplay centos1

Sample outputs:


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 centos1.xml file as follows:

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


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


  • 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=’′: The listen attribute is an IP address for the server to listen on.

Save and close the file. Restart services as follows:
# /etc/init.d/libvirtd restart
# virsh start centos1

This entry is 14 of 14 in the CentOS / Redhat (RHEL) KVM Virtulization series. Keep reading the rest of the series:
  1. CentOS / Redhat: Install KVM Virtualization Software
  2. CentOS / Redhat: KVM Bridged Network Configuration
  3. KVM virt-manager: Install CentOS As Guest Operating System
  4. KVM virt-install: Install FreeBSD / CentOS As Guest Operating System
  5. KVM: Install CentOS / RHEL Using Kickstart File (Automated Installation)
  6. Troubleshooting KVM Virtualization Problem With Log Files
  7. KVM Virsh: Redirect FreeBSD Console To A Serial Port
  8. KVM: Starting / Stopping Guest Operating Systems With virsh Command
  9. Linux KVM: Disable virbr0 NAT Interface
  10. FreeBSD / OpeBSD Running in KVM Does Not Accept FTP Traffic
  11. KVM: Start a Virtual Machine / Guest At Boot Time
  12. KVM virt-install: Install OpenBSD As Guest Operating System
  13. Linux KVM: OpenBSD Guest Hangs At Starting tty Flags
  14. KVM Virtualization: Start VNC Remote Access For Guest Operating Systems

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 16 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
16 comments… add one
  • Kristian Kirilov Oct 10, 2011 @ 10:02

    Hey, thanks for a great tutorial.

  • Ray Oct 31, 2011 @ 18:14

    Would love to see someone tackle using PCI passthrough with a KVM instance. I’m having a really difficult time getting it to work and would love to know if I’m doing something wrong or trying to get a card that just doesn’t want to work with KVM going.

  • xuhoudao May 18, 2012 @ 8:42

    it’s a great tutorial.
    tks a lot.

  • Dr.Awesome Aug 21, 2013 @ 17:11

    Awesome Stuff! Now I can dive head first into KVM =}

  • Yashpal Aug 29, 2013 @ 17:30

    Thanks…It helped

  • Jeff Pettorino Dec 31, 2014 @ 22:28

    Thanks for the write up. This _almost_ works for me. When I connect, I get the VNC window open, but it immediately closes. The VNC client process has terminated or died or exited cleanly…I don’t know. I don’t get an opportunity to enter the password or anything.
    I am using RealVNC client and connecting via an SSH tunnel (socks 5 proxy through a jump host). I know the SSH config works fine with RealVNC because I can connect to a Mac on the same network.
    Any ideas?

    • Robert Schulte Jan 8, 2015 @ 16:58

      Hello Jeff, i have the exact same problem :-( VNC connects, i can see the window for less than one second and then the connection is closed again. Did you already find a solution? I’ll continue searching and post any possible solutions here.

      • Logan V Feb 12, 2015 @ 0:39

        Same problem here…

        • McPata Mar 1, 2015 @ 13:47

          I use UltraVNC and it works, but no luck with RealVNC. It’s hard cos’ only RealVNC on Mac.

          • McPata Mar 1, 2015 @ 14:07

            Okay, RealVNC….

            Options… > Advanced… > Expert

            – FullColour to True
            – PreferredEncoding to Hextile

            and it works.

            • Heng May 15, 2016 @ 7:50

              Goooood! That slove my problem. Thanks!

  • Robert Schulte Jan 8, 2015 @ 17:36

    Hello Jeff (and all the others who might have the same problem),

    i found a solution that works for me!

    I use tightvnc as client and set the “colourlevel” in expert options to rgb332.

    See this post



  • kizi1 Jan 28, 2015 @ 22:26

    I really like what you guys are usually up too. Such clever work and reporting!

    Keep up the superb works guys I’ve you guys to my blogroll.

  • Kevin Jun 18, 2015 @ 20:59

    How do you select a specific port? I’m novice when it comes to virtualization/ networking so I’m kind of confused as to why it’s port=’-1′

  • Kamen Feb 8, 2017 @ 14:22

    Thanks for the tutorial, it was helpful, though it’s still not working for me.

    I got KVM’s VNC server running alright with both methods above, however my mouse is not synchronized with the virtual machine mouse. It makes it virtually impossible to use it because as soon as my physical cursor leaves the VNC window, my VNC client stops sending its location to the VNC server. Full screen behaves much the same way as well. All VNC clients here seem to have this problem.

    So far I have found no solution for this. Some forums say adding “-usbdevice tablet” to the QEMU command line, however best I can tell this does one thing and one thing only: disable the mouse. Granted my guest OS is not Linux, but I see even you have this problem on your screenshot.

    How do you solve this problem?

  • Mr. X Apr 1, 2017 @ 14:46

    #i ……####like#####u…

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum