Several Desktops On A Single VM In The Cloud

Earlier this year, I discovered how to run a graphical desktop in a virtual machine in the cloud. I promptly put this to good use and ran a couple of online hands-on workshops on various topics in recent months. In combination with Guacamole, participants don’t even need to install a remote desktop viewer, but can use the remote desktop right in the web browser. My latest improvement: Run several desktops on a single Virtual Machine.

The idea came to me after playing with an HDMI dummy dongle that simulates a screen for my workstation. On a VM, there’s no need for an HDMI dummy dongle, the virtual GUI uses a virtual screen buffer that doesn’t even require a graphics card. When I arrived at this point, I was wondering why I couldn’t just start several virtual screen buffers and run an individual gnome session on each? And indeed, this works like a charm out of the box.

In case you are interested on how to set this up, have a look again at my original article on the topic. There, you will find the following script to create a virtual display buffer with “Xvfb”, run a gnome session on it and finally run x11vnc server to enable access to it remotely with a VNC client viewer via tcp port 29000:

export DISPLAY=:0 
Xvfb $DISPLAY -screen 0 2048x1200x16 & 
sudo -u cloud gnome-session & 
x11vnc -usepw -forever -repeat -shared -rfbport 29000 -display :0

Getting a second desktop is as easy as doing this again with a different display number, a different user and a different TCP port number for VNC:

export DISPLAY=:1
Xvfb $DISPLAY -screen 0 2048x1200x16 & 
sudo -u cloud2 gnome-session & 
x11vnc -usepw -forever -repeat -shared -rfbport 29001 -display :1

Note that the screen number given to Xvfb does NOT change! Also note that a different user should be used for the second GUI, so starting the same program on each GUI does not lead to strange side effects due to the use of the same configuration files. As the users ‘cloud’ and ‘cloud2’ should be identical for running online hands-on workshops, ‘cloud2’ can be cloned from ‘cloud’ while the GUI is NOT running as follows:

adduser --disabled-password --gecos "" cloud2
sudo rm -rf /home/cloud2
cp -r /home/cloud /home/cloud2
sudo chown -R cloud2:cloud2 /home/cloud2

It is of course possible to run more than two desktops at a time on a single virtual machine. The number is only limited by the amount of memory and CPU capacity. On virtual machine with 2 vCPUs and 4 GB of RAM, I could easily spawn 3 desktops with Firefox + Wireshark running on each. 4 desktops were possible as well but that was definitely pushing the memory limit.

If such a multi-desktop approach is usable for online hands-on workshops depends on the topic. For Docker or Kubernetes introduction workshops, the setup is not ideal, as this type of software runs system wide and hence, users would get in the way of each other. However, for hands-on workshops which only require ‘higher layer’ software like a web browser or tools such as Wireshark with pre-recorded trace files, this setup works great. Depending on the number of participants, it reduces VM rental cost and setup effort, or allows to increase the number of people that can participate in a workshop when the maximum number of VMs that one can spawn at any one time is limited.