title: SSH to WSL2 from a remote with UI/X11 Forwarding support
body: <p>I want to ssh to a WSL2 instance using X11 forwarding from another machine using MobaXterm. The WSL2 host machine and remote machine are literally sitting beside each other and connected to the same switch on a private network so security shouldn't be an issue. MobaXterm is the app I'm using for my ssh server and it has a built in X Server. I'm assuming the approach would be similar using other tools.</p>
  <p>It's currently easy to do this from the host machine. I create a WSL Session in MobaXterm and start it. X11 forwarding is configured by default. From there launching UI apps works without any issues or changes to configuration. This automatically sets the DISPLAY variable in the shell to something like 'localhost:10.0' where 10, the display number, will increment for ever session that is opened.</p>
  <p>To ssh from the remote machine I followed the steps outlined by <a href="https://stackoverflow.com/questions/64680740/x11-forwarding-from-wsl2-to-remote-machine">x11 forwarding from WSL2 to remote machine</a>: &quot;THE EASY WAY how to SSH into Bash and WSL2 on Windows 10 from an external machine&quot;
  <a href="https://www.hanselman.com/blog/the-easy-way-how-to-ssh-into-bash-and-wsl2-on-windows-10-from-an-external-machine" rel="nofollow noreferrer">https://www.hanselman.com/blog/the-easy-way-how-to-ssh-into-bash-and-wsl2-on-windows-10-from-an-external-machine</a></p>
  <p>This allows me to ssh from the remote machine. However, the DISPLAY shell variable isn't set and I can't run any UI apps.
  If I'm running a WSL session already on the host as I explained in the first step; I can manually set DISPLAY (ex: export DISPLAY=localhost:10.0) and that will work but not too surprisingly it will open the UI on the host machine rather than the remote machine. I assume I need to somehow create a new display on the remote mobaXterm server and set DISPLAY to use that, but I don't know how to do this. I've opened up the related X11 ports in the firewall. ex: 6000-6020.</p>
  <p>Scott Hanselman also outlines another approach, where he doesn't replace the default ssh shell. But he recommends against this for various reasons. I've had some success setting up my own SSH server directly in WSL linux, but this is also a bit of a pain in part because the WSL IP address is always changing on reboot.</p>
  <p>There are a few related posts already:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/64680740/x11-forwarding-from-wsl2-to-remote-machine">x11 forwarding from WSL2 to remote machine</a> this mentions the recommend Scott Hanselman doesn't work for X11 but doesn't add much detail as to an alternative.</li>
  <li><a href="https://stackoverflow.com/questions/61110603/how-to-set-up-working-x11-forwarding-on-wsl2/61110604">How to set up working X11 forwarding on WSL2</a> this post focuses on getting X11 working on the host machine. But now (around April 2021 release) that is easy to do as described above. I want to get this working from a remote machine.</li>
  </ul>
  <p>Using:</p>
  <ul>
  <li>MobaXterm version: V21.2 Build 4642</li>
  <li>WSL2 version: 5.10.16</li>
  </ul>