How to set up working X11 forwarding on WSL2

前端 未结 14 2229
灰色年华
灰色年华 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:29

    Copied my answer from this github issue.

    The idea is to use the ability to communicate over stdio.

    Prerequisite

    • Just so we can use socat in Windows host, you need a distribution running WSL1. I am sure you can do this in powershell but I didn't have time to research this. Maybe someone can write a stdio->tcp redirector in powershell, then we wouldn't need to have 2 WSL distros.

    How to forward X-server connection

    1. Have your favorite X server running on Windows. By default they would listen to port 6000.
    2. In the WSL2 distro, run the following command in the background (ubuntu is the name of the WSL1 distro with socat installed):
    mkdir -p /tmp/.X11-unix/
    socat UNIX-LISTEN:/tmp/.X11-unix/X0,fork EXEC:"/mnt/c/Windows/System32/wsl.exe -d Ubuntu socat - TCP\:localhost\:6000"
    

    Basically this sets up a tunnel from the normal X unix domain socket into the host's port 6000.

    How to forward any TCP connection back to host

    Let's assume there is a tcp service running at port 5555 on Windows. In the WSL2 distro, run the following command in the background (ubuntu is the name of the WSL1 distro with socat installed):

    socat TCP-LISTEN:5555,fork EXEC:"/mnt/c/Windows/System32/wsl.exe -d ubuntu socat - TCP\:localhost\:5555"
    

    How to forward any TCP connection from host into WSL2

    This is simply doing the same thing, but in the opposite direction. You can run the following in your WSL1 distro:

    socat TCP-LISTEN:5555,fork EXEC:"/mnt/c/Windows/System32/wsl.exe -d ubuntuwsl2 socat - TCP\:localhost\:5555"
    

    Performance

    On my PC, it can handle up to 150MB/s of data so it's not the fastest but fast enough for most applications.

提交回复
热议问题