WSL: WSL 2 Error: Can't open display: localhost:0.0

Environment

Windows build number 10.0.19041.746
Ubuntu 20.04
Linux version 4.19.128-microsoft-standard (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Tue Jun 23 12:58:10 UTC 2020  

Steps to reproduce

The error can be reproduced by running any X server related command. I have been using xeyes for troubleshooting

xeyes strace output

Expected behavior

A pair of eyes should open outside of the terminal which follows my mouse. (All other X server programs have a similar error)

Actual behavior

Nothing happens and I get the following error in my terminal: Error: Can't open display: localhost:0.0

Additional Details

I have tried running Xming and XLaunch/VcXsrv using the -ac flag. I have also attempted to use the X410 X server with the same result.

VcXsrv is system tray: DESKTOP-LN1HSHQ:0.0 - 0 clients

In /etc/ssh/sshd_config I have set X11Forwarding yes X11UseLocalhost yes

I have tried setting DISPLAY to localhost:0.0 :0.0 127.0.0.1:0.0 $(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0 Which is my address from /etc/resolv.conf

As an additional note, setting DISPLAY=$(cat /etc/re…):0 results in an ~30 second delay before the error occurs. All other DISPLAY settings produce the error instantly.

Disabling Windows Defender Firewall had no effect.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 13
  • Comments: 40

Most upvoted comments

I’m was having the same issue using WSL and Ubuntu 20.04

I found the answer here

https://stackoverflow.com/questions/61860208/wsl-2-run-graphical-linux-desktop-applications-from-windows-10-bash-shell-erro

Your DISPLAY env variable should be set as export DISPLAY=X.X.X.X:0 to use the Windows host’s IP address as WSL2 and the Windows host are not in the same network device, where X.X.X.X is the IP address

and your IP address is listed in resolv.conf against the nameserver ($ cat /etc/resolv.conf)

or simply export DISPLAY=“grep nameserver /etc/resolv.conf | sed 's/nameserver //':0” to load the correct IP address automatically. Additionally, you can add this to .bashrc or .zshrc (If you use Zsh)

I changed my display to this as you suggested:

export DISPLAY="grep nameserver /etc/resolv.conf | sed ‘s/nameserver //’:0"

This didn’t solve this issue… BUT in that link you posted, I looked at some of the solutions people were offering. I decided to check VcXsrv’s firewall permissions and discovered it never even created a profile for itself when I installed it.

I went to Control Panel > System and Security > Windows Defender Firewall > Advanced Settings > Inbound Rules > New Rule… > Program > %ProgramFiles%\VcXsrv\vcxsrv.exe > Allow the connection > checked Domain/Private/Public > Named and Confirmed Rule.

And it worked!!! I’ve been working on this for 3 days, I don’t know how it has taken this long to find the solution, but I am so grateful for your help @moloned!

Editing moloned’s answer to below fixed the issue for me: export DISPLAY="$(grep nameserver /etc/resolv.conf | sed 's/nameserver //'):0"

In case anyone here is unaware (as I was) the above setup is not necessary with WSLg, released (as beta) on 2021-04-27. It is now also available on Windows 10.

The latest WSLg does not need to use the IP of the Windows host, VcXsrv, nor any firewall changes.

$ echo $DISPLAY
:0

From https://docs.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

Prerequisites:

  1. Ensure using Windows 10 Insider build 21362+ [currently only Windows 11]
  2. Recommended: Install driver for vGPU: Intel AMD NVIDIA

For existing WSL systems:

  1. In administrative PowerShell: wsl --update
  2. wsl --shutdown to force a restart of the wsl

Editing moloned’s answer to below fixed the issue for me: export DISPLAY="$(grep nameserver /etc/resolv.conf | sed 's/nameserver //'):0"

I followed @vinpremkumar’s advice but got this:

sudo xeyes
[sudo] password for xzleo:
Authorization required, but no authorization protocol specified
Error: Can't open display: 172.30.0.1:0

I solved this by turning on “Disable access control” setting in XLaunch. And then Problem solved! Thanks so much!

@sorenwacker don’t mess with $DISPLAY environment variable. It is not needed in modern Windows 10 or 11.

image

In case anyone here is unaware (as I was) the above setup is not necessary with WSLg, released (as beta) on 2021-04-27. It is now also available on Windows 10.

The latest WSLg does not need to use the IP of the Windows host, VcXsrv, nor any firewall changes.

$ echo $DISPLAY
:0

From https://docs.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

Prerequisites:

  1. Ensure using Windows 10 Insider build 21362+ [currently only Windows 11]
  2. Recommended: Install driver for vGPU: Intel AMD NVIDIA

For existing WSL systems:

  1. In administrative PowerShell: wsl --update
  2. wsl --shutdown to force a restart of the wsl

Nothing worked except this.

apt install x11-apps
export DISPLAY=:0
xeyes

Just need to set DISPLAY to :0 and export that.

I wonder why the official Microsoft/Ubuntu tutorials do not mention this required step.

I had a similar issue first on Windows 10. Then I realized I must have Windows 11 for GUI to work, but the error was the same. That was because I tried to work around the issue while being on Windows 10 with adding export DISPLAY=$(echo $(grep nameserver /etc/resolv.conf | sed 's/nameserver //'):0) to .bashrc. That set my $DISPLAY variable to <ip-address>:0 while it should have been just :0. After removing the export line from my .bashrc file and restarting WSL the GUI apps started to work 🎉

Assuming VcXsrv is installed and run with display = 0 with ‘Disable access control’ checked, set the display as follows:

1- Get the IP from WSL teminal as

cat /etc/resolv.conf

2- Set the display to that ip

export DISPLAY=_<your ip from above>_:0.0

I changed my display to this as you suggested:

export DISPLAY="grep nameserver /etc/resolv.conf | sed ‘s/nameserver //’:0"

This didn’t solve this issue… BUT in that link you posted, I looked at some of the solutions people were offering. I decided to check VcXsrv’s firewall permissions and discovered it never even created a profile for itself when I installed it.

I went to Control Panel > System and Security > Windows Defender Firewall > Advanced Settings > Inbound Rules > New Rule… > Program > %ProgramFiles%\VcXsrv\vcxsrv.exe > Allow the connection > checked Domain/Private/Public > Named and Confirmed Rule.

And it worked!!! I’ve been working on this for 3 days, I don’t know how it has taken this long to find the solution, but I am so grateful for your help @moloned!

How did it “worked?” I only still get https://dpaste.org/GtSb even after adding VcXsrv to the firewall and restarting my desktop?

export DISPLAY=$(echo $(grep nameserver /etc/resolv.conf | sed ‘s/nameserver //’):0.0)

How will this work when I have to change my resolv.conf file to work with my work’s VPN? I many different IP addresses and none of them seem to work.

  1. I run & 'C:\Program Files\VcXsrv\vcxsrv.exe' -ac via powerhell
  2. I set my top nameserver value to one of the 4 options I have in my resolve.conf
  3. I run export DISPLAY=$(grep -m 1 nameserver /etc/resolv.conf | awk "{print $2}"):$DISPLAY_NUMBER
  4. I call emacs, and I get error: Display nameserver 172.26.64.1:0.0 unavailable, simulating -nw

This error occurs for any ip address I use

For anyone else coming here, it was reinstalling the wsgl nvidia driver that fixed it for me

Has anyone set up protocols or have an idea how to do it this way. I have tried all the above solutions from the comments.

No protocol specified
Error: Can't open display: 172.29.48.1:0

edit Solution: I didn’t have it connected to my local machine, but to some server, I reloaded the windows on localhost, exported the WSL ip and it works. Check if you Disable Access Control in XLaunch on Windows.

and add this to .bashrc export DISPLAY=$(grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}'):0.0

The above instructions are old, as of 2021-04-27.

The latest wsl does not need to use IP of windows host, nor does it need VcXsrv or any firewall changes.

$ echo $DISPLAY
:0

From https://docs.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

For existing WSL systems:

  1. Ensure using Windows 10 Insider build 21362+
  2. Install driver for vGPU: Intel AMD NVIDIA
  3. In administrative Power Shell: wsl --update
  4. wsl --shutdown to force a restart of the wsl

Will this downgrade my AMD driver if I use it? When I first tried this is said it was a downgrade. Do I need this driver if I have a newer version?

I can confirm this worked for me https://stackoverflow.com/a/66398613

I was following this tutorial and just used the alternative command https://www.youtube.com/watch?v=XJ0dI2SYHIE

I wonder why the official Microsoft/Ubuntu tutorials do not mention this required step.

@rwp0 because that’s the default value. Unless people start messing with $DISPLAY, it will have the correct value.

I didn’t mess with DISPLAY in any way. It was so (undefined) from a default installation.

I had a similar issue first on Windows 10. Then I realized I must have Windows 11 for GUI to work, but the error was the same. That was because I tried to work around the issue while being on Windows 10 with adding export DISPLAY=$(echo $(grep nameserver /etc/resolv.conf | sed 's/nameserver //'):0) to .bashrc. That set my $DISPLAY variable to <ip-address>:0 while it should have been just :0. After removing the export line from my .bashrc file and restarting WSL the GUI apps started to work

So easy - just removing $DISPLAY from .bashrc. Needed hours to find this. Thanks a lot

For me the correct IP was the one taken from the Xsrv log: XdmcpRegisterConnection: newAddress

@tolache - that was just waaaay too easy, mate! 😃 Wasted several hours reading answers over on SO and SU, and just removing the broken export made everything work. Thank you so much.

Restart WSL distribution after running this command: export DISPLAY=$(echo $(grep nameserver /etc/resolv.conf | sed 's/nameserver //'):0) restart distribution: wsl --shutdown <distribution>

@Tapuzi This has been solved on Windows 11 with WSLg.

Well, why are you tagging me? this discussion is irrelevent for me. I have a solution. Heared Windows 11 is good by the way, but I don’t tend to upgrade systems hastely.