It´s awesome when you have your notebook with speed and memory enough to do your development experiments but when you do not have, it sucks! Use a remote machine hosted in the cloud is a good option but access it only by the terminal can not be enough if you want to install an IDE or even use the browser. In this tutorial I will show how you can use a remote VM Instance on GCP in the same way you use your local machine. You will be able to have a full desktop user interface and use any application that needs the UI.
As a pre-requirement for this tutorial you will need:
- A VM instance with Ubuntu 18.04-LTS image installed and running in your GCP.
- Google Cloud SDK installed in your local machine.
Check my previous tutorial where I show how to create a VM Instance with Ubuntu on GCP. To allow your local machine to connect safely to the VM Instance you will also need the Google Cloud SDK. It will be used to create a tunnel between your machine and the VM Instance. This installation is different according to your operating system. If you are using windows check my tutorial where I show how to install it, otherwise go to the Google Cloud Quickstarts documentation and follow the steps according to your operating system.
Installing the virtual desktop components
Start your VM Instance and connect to it through the SSH browser.
Once you are connected, update the Linux source list and install all the necessary components for our virtual desktop. Type the following commands one by one in the terminal.
sudo apt-get -y update sudo apt-get -y upgrade sudo apt-get -y install gnome-shell sudo apt-get -y install ubuntu-gnome-desktop sudo apt-get -y install autocutsel sudo apt-get -y install gnome-core sudo apt-get -y install gnome-panel sudo apt-get -y install gnome-themes-standard
Now install and configure the
tightvncserver. It will allow connections in your remote desktop environment. Type the following command:
sudo apt-get -y install tightvncserver touch ~/.Xresources
tightvncserver with the command below. It will request you to create a password. Define a password of 8 characters i.e
becloud. Type the command:
The output will be something like this bellow. When asked to a view-only password, type
n for not now.
educostadev@becloudnativedesktop:~$ tightvncserver You will need a password to access your desktops. Password: Verify: Would you like to enter a view-only password (y/n)? n xauth: file /home/educostadev/.Xauthority does not exist New 'X' desktop is becloudnativedesktop:1 Creating default startup script /home/educostadev/.vnc/xstartup Starting applications specified in /home/educostadev/.vnc/xstartup Log file is /home/educostadev/.vnc/becloudnativedesktop:1.log
The command above generated the configuration files. Kill the session we created. Type the command:
vncserver -kill :1
Edit the file
~/.vnc/xstartup that was before created. In this example, I´m using the
vimeditor but you can use any other. Follow the steps bellow:
- Type on console:
- Once the editor is opened enter in the Insert Mode, type:
- Delete all the content. Press the key:
Copy and paste the content bellow inside the editor:
#!/bin/sh autocutsel -fork xrdb $HOME/.Xresources xsetroot -solid grey export XKL_XMODMAP_DISABLE=1 export XDG_CURRENT_DESKTOP="GNOME-Flashback:Unity" export XDG_MENU_PREFIX="gnome-flashback-" unset DBUS_SESSION_BUS_ADDRESS gnome-session --session=gnome-flashback-metacity --disable-acceleration-check --debug &
Now it´s time to exit the insert mode. Save and close the file:
Escto exit the insert mode. And type
:wqto write and quit the file.
Start the vnc server to accept remote connections. Type the command below and replace the screen resolution to a resolution that fits well in your monitor. i.e 1024×768 , 1280×1024, 1440×900
vncserver -geometry 1440x900
Configuring the Google SDK tunnel
Once you have the Google SDK Installed, open the terminal in your local machine and type the command below. The command will open a connection with your remote machine and map the port 5901. Check the documentation for a detailed description of each parameter.
Type in your local terminal the command bellow replacing the values of the parameters with the information of your GCP account and remote machine.
gcloud compute ssh educostadev@becloudnativedesktop --project modern-optics-252617 --zone us-west1-b --ssh-flag "-L 5901:localhost:5901"
The meaning of the main parameters are:
- compute ssh <USER>@<REMOTEHOSTNAME> : The user name and host name of the remote machine.
- –project <PROJECT ID> . Your project ID. You can found it in your GCP Dashboard
- –zone <ZONE>. The machine´s zone. You can found it in the description of your VM Instance
The output in the terminal will be something like this:
λ gcloud compute ssh becloudnativedesktop --project modern-optics-252617 --zone us-west1-b --ssh-flag "-L 5901:localhost:5901" WARNING: The PuTTY PPK SSH key file for gcloud does not exist. WARNING: The public SSH key file for gcloud does not exist. WARNING: The private SSH key file for gcloud does not exist. WARNING: You do not have an SSH key for gcloud. WARNING: SSH keygen will be executed to generate a key. Updating project ssh metadata.../Updated [https://www.googleapis.com/compute/v1/projects/modern-optics-252617]. Updating project ssh metadata...done. Waiting for SSH key to propagate. The server's host key is not cached in the registry. You have no guarantee that the server is the computer you think it is. The server's ssh-ed25519 key fingerprint is: ssh-ed25519 255 b9:78:65:e0:b0:a0:58:65:1d:2d:4a:f4:2d:c5:46:7e If you trust this host, enter "y" to add the key to PuTTY's cache and carry on connecting. If you want to carry on connecting just once, without adding the key to the cache, enter "n". If you do not trust this host, press Return to abandon the connection.
And a connection between your local machine and the remote host will be made. The output in the terminal will be something like this.
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-1044-gcp x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Sun Nov 3 01:42:23 UTC 2019 System load: 0.0 Processes: 163 Usage of /: 9.0% of 48.29GB Users logged in: 1 Memory usage: 23% IP address for ens4: 10.138.0.4 Swap usage: 0% * Kata Containers are now fully integrated in Charmed Kubernetes 1.16! Yes, charms take the Krazy out of K8s Kata Kluster Konstruction. https://ubuntu.com/kubernetes/docs/release-notes * Canonical Livepatch is available for installation. - Reduce system reboots and improve kernel security. Activate at: https://ubuntu.com/livepatch 10 packages can be updated. 0 updates are security updates. *** System restart required *** Last login: Sun Nov 3 01:39:03 2019 from 18.104.22.168 eduardo@becloudnativedesktop:~$
Keep this new terminal open.
Download and install the VNC Viewer. Open the VNC Viewer and in the program bar type
localhost:5901. Confirm all options and when requested type the password defined before.
You finally went into the desktop of your Ubuntu virtual machine on Google Cloud Compute.