25

I'm trying to let WSL use a localhost proxy(actually its Fiddler) on my windows, but I can't see any connection like that, and downloads usually fail, how do I fix that?

I know that I can connect to Linux localhost from the windows one, but how can I reverse this?

WSL is Ubuntu 18.04

1
  • add to your question: What OS. a OS tag.
    – somebadhat
    Mar 24, 2020 at 4:05

7 Answers 7

10

I am not sure if the following will work with applications that are bound to just localhost, but this worked for Fiddler and WSL2:

  1. Set the Fiddler option "Allow remote computers to connect" and restart Fiddler Fiddler options: allow remote computers to connect

  2. Grab your WSL network connection info (on my machine, the adapter is called vEthernet (WSL)) WSL network connection details

  3. Use that address, e.g. 172.26.224.1 where you would normally use localhost curl -I 172.26.224.1 output in WSL2

1
  • 1
    For me to make this working windows firewall settings for port 8888 needs to be adjusted, however I cannot find any quick solution for that so I went into dirty path of disabling whole firewall for WSL vEthernet by PS command Set-NetFirewallProfile -DisabledInterfaceAliases "vEthernet (WSL)". There are multiple issues on WSL github for those problems e.g github.com/microsoft/WSL/issues/4585
    – grexlort
    Jul 5, 2020 at 12:54
17

You should run the following command in a powershell run as administrator:

New-NetFirewallRule -DisplayName "WSL" -Direction Inbound  -InterfaceAlias "vEthernet (WSL)"  -Action Allow

This command comes from this WSL2 ticket. Any firewall rule related to WSL IP address won't be effective because with WSL2 IP changes every time.

Edit 2023: A new network mirror mode and a new firewall management tool have been implemented.

1
  • 1
    I used your rule and export hostip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }') from here and I'm all set, thanks!
    – headsvk
    Mar 26, 2021 at 12:10
8

WSL 2 requires some extra work as compared to WSL 1.

  1. Allow the required port(s) through the firewall.

    1.1 Launch Windows Defender Firewall with Advanced Security

    1.2 On the left pane select Incoming Rules.

    1.3 On the right pane click on New Rule.

    1.4 For the rule type select Port. Next.

    1.5 Select TCP and Specific local ports. Insert the port, a comma-separated list of ports or a port range. Next.

    1.6 Select Allow connection. Next.

    1.7 Check only the Public profile. Next.

    1.8 Enter a name for the rule. e.g. WSL. Done.

  2. Find out the IP Address of WSL from within it:

grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}'

This is the IPv4 Address of the Network Interface vEthernet (WSL).

  1. Test the connection to the Windows host using curl or telnet:

    3.1 curl ip.from.step.2:port

I have used this procedure to connect to an Oracle server and an X server running on Windows.

Update: Since the IP Address of WSL will change every time Windows is restarted, it is convenient to automatically fetch it and update the hosts file.

In WSL:

  1. execute cat /etc/hosts to find out the name of the Windows host.

  2. Add the following code to ~/.profile:

    HOST=<WINDOWS_HOSTNAME>
    IP_HOST=$(grep -m 1 $HOST /etc/hosts)
    OLD_IP=$(echo "$IP_HOST" | awk '{print $1}')
    NEW_IP=$(grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}')
    
    if [ "$NEW_IP" != "$OLD_IP" ]; then
       echo "Updating IP Address-hostname mapping"
       sudo sed -i "s|$IP_HOST|$NEW_IP\t$HOST|" /etc/hosts
       echo "$HOST has the IP Address $NEW_IP"
    fi
    
2
  • 1
    It works, however it need to be done again and again and makes the db public...
    – Kiruahxh
    Jan 26, 2021 at 16:50
  • 1
    @Kiruahxh I just added a script to automatically update the IP-Hostname mapping. In the case of Oracle DB, instead of opening the port in the Firewall you can add a rule to allow connections to C:\oraclexe\app\oracle\product\11.2.0\server\BIN\tnslsnr.exe.
    – Marduk
    Jan 27, 2021 at 8:34
2

For WSL2, the easiest solution, IMHO, is to use the mDNS name that WSL assigns to the Windows host adapter. This is <computer_name>.local, where <computer_name> is your Windows computer name, which is also, helpfully, the default hostname for WSL.

As a result, from Bash:

export WINDOWS_HOST=$(hostname).local
ping $WINDOWS_HOST

For many services, you will need to make sure the Firewall port is also open, since the WSL2 network is separate from the Windows host. This can be done as mentioned in @Kiruahxh's answer.

Also note that this will fail if you have disabled the WSL2 resolver via /etc/wsl.conf.


Side-note 1:

This question seems specific to WSL2, as under WSL1 just using localhost should work.


Side-note 2:

A very recent (at the time of this answer) change to WSL2 is that it will now try to reuse the same IP address after a reboot. This should alleviate many of the concerns mentioned in other answers around having to make certain changes after every reboot. However, to be as deterministic as possible, I still prefer methods which do not rely on the assigned IP address itself, but rather a known name.

1

There is a convenience 'function' that comes with WSL-VPNKit. If you install and import this distro as shown in the README instructions you can then use host.internal to reach the Windows host without knowing its IP address.

After downloading,

wsl --import wsl-vpnkit --version 2 $env:USERPROFILE\wsl-vpnkit wsl-vpnkit.tar.gz

Add to your ~/.bashrc,

wsl.exe -d wsl-vpnkit --cd /app service wsl-vpnkit status >/dev/null || wsl.exe -d wsl-vpnkit --cd /app service wsl-vpnkit start

After reloading the terminal, try hitting host.internal from WSL. For example if the Fiddler proxy port is 8888,

nc -v -w5 -z host.internal 8888
0

See guidance here

https://docs.microsoft.com/en-us/windows/wsl/networking#accessing-windows-networking-apps-from-linux-host-ip

If you want to access a networking app running on Windows (for example an app running on a NodeJS or SQL server) from your Linux distribution (ie Ubuntu), then you need to use the IP address of your host machine. While this is not a common scenario, you can follow these steps to make it work.

Obtain the IP address of your host machine by running this command from your Linux distribution: cat /etc/resolv.conf Copy the IP address following the term: nameserver. Connect to any Windows server using the copied IP address. The picture below shows an example of this by connecting to a Node.js server running in Windows via curl.

You will also need to allow inbound connections to the port on the host.

0

I found this solution to work for me. You have to connect to a different IP address found in the /etc/resolv.conf file. You have to search for the line with nameserver in it and extract the IP address there (it's the 2nd argument). This line will do this and assign it to the environment variable WINDOWS_HOST:

export WINDOWS_HOST=`grep -m 1 nameserver /etc/resolv.conf | awk '{print$2}'`

This can be put in your .bashrc.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .