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.
- 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.
-
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
-
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
- Login with new session from your local machine to the remote Google Compute Engine instance with X11-forwarding flag enabled.
-
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"
-
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)
-
For ssh, simply add -X option for X-Forwarding:
$ ssh -X -i PATH_TO_PRIVATE_KEY USERNAME@EXTERNAL_IP
- For Windows clients like putty, then please refer here for required steps.
-
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
-
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
- 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.
- 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
-
- Post installing the xauth package, login again into the remote Google Compute Engine instance with a new session by following Step 5.
-
Check that the display variable has been set to localhost somewhat as below:
$ echo $DISPLAY localhost:10.0
-
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
- Now X11 forwarding is set up and users can launch their required application and the respective GUI would be forwarded to local machine.