VS code remote x11 cant get DISPLAY while connecting to remote server
Asked Answered
P

5

16

I use Windows VS code and this extensions "Remote SSH", "Remote X11", "Remote X11 (SSH)" to connect and to get graphics from remote server.

I run VcXsrv, Pageant. X11 forwarding works if I connect with Pageant directly. It even works in VS code terminal if I set "export DISPLAY=localhost:10.0" in remote server. So all works besides Remote X11, it cant set DISPLAY variable, with this log: Failed to get DISPLAY: Error: Invalid cygwin unix socket path

Puiia answered 27/12, 2020 at 17:47 Comment(1)
From my perspective, I would recommend using MobaXterm for remote GUI.Jory
F
11

I found the solution to this here https://github.com/microsoft/vscode-remote-release/issues/4600. I am summarizing the steps here, since the steps are not very clear there. Before you start make sure you have one of the recent versions of VScode (I am using 1.55.2).

Steps to connect from Windows to Linux:

  1. Install Xming (Should also work with VcXsrv). Start Xming (Default Display:0.0)
  2. Install Remote-SSH. You don't need the other extensions. Nor do you need a running terminal with active x11 connection.
  3. Add a new environment variable to Windows: DISPLAY = localhost:0.0 (Adjust this according to your Xming setting. I prefer the default value).
  4. Connect remotely using VScode. Make sure that the ssh config forwards x11 connection: ForwardAgent yes ForwardX11 yes ForwardX11Trusted yes You can actually check the logs of remote host, if x11 forwarding is successful and port is set. If not set properly, it will complain that the display is not set. An easier check is to see if the DISPLAY variable is automatically set in the Linux server.

Hopefully everything works after these steps.

Freewill answered 29/4, 2021 at 15:3 Comment(8)
This is great on Linux, now I have to figure out how to translate for Windows users.Langevin
I see that this is supposed to be for Windows. More info on 3 would be good. How do you set the display in Windows so that SSH makes use of it?Langevin
agree with BdayLaaland
Here is a tutorial on how to do it: zhuanlan.zhihu.com/p/461378596Laaland
Note: Prerequisite, The server terminal must be able to ping through to the local IP!Coach
Followed your steps, and to piggy back off of Bday's comment, I added a DISPLAY environment variable with the value of localhost:0.0. At this site I was able to Create a User Environment Variable in Windows 10: archive.is/sfhK7#selection-533.3-533.51Crampon
For the ssh config settings, in VSCode open the Remote - SSH settings. In the User section you should see Remote.SSH: Config File. In case you don't know which ssh config file VSCode is using, you can specify the path to an ssh config file in which you added the settings in step 4. Per the link zhuanlan.zhihu.com/p/461378596 shared in the comments, my ssh config being used was at C:\Users[user]\.ssh\config.Aniseikonia
The ssh config settings worked for me without explicitly specifying ForwardAgent yes in my ssh config file. Is this ForwardAgent yes necessary?Aniseikonia
L
9

Reproduced from zhuanlan.zhihu.com see comment above. Basically, all we need to know is that on the server side (mostly are Linux OSs), they use X11 protocol to display GUI.

IMHO, the X11 protocol is consists of two parts, the XServer and the XClient. XServer is the one who actually draws the pixels on display equipment, while the XClient takes charge of processing the logics behind displaying.

To show GUI from remote server, we need to use a tech called X11 Forwarding, which means forwarding the remote XClient's stuff to the XServer that runs on the local machine.

Step 1: VS Code Configuration

Step 1.1: Install VS Code

Step 1.2: Install Remote-SSH plugin

enter image description here

Step 2: Client Machine Configuration (Windows)

Step 2.1: Modify config file

The config file is used by SSH. It locates at C:\Users\[user]\.ssh\config, where [user] should be your username.

To establish a key-based SSH connection, you should specify the path of the key file. What's more, to enable X11 Forwarding, the following two lines should be added to config file:

ForwardX11 yes
ForwardX11Trusted yes

enter image description here example of config file

Step 2.2: Download & install VcXsrv

Download VcXsrv from here: VcXsrv Windows X Server download | SourceForge.net

After the installation, the following software should appear in your Menus. This is exactly the XServer we need.

enter image description here

Step 3: Server Machine Configuration (Ubuntu)

Remember to copy your public key into the authorized_keys on your server machine if you configure a key-based SSH connection.

Step 3.1: Get $DISPLAY

Usually, the $DISPLAY variable is set by default. So, type the following command in your server's terminal, and it will output something in the form of IP:D.S. Remember the D's value, which is 10 here.

> echo $DISPLAY
localhost:10.0

If the output is blank, you should set the $DISPLAY value by yourself.

export DISPLAY=localhost:10.0 

Step 4: Workflow / Pipeline

Step 4.1: Run VcXsrv

Display number should be the value you get from Step 3.1, here I set it as 10. enter image description here

Tick Disable access control

enter image description here

There should be an 'X' icon on your task bar when you run the software successfully.

enter image description here

Step 4.2: Run VS Code & connect to the server

Check the value of $DISPLAY, it should be consistent with your before setting.

Then type the command:

xeyes

If the GUI of two eyes shows up, it means you make it!

enter image description here

Step 4.3: Test your own GUI program (optional)

You can even run your self-written GUI, pretty cool huh?

enter image description here

Laaland answered 15/4, 2022 at 14:48 Comment(1)
I have seriously tried all your steps and they still don't work, can you answer the questions I gave below? unix.stackexchange.com/questions/750145/…Coach
F
5

If this does not work, there is another trick:

  1. Install Xming and start a display with the display number 0 as shown up.

  2. Install Putty

  3. Use Putty to connect to the remote Linux system and make sure the X11 works for that Putty session, remember to check the X11 forwarding in SSH options as shown:

enter image description here

  1. Echo $DISPLAY in putty, and should see the output value such as localhost:15.0

  2. In the VSCode remote ssh, connect to the remote Linux too, if the X11 does work, then set it DISPLAY variable to be the same as putty: setenv DISPLAY localhost:15.0

Things work like a charm

Farber answered 25/6, 2022 at 2:50 Comment(2)
Worked for me! This is a nice work around if you really need it.Aniseikonia
if not using Putty, may try use ssh with argument -Y: ssh -Y user@hostName this will enable X11 forwarding without using Putty. it works for me :-)Farber
D
0

To add on to the answers above, for anyone looking to connect to remote server via VS Code on Windows PC, I realised that we had to set the DISPLAY variable on VS Code.

Please follow this link for Forwarding over SSH related to Step 2, I was finally able to display GUI on my Windows PC.

Small note for newbies like me: make sure to set the same display number on your remote server (e.g. Linux Jetson Nano) and client server (e.g. your Windows laptop) Examples:

  1. In Windows cmd prompt: set DISPLAY=localhost:10.0 (display number = 10)
  2. For setting DISPLAY var in VSCODE (display number = 10)
  “terminal.integrated.env.windows”: {
    “DISPLAY”: “127.0.0.1:10.0”
  }
  1. Launching vcxsrv via Xlaunch, set display number = 10 Xlaunch settings
  2. SSH into remote server:
echo $DISPLAY -> expected: localhost:10.0 (display number = 10)
Dabble answered 16/4, 2024 at 11:5 Comment(0)
C
0

Unfortunately, no one answer here is neither clear nor comprehensive. Some issues may happen with these answers and you will spend a lot of time on fixing them. That's why I've compiled this answer.

1. Prerequisites

Note. You may use apps installation and configuration screenshots from this answer, but with my explanation.

To forward X11 from a Linux server to a Window client you have to:

  1. install a Remote-SSH extension in VSCode on a Windows side.
  2. install X11 server on Windows. Use VcXsrv, for example. You may take it from SourceForge.net(binaries) or from github.com. Yes, when Linux sends X11 traffic to Windows via ssh Linux becomes a client for Windows' X11 server =)
  3. use ssh connection to the Linux server with x11 forwarding. It means:
    • use a -Y key in cli
    • modify your Windows ssh config C:\Users\ [user]\ .ssh\config to forward X11:
      ForwardX11 yes
      ForwardX11Trusted yes
      

2. Environments configuration

A bit theory

When ssh connects from a client (Windows in our case) to a server (Linux) with enabled X11 forwarding it produces the next steps:

  • takes the DISPLAY environment variable's value from the Windows environment;
  • connects to the server;
  • creates a virtual X11 display on the server with a number, assigned according to Linux server possibilities;
  • sets Linux environment variable DISPLAY with the number from the previous step;
  • sets forwarding from the Linux' DISPLAY port number to Windows' DISPLAY port number.

From the theory you may see that it is NOT necessary to use the same port on Windows and Linux sides for X11 redirection as it was mentioned in other answers. Even more, it is more convenient to use different port numbers because it is difficult to catch a port number assigned on the Linux (server) side (it can be random for each session), there is no simple way to propagate this port number from Linux to Windows to use it for ssh. And even if you do it, you will not be able to use it for an already established connection. Furthermore, I'm pretty sure, you will want to start up VcXsrv in Windows during startup. In this case it is out of the question to have same ports in Windows and Linux.

That's why, altogether, that's fine to use different ports in Windows and Linux with the next rule in a head: a port in all configs on each side should be the same. It means:

  • same port, for example 23, for DISPLAY in cli and PowerShell and VcXsrv in Windows. You have to set it manually anyway.
  • same port, for example 13, for DISPLAY in terminal and xauth in Linux. It will be set automatically during ssh connection instantiation.

2.1 How to set up a DISPLAY variable in Windows

  • VSCode only
    “terminal.integrated.env.windows”: {
        “DISPLAY”: “localhost:23.0”
    }
    
    Sometimes localhost may not work, use 127.0.0.1 instead. Buy I've never met that localhost doesn't work.
  • cli (CMD): set DISPLAY=localhost:23.0
  • PowerShell: $env:DISPLAY = "localhost:23.0"
  • Windows overall. It sets a permanent DISPLAY environment variable. In this case you don't need to use previous approaches. I think, it is the best option. So, use setx DISPLAY "localhost:23.0" or assign it as an environment variable via GUI: Win+x -> System -> About -> Advanced system settings (on the right side) -> tab Advanced -> Environment variable (right bottom) -> New... for "User variable for " -> Variable name: DISPLAY, Variable value: localhost:23.0 -> OK -> OK. Close all opened for this action windows.

2.2 How to set up a DISPLAY variable in Linux

You don't need to do it. ssh will do it for you! Even more, if you do it and set a wrong port number you will get an error "Error: Can't open display: localhost:15.0".

3. Troubleshouting

Sometimes, when ssh connects to a Linux server the DISPLAY environment variable is not set. Especially, it can happen when it connects via VSCode in its built-in terminal. You may check it via: echo $DISPLAY in VSCode built-in terminal: empty output (means not set) or expected localhost:14.0, for example.

  • In case of empty output you have to close all your applications and sessions on the Linux server with shooting down VSCode server in Linux. I use killall -9 -u $USER for this purpose. Sometimes, it doesn't help for the first time and you have to clean up VSCode cache. But usually, after several attempts killall and waiting for some time VSCode terminal starts to see a DISPLAY variable and X11 redirection starts to work properly. Remember, if you configured Windows side properly (VcXsrv and the DISPLAY environment variable for ssh connection have the same port number), you don't need to restart anything on Windows side and you don't need to try to set same an X11 port number in Windows as in Linux.
  • In case of NOT empty output, for example localhost:14.0 and with an error "Error: Can't open display: localhost:14.0" you need to check available ssh monitors. Use xauth list.
    1. if there are > 1 records, you may remove the file ~/.Xauthority and reconnect to the server via ssh. The file will be re-created with the only record.

    2. if there is the only record and the number from .../unix:<number> is not equal the number in localhost:<number>.0 from echo $DISPLAY than check what sets the DISPLAY variable in Linux. It can be from

      • VSCode. See Settings -> terminal.integrated.env.linux
      • bash. See ~/.profile, ~/.bashrc, etc.

      Remove any DISPLAY assignment. Kill all apps and sessions in Linux. Connect again. You have to get when there are same numbers in .../unix:<number> from xauth list and localhost:<number>.0 from echo $DISPLAY are set in Linux consistently.

And yes, keep in mind, a VSCode server lives its own live and sometimes works peculiar unpredictable way. That's why it is a good idea to shoot it down fully time to time or when something doesn't work in it.

Colonialism answered 8/7, 2024 at 9:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.