Can I connect to Google Cloud VM using a GUI?

Solution 1:

You can definitely have a GUI on your VM. It just requires a minimum setup.

The easiest is to use a Linux/Unix (Mac included) pc to access your VM, because then all you need to do is the following:

  1. On the VM, edit the file /etc/ssh/sshd_config, and make sure you have these two lines:

    X11Forwarding yes
    X11DisplayOffset 10
    

    and restart the ssh daemon,

    sudo systemctl force-reload ssh
    

    (On a Mac, that would be:

    sudo launchctl unload /System/Library/LaunchDaemons/ssh.plist
    sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist
    

    while on an old Linux or a Unix that would be:

    sudo /etc/init.d/ssh restart
    

    ).

  2. On your Linux/Unix pc, connect with the command:

    ssh -Y [email protected]
    

    then on the VM make sure the graphical display is correctly selected,

    export DISPLAY=localhost:10.0
    

    and now from within the ssh session you can start graphical applications which will be displayed on your Linux pc monitor. Try, for instance,

    xeyes
    

In line of principle, there is a way to start a whole Desktop Environment (KDE, Gnome, Xfce, Enlightenment, you name it) on the VM, and have it displayed on your Linux pc, but this requires very large bandwidths, so I will not tell you how to do it unless you explicitly ask.

The reason why you were not initially offered this solution is that most people seem to forget that Xorg, the Linux graphical interface, is a server, i.e. it performs the service of displaying on the monitor it is running on the graphical applications which send it data, whether they come from the local or a remote pc. So your Xorg session is perfectly capable of doing this.

If instead you are connecting from a Windows pc, vnc will do that for you, but this requires that you have an Xorg session on the VM, to which vnc connects and replicates it locally, albeit with much worse resolution. If this what you wish to do, you may read here.

EDIT:

a reply to Vidya's comment below. The following assumes you already have a graphical desktop installed on your VM, KDE, Gnome, or what not.

I like this connection to be shown in a window of its own, which I can resize, move, fold, or maximize as need be. So I use Xephyr: on your home computer running any Linux, first you will have to install it,

sudo apt-get install xserver-xephyr

(this command is suitable for Debian and derivatives, if you use a diffent distro you will have to use a suitable package manager), then you start it with

Xephyr -ac -br :1 -resizeable &

(please notice the capital X here, not in the install package), where the important options are :1 (this is what your new display will be called) and resizeable, which allows you to modify the window as per your wishes.

Now you can start your remote session to your server with:

DISPLAY=:1.0 ssh -Y [email protected]

and, once you are inside the virtual machine a command suitable to your display manager, like startkde (if you have KDE), or startx and so on will start your graphical session inside the Xephyr window. If you want, before starting the graphical session, you can just start a simple applications like xeyes which is very fast to load, and should open in the Xephyr window. When you are done, just kill the Xephyr window, that's it.