How to set up working X11 forwarding on WSL2

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

    The solution from https://github.com/microsoft/WSL/issues/4793#issuecomment-588321333 uses VcXsrv as the X-server, and it is where I'm getting this answer (slightly edited for readability). Note that the original is being updated by its author, so don't forget to re-check.

    To make it work:

    1. On Windows, with the following, change E:\VcXsrv to where your installation is, and save it as xxx.bat in your Windows startup folder, e.g., C:\Users\Me\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup, and you can make it run when boot if you like:
    @ECHO OFF
    
    REM Start WSL once to create WSL network interface
    wsl exit
    
    REM Find IP for WSL network interface
    SET WSL_IF_IP=
    CALL :GetIp "vEthernet (WSL)" WSL_IF_IP
    ECHO WSL_IF_IP=%WSL_IF_IP%
    setx "WSL_IF_IP" "%WSL_IF_IP%"
    setx "WSLENV" "WSL_IF_IP/u"
    
    REM Change E:\VcXsrv to your VcXsrv installation folder
    START /D "E:\VcXsrv" /B vcxsrv.exe -multiwindow -clipboard -nowgl -ac -displayfd 720
    GOTO :EOF
    
    
    
    :GetIp ( aInterface , aIp )
    (
        SETLOCAL EnableExtensions EnableDelayedExpansion
        FOR /f "tokens=3 delims=: " %%i IN ('netsh interface ip show address "%~1" ^| findstr IP') DO (
            SET RET=%%i
        )
    )
    (
        ENDLOCAL
        SET "%~2=%RET%"
        EXIT /B
    )
    
    1. In WSL, edit ~/.bashrc file to add following lines:
    export DISPLAY=$WSL_IF_IP:0
    unset LIBGL_ALWAYS_INDIRECT
    

    That's all to make WSL2 work automatically. The idea is to get the private LAN IP of WSL interface on Windows, and use Environment variable to pass it to WSL. WSL then updates this LAN IP to DISPLAY for X-Server connection.

    The clipboard works well, too, with this setup. I tested this with a WSL2 install of Ubuntu 20.04 LTS.

提交回复
热议问题