How to set up working X11 forwarding on WSL2

前端 未结 14 2047
灰色年华
灰色年华 2020-12-12 12:09

When moving from WSL1 to WSL2 many things change; apparently this applies to X11 forwarding as well.
What steps do I need to make in order to use X11 forwarding with WSL

相关标签:
14条回答
  • 2020-12-12 12:38

    I've managed to work with the out-of-the-box VcXsrv firewall configuration (i.e., no need to override/disable any firewall rules) by using the LAN adapter IP of the Windows host. Added the below to my ~/.bash_aliases

    export DISPLAY=$(pwsh.exe -c ipconfig | grep -A 3 lan | grep IPv4 | head -1 | awk '{ print $NF }'):0
    

    where lan is my Connection-specific DNS Suffix (yours may differ, in which case you should replace it in the command line above).

    0 讨论(0)
  • 2020-12-12 12:38

    I would rather set up an ssh server in the guest, install an X11 server like Xming on the host and connect to localhost via putty with X11 forwarding. No fiddling with firewall rules, host IP is not required.

    0 讨论(0)
  • 2020-12-12 12:41

    TL;DR:

    Add the following to your ~/.bashrc:

    export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
    export LIBGL_ALWAYS_INDIRECT=1
    

    Enable Public Access on your X11 server for Windows.*

    Add a separate inbound rule for TCP port 6000 to the windows firewall in order to allow WSL access to the X server, as described by the wsl-windows-toolbar-launcher people.


    As pointed out by WSL_subreddit_mod on reddit and as you can read in Microsoft's documentation on WSL2, the WSL2 architecture uses virtualized network components. This means that WSL2 has a different IP address than the host machine. This explains why the X11 forwarding settings of WSL1 cannot simply be transferred to WSL2.

    On the Ubuntu Wiki page about WSL you can already find a configuration adapted for WSL2 under Running Graphical Applications. A similar configuration is also suggested by the above mentioned Reddit User, who also contributes another part of the solution: Enable Public Access on the X11 server under Windows.

    This means add the following to your ~/.bashrc:

    export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
    export LIBGL_ALWAYS_INDIRECT=1
    

    And Enable Public Access on your X11 server for Windows.*

    The most important part to enable X11 forwarding for WSL2 on Windows 10 is still missing: the Windows firewall blocks connections via the network interface configured for WSL by default.
    A separate inbound rule for TCP port 6000 is required to allow WSL access to the X server. After the rule has been created, as described by the wsl-windows-toolbar-launcher people, the IP address range can be restricted to the WSL subnet in the settings of the newly created rule, under Scope: 172.16.0.0/12.

    *: If you use VcXSrv you can enable public access for your X server by disabling Access Control on the Extra Settings:

    Or by calling vcxsrv.exe directly with the ac flag: vcxsrv.exe -ac as pointed out by ameeno on the github issue.

    0 讨论(0)
  • 2020-12-12 12:41

    Using /etc/resolv.conf nameserver won't work for me since I disabled resolv.conf generation in /etc/wsl.conf (I have a custom resolv.conf).

    Ultimately you want the WSL2 host IP address, which should also be your default route. Here's my ~/.bashrc entry for my Debian WSL2 distro:

    export DISPLAY=$(ip route | awk '/^default/{print $3; exit}'):0
    
    0 讨论(0)
  • 2020-12-12 12:41

    I found there is a official document fro Ubuntu which is comprehensive for your reference. As we know, this tip will work on Debian/WSL2 as well. https://wiki.ubuntu.com/WSL

    Thanks for Kennyhyun and other people's shares. All of them are some how or some way works on my computer to enable X11 server on WSL2 hosted on Windows10. Since the WSL2 is as a VM not longer be the same infrastructure as WSL1 anymore. It did take me some time to go through it.

    0 讨论(0)
  • 2020-12-12 12:42

    The following workaround works for me:

    Set-NetFirewallProfile -Name $(Get-NetConnectionProfile).NetworkCategory -DisabledInterfaceAliases $(Get-NetAdapter | Where-Object Name -like 'WSL').Name

    0 讨论(0)
提交回复
热议问题