Setup GUI Desktop on Google Cloud Platform (GCP) VM instance

This is a tutorial of how to setup GUI desktop environment on the VM instance of Google cloud platform. You can setup with just a few steps of installation. Follow the instructions here as below.

Environment

  • Ubuntu 16.04
  • Google Cloud Platform

Prerequisites

Create VM instance.

Before the installation, you need to create a new VM instance based on your requirement. See "How to create a GCP VM instance."

Create 5901 port in firewall rules.

Create a port on firewall page for VNC viewer, default port is 5901. To avoid from internet attack, we put constraint on client IP addresses according to CIDR (Classless Inter-Domain Routing). In other words, We only allow some valid hosts that can be used on a specific subnet to connect VNC server.

Let's say 123.123.123.123 as client IP address, where it is a dynamic IP address, but the IP may change from 123.123.1.1 to 123.123.255.255. We don't know what IP address would change to next time. To solve this problem we create a VNC server port (5901) in firewall rules according to CIDR table (click here to see more).

Option 1: VPC Network → Firewall rules

Option 2: Create port via gcloud command with CIDR subnet.
gcloud compute firewall-rules create vncserver --allow tcp:5901 --source-ranges 123.123.0.0/16


Installation

Step 1: Open SSH window of the instance.


Step 2: Create a bash script named install_desktop.sh.

This script is going to upgrade system followed by the installation of desktop dependencies and VNC server.
#!/bin/bash

echo '==== Upgrade system ===='
sudo apt-get update
sudo apt-get upgrade -y

echo '==== Install packages ===='
sudo apt-get install -y gnome-shell
sudo apt-get install -y ubuntu-gnome-desktop
sudo apt-get install -y autocutsel
sudo apt-get install -y tightvncserver
sudo apt-get install -y gnome-core
sudo apt-get install -y gnome-panel
sudo apt-get install -y synaptic

touch ~/.Xresources
autocutsel is a package that allows you to copy and paste between remote server and local computer in vnc viewer.
Run created bash script install_desktop.sh.
bash install_desktop.sh

Step 3: Setup VNC server

To setup VNC server, run:
$ vncserver

You will require a password to access your desktops.

Password: 
Verify:   
Would you like to enter a view-only password (y/n)? n
xauth:  file /home/chunming/.Xauthority does not exist

New 'X' desktop is multitask-gpu-asia:1

Creating default startup script /home/chunming/.vnc/xstartup
Starting applications specified in /home/chunming/.vnc/xstartup
Log file is /home/chunming/.vnc/multitask-gpu-asia:1.log

We can see that the startup script was generated under /home/chunming/.vnc/xstartup

Step 4: Modify VNC configuration

Modify the generated startup script /home/chunming/.vnc/xstartup to the same as below:
#!/bin/sh

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:$PATH

[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
autocutsel -fork
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 &

Step 5: Edit .bashrc file

Add a new command to~/.bashrc. It willl kill VNC session and then start a new one with indicated resolution.
$ vim ~/.bashrc

Add restart_vnc() function
function restart_vnc() {
        vncserver -kill :1
        vncserver -geometry "$1"
}

Activate it.
$ source ~/.bashrc

Step 6: Run VNC server with 1920x1080 resolution and test connection.

$ restart_vnc 1920x1080

Download VNC viewer (Click HERE to download). You can connect to IP address with 5901 port of VM instance.




Step 7: Adjust desktop theme

Install theme packages
sudo apt-get install -y gnome-themes-standard
sudo apt-get install adwaita-icon-theme-full adwaita-icon-theme
gsettings get org.gnome.metacity theme
gsettings set org.gnome.metacity theme 'Adwaita'

Open Terminal after connected to VNC server. Execute following command.
$ sudo gnome-tweak-tool
Then you are allowed to change desktop theme.





Step 8: Keep long running background process (Optional)

Any background processes that were running are likely killed automatically when the user logs out of a session, whether it was a desktop session, a VT session, or when you SSHed into a machine. For example, you can no longer start a screen or tmux session, log out, and expect to come back to it.

This feature by default, and it should rather be disabled by default in Debian, either by compiling systemd with --without-kill-user-processes or by setting KillUserProcesses=no in /etc/systemd/logind.conf.

To enable it, edit system login config.
$ sudo vim /etc/systemd/logind.conf
...
# Uncomment this line
KillUserProcesses=no
...
Then reboot your system.



Troubleshooting

Terminal does not start on Remote desktop

Reason: The environment that gnome-terminal-server is started with does not correctly set the locale to a UTF-8 locale.

Check /etc/default/locale
$ vim /etc/default/locale
# check if locale is correct
LANG=en_US.UTF-8

Configure locales
$ sudo dpkg-reconfigure locales

Select en_US.UTF-8

However, one of my VM instances does not work at all even though I did reconfigure the locales. The problem is resolved by re-installation from step 1 to step 6. Then it works fine without any problem.

Too many authentication failures with VNC

The likely reason why this occurs is due to bots crawling the internet and looking for vulnerable servers to attack. They will try to brute force their way into a server, which is a numbers game, and will only work on a small amount of servers.

Make sure that your instance is not exposed to the internet entirely. Check the Source filter in Firewall rules is not set as 0.0.0.0/0. Limit IP ranges via CIDR rule or your client IP address.

Next, change the VNC password immediately by executing this command.
cd ~/.vnc/passwd
# change password
vncpasswd passwd

→ Read more: Add Swap space to GCP VM Instance


References

Share:

5 則留言: