I recently had to set up a VM on Google Cloud, but have a GUI access to it. It took me longer than it should have to set it up between reading multiple articles online, so here’s how I ended up succeeding.
- Create a VM with a display device. We’ll start by creating a VM. We’ll go to Compute Engine and click on Create Instance. Most of the defaults here are okay, except that we select “Enable display device”. We’ll change the boot disk to use Ubuntu 22.04 LTS, with a size of 30 GB. Click on Create to create and boot the VM.
- Enable access to VNC ports. Before we ssh in to the VM, we’ll set up firewall rules. Select the instance and click on Edit. Scroll down to Network tags, and add a tag called “vnc-server”. We’ll define the rules for this next. Click Save. In the Google Cloud website, in the sidebar, navigate to VPC network –> Firewall. Click on Create Firewall Rule at the top (not Policy), and give it the name
vnc-server. Make sure the direction of traffic is ingress, Action on match is Allow, and set Targets to All instances in the network. Set Source IPv4 ranges to 0.0.0.0/0. Under Protocols and ports, check TCP, and enter the port
- Install a desktop environment and VNC server. Let’s ssh into the machine:
gcloud compute ssh instance-1 --zone us-central1-a
Now, we’ll install the things we need.
sudo apt update sudo apt install -y tightvncserver tasksel sudo tasksel install gnome-desktop --new-install sudo apt install gnome-panel vncserver
That last command will prompt you to enter a password, so create one. It cannot be empty. You can test that VNC server is running by typing
nc localhost 5901
which should give you
You can use Ctrl + C to exit the command once it prints this. Now, we’ll need to configure the VNC server, so we’ll first kill it and edit the config file.
vncserver -kill :1 vim ~/.vnc/xstartup
Change it to:
#!/bin/sh # Fix to make GNOME work export XKL_XMODMAP_DISABLE=1 [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources xsetroot -solid grey vncconfig -iconic & x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" & x-window-manager & gnome-panel & gnome-settings-daemon & metacity & nautilus &
Save, and then run
- Install a VNC viewer locally. On your local machine, you’ll need a VNC viewer. I use RealVNC.
- Connect to your VM using VNC. Once you have RealVNC installed, you can finally connect to your machine! On the Compute Engine page where your instances are listed, get the External IP address of your VM. In the VNC Viewer application, in the address bar, paste the URL, followed by
:5901. You’re specifying the VNC port 5901 here.
That’s it! You should now have a working VNC connection to your VM that lets you use a GUI to operate it.