14

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

4 Answers 4

6

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
10

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.

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
5

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
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.

New contributor
Dasith Wijes is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.