4

I'm using Windows 10 and Ubuntu on top of WSL. Tools like Eclipse and Code are running as traditional Windows applications. The server(s) that I develop are run on WSL; they launch Tomcat and NodeJS servers. The problem is that Windows does not see the Ubuntu ports and the WSL does not see the Windows services.

I believe WSL starts much as though it were a separate machine, hence the different port. Is it possible though to have it share the host networking? Ideally if I ran netstat in a Window console, I'd see the Tomcat and Node servers started under WSL, and vice versa. If memory serves, this would be possible had I been running Ubuntu as a VM using VirtualBox.

I have a similar question for Docker, but in this case, I'm interested specifically in non-Docker related services.

Followup: Per questions below, I have WSL 2:

wsl -l -v NAME STATE VERSION * Ubuntu-20.04 Running 2 openSUSE-Leap-15-1 Stopped 2 docker-desktop-data Running 2 fedoraremix Stopped 2 docker-desktop Running 2

Woodsman
  • 901
  • 21
  • 61

2 Answers2

7

It sounds like you are running WSL2, since as Woodsman mentioned in the comments, network services running under WSL1 appear to be running on the Windows host network itself.

In WSL2, the instance is running in a Hyper-V VM with a virtual NIC that is NAT'd behind the Windows host. However, the Windows host itself should have direct access to services running in the instance through localhost. WSL appears to do some automatic port-forwarding, but only from the local Windows host to the WSL instance.

In other words, other machines on your local network will not see the WSL network services unless you do some port forwarding (and firewall rules) as per something like this comment in a Github thread on the topic. Again, this should only be necessary if you are trying to access the WSL network apps from elsewhere on your network (your phone, for instance). However, following those steps will also have the side effect of resolving the next issue.

That issue being that localhost auto-forwarding under WSL seems to break down under (at least) several scenarios. If that's the case, and you aren't able to access your node or Tomcat servers via localhost:port, then see this answer for suggestions. It's most likely that a quick wsl --shutdown will resolve (even if you've rebooted). If the problem occurs again, it's likely due to hibernation, or a Windows shutdown/restart with the Fast Startup feature enabled (which also does "partial" hibernation).

You also mention that you aren't able to see Windows network services from WSL, although the examples you give there are VSCode and Eclipse, which are a bit incongruous to me since those are editors rather than network apps/services.

But let's say you were running Maria DB on Windows, for instance, and you wanted to access it from inside WSL2. localhost will not work in that case, since localhost is the virtual NIC of the WSL subsystem. For that, see this answer -- Short summary is to try mDNS (mywindowscomputername.local, replacing the first part with your computer name) or get the Windows's host IP via command. I didn't realize it when I wrote up that answer (I should go back and edit it), but windowscomputername.mshome.net is also reported to work.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
  • Thank you for a thorough well thought out reply. I'm still digesting what you said but do appreciate your efforts. I have WSL 2. Also, VSCode and Eclipse (on the true Windows side) need network connectivity to debug my servers launched via WSL based servers (things started with node, ng serve or mvn spring-boot:run). Both types launch a debugging port for Node and Java. – Woodsman Feb 24 '21 at 00:54
0
  1. Add the following code to ~/.bashrc or ~/.zshrc, and then use winhost to access the windows host ip。
sed -i -e '/winhost/d' /etc/hosts

win_ip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')

win_host="$win_ip winhost"

echo $win_host >> /etc/hosts
  1. install wsl2host service, so that you can use like arch.wsl to connect wsl host.
172.23.85.203 arch.wsl    # managed by wsl2-host
fgd
  • 21
  • 2