A while back a project needed a remote desktop, and to keep costs down I installed a desktop environment directly on Ubuntu Server. Recording the steps here. Ubuntu 18.04 is already EOL. For a new environment, use 22.04 or 24.04; the package names are basically the same.

The environment is Ubuntu 18.04 on Google Cloud Platform.

Install Desktop and Browser

To save memory, I’m using XFCE for the desktop:

sudo apt-get update
sudo apt-get install xfce4 xfce4-terminal firefox -y

Install and Configure VNC Server

Install VNC

sudo apt-get install tigervnc-standalone-server tigervnc-common -y

Initialize the VNC server

vncserver

Set the vncserver password when prompted.

Kill the current vncserver

pkill Xtigervnc

Create and edit the file

vi $HOME/.vnc/xstartup

with the following content:

#!/bin/bash
exec startxfce4

Make it executable, then restart VNC Server:

chmod +x $HOME/.vnc/xstartup
vncserver -localhost no

VNC itself isn’t encrypted, and -localhost no listens directly on the public internet. Pick one of the two access methods below.

Option 1: Restrict Source IPs with a Firewall

Allow only your own IP:

gcloud compute firewall-rules create vncserver \
  --allow tcp:5901 \
  --source-ranges <your-ip>/32

Then connect to <instance-ip>:5901 with a VNC client.

Option 2: SSH Tunnel (More Secure)

Have VNC listen only on the loopback interface. Stop the previous session first:

vncserver -kill :1
vncserver -localhost yes

Create the tunnel from your local machine:

ssh -L 5901:localhost:5901 <user>@<instance-ip>

Connect the VNC client to localhost:5901. Traffic goes over SSH, so 5901 doesn’t need to be exposed to the public internet.

Either way, don’t use --source-ranges 0.0.0.0/0 to open VNC to the whole internet.

References