How to Access Linux Virtual Machines Remotely Over VNC

Virtual machines (VM) are great. They allow you to get more done across multiple different platforms. They also provide a sandbox for you to test out new things. When you need a graphical desktop too, most situations require you to be tied to the hypervisor machine.

That said, you can share your VM’s desktop over your network with VNC. That will allow you to access your VMs from anywhere on your local network. VNC is a desktop sharing protocol that allows a Linux system to output its graphical desktop over a network (much like the Remote Desktop Connections in Windows). This way you can open up the desktop on another computer and interact with it directly like you would if you were sitting in front of it.

There are a bunch of ways to handle this. This guide will focus on Ubuntu and will use TigerVNC. If you have other preferences, you can definitely adjust accordingly.

Before you get started, you’re going to need to install the required packages. Most virtualization is done through the kernel itself, so there isn’t much else that you need.

On the Server

The server, in this case, refers to the computer that is running the virtual machine.

sudo apt install qemu-kvm libvirt-bin bridge-utils

On the Client

The client, in this case, refers to the computer that is remotely accessing the computer with the VM.

sudo apt install tigervnc

Network bridging isn’t strictly necessary for virtual machines, but it is very useful, and it makes them behave like physical machines on your network.

This part isn’t too difficult, but you probably shouldn’t do it remotely over SSH since you will need to restart after the process.

On the server machine, start by looking up the name of your network interface. Run ip a to see which network interface your computer is using. Once you have it, use your favorite text editor with sudo to open “/etc/network/interfaces.” Make it look similar to this:

auto lo
iface lo inet loopback
 
auto br0
iface br0 inet dhcp
bridge_ports eth0

Replace “eth0” with the name of your interface. If you have multiple ones in use, you can list them separated by a single space. When your file is ready, save and exit. It’s best to restart your computer after this to apply the change.

There is a direct way to create your virtual machines from the command line. Of course, if you really don’t want to, you can use a tool like virt-manager to make the VMs, but you’ll need to modify them later.

If you’re planning to go the command line route, it’s not that hard. There are a lot of options, but you certainly don’t need all of them.

The command for creating your virtual machine is fairly complex. There are a lot of options, and that amount can increase with the complexity of the machine.

Start with naming the machine.

sudo virt-install --name yourVM

Set the amount of memory in megabytes and the number of CPU cores.

--memory 2048 --vcpus 2

Next, tell it how much hard drive space to allocate in gigabytes and where the “.iso” file is that it’ll be installing from.

--disk size=20 --cdrom /home/user/Downloads/ubuntu-17.10.1-desktop-amd64.iso

You’ll also need to tell it to make graphics available over VNC. Use the defaults and configure it like this:

--graphics vnc,listen=0.0.0.0 --noautoconsole

Finally, tell it to use your network bridge.

--bridge br0

Create a virtual machine from the command line

Put it all together, and you’ll get a command something like this one:

sudo virt-install --name ubuntuArtful --memory 4096 --vcpus 6 --disk size=20 --cdrom /home/user/Downloads/ubuntu-17.10.1-desktop-amd64.iso --graphics vnc,listen=0.0.0.0 --noautoconsole --bridge br0

Connect To Your VM

On the client machine open up TigerVNC. It will probably be called “vncviewer” on your system. The window is very simple. Enter in the IP address of your server and connect.

Install Ubuntu over VNC

A second window will open up to your VM. Use the controls on the side to resize your VM window to your liking. Then, finish the installation of your VM.

When your install is done, you can use your VM like you would any other (or the native desktop). After you shut it down, you can restart it via virsh and connect again over VNC like you did here.

sudo virsh start ubuntuArtful

That’s it! You can now create and run virtual machines with graphical desktops remotely over your network.

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.