Can't open display on Google Compute Engine

Problem

Unable to launch GUI for an application in Google Compute Engine Linux instance. While doing so, getting the following error:

$ xclock

Error: Can't open display:

Environment

  • Google Compute Engine
  • Linux Guest OS

Solution

X11 forwarding is a mechanism that allows a user to start up remote applications but forward the application display to your local machine.

Follow the below steps to enable X11 forwarding so that the display can be forwarded to the local machine while launching the application GUI being logged in to the remote Google Compute Engine instance.

  1. On the local machine, ensure that you have an X server running. There are a number of X server software programs available like Xmanager, Xquartz, Xming etc. Depending upon the respective company policy, the customer can choose to install any one of those.
  2. Login to remote Google Compute Engine instance and ensure that X11 forwarding is enabled in sshd configuration:

    # cat /etc/ssh/sshd_config  | grep -i X11Forwarding
    X11Forwarding yes
  3. If you see the above parameter as no then edit the file to change it to yes and restart the sshd service (perform the task as root user):

    # systemctl restart sshd
  4. Login with new session from your local machine to the remote Google Compute Engine instance with X11-forwarding flag enabled. 
  5. Users can either log in with GCloud CLI command by passing the -X flag as below:

    $ gcloud compute ssh [INSTANCE_NAME] --zone [ZONE] --ssh-flag="-X"
  6. Users can also choose to log in via some available third-party built-in ssh command (when logging from linux/macOS) or client like putty (when logging in from Windows)

  7. For ssh, simply add -X option for X-Forwarding:

    $ ssh -X -i PATH_TO_PRIVATE_KEY USERNAME@EXTERNAL_IP
  8. For Windows clients like putty, then please refer here for required steps.
  9. While logging into the Google Compute Engine instance with X-Forwarding enabled you may encounter below message, although it would not prevent you from login into the VM, you would still get logged in but without X-forwarding been set up:

    $ gcloud compute ssh ubuntu --zone us-central1-a --ssh-flag="-X"
    
    X11 forwarding request failed on channel 0
    
  10. For more verbose error capturing, use option (-v) to ssh, and you would see error as follows:

    $ gcloud compute ssh ubuntu --zone us-central1-a --ssh-flag="-X -v"
    debug1: Remote: No xauth program; cannot forward with spoofing.
    X11 forwarding request failed on channel 0
  11. This is due to the fact that the xauth program is not installed on a remote Google Compute Engine instance.
    • Note: The xauth command is used to edit and display the authorization information used in connecting to the X server.
  12. Being logged in to the remote Google Compute Engine instance install the xauth package depending upon the OS image type:
    • 8.1 Run the following command as root user (or use sudo) to install xauth under Fedora / RHEL / CentOS / Scientific / Red Hat Enterprise Linux:

      # yum search xauth
      # yum install xorg-x11-xauth
      
    • 8.2 If you are using Debian / Ububtu Linux, enter the following command as a root user (or use sudo):

      # apt-get install xauth
  13. Post installing the xauth package, login again into the remote Google Compute Engine instance with a new session by following Step 5.
  14. Check that the display variable has been set to localhost somewhat as below:

    $ echo $DISPLAY
    localhost:10.0
  15. Test the X11 display forwarding by running xclock command in the remote Google Compute Engine instance, you should see a clock GUI popping up in your local machine.

    $ xclock
  16. Now X11 forwarding is set up and users can launch their required application and the respective GUI would be forwarded to local machine.

Cause

Google Compute Engine Linux images are not available with GUI packages installed and when we boot the instance it boots into a multi-user target which does not have GUI enabled. Thus, in cases where users need to launch an application in the Google Compute Engine Linux instances that requires a GUI, they won't be able to do so unless we forward the remote Google Compute Engine instance display to a local machine.