Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access host ip and port? #1032

Closed
ghost opened this issue Sep 1, 2016 · 28 comments
Closed

How to access host ip and port? #1032

ghost opened this issue Sep 1, 2016 · 28 comments

Comments

@ghost
Copy link

ghost commented Sep 1, 2016

I am using bash on windows 10. i want to access host ip:port

there be service listen on 0.0.0.0:80 of host windows 10.
how do i access to host windows 10 port?

@sunilmut
Copy link
Member

sunilmut commented Sep 1, 2016

@netroby - I am not sure I fully understand your problem. Currently, Windows and WSL have the same IP address. Can you use the explicit host IP address to access your Windows host?

@rodrymbo
Copy link

rodrymbo commented Sep 1, 2016

WSL (Bash.exe) shares the IP address (and port availability) with Windows 10. From Windows CMD, you can issue netstat -an to see what ports are open, and (with a bit of luck) which have something listening. (So for example you can have one Windows process listening on port 80 as Skype sometimes does by default, or one WSL process listening on port 80, but not both. If Skype starts listening on port 80, it has won, and nothing else on either Windows or WSL will be able to, for example.)

The other issue is that the Windows firewall probably blocks connections to listening services it doesn't recognize. If you are setting something up on WSL to listen on port 80, you might want to use Windows Firewall to open that port. Windows Firewall doesn't appear to have an easy way to open up to WSL processes specifically. If you are setting up something on Windows, you can use Firewall to open up that application. Windows Firewall might allow connections to localhost by default, but you never know.

@aseering
Copy link
Contributor

aseering commented Sep 1, 2016

Hi @netroby -- in WSL, actually there is no such thing as the "host" in the sense that you're referring to; it's all just Windows. Even your Linux environment is actually all running directly in Windows; it's just running with a wrapper library that makes it work.

So, for example, if you have a service running on 127.0.0.1 on Windows, Linux programs should also connect to it at 127.0.0.1, on the same port.

0.0.0.0 is a special IP; it means "any IP address". So Linux processes can pick any IP address that's valid on the Windows host.

@ghost
Copy link
Author

ghost commented Sep 2, 2016

Thanks.

@ghost ghost closed this as completed Sep 2, 2016
@cloudnine
Copy link

Possible to specify a port to zone alarm to let WSL through? WSL uses any port it can, or a specific port to run say apt or apt-get?

Running tasklist, then netstat -abn when bash wsl is running sudo apt update with zone-alarm disable doesn't reveal the port apt uses to connect to repo.

@bruno-brant
Copy link

Does this still holds true for WSL 2?

I have an application running on port 8080 "on Windows" (I know it's all Windows...), and I'm cURLing it on WSL without success.

@craigloewen-msft
Copy link
Member

@bruno-brant At the time of writing this comment (Insiders Preview Build 18990 is the latest build on the fast ring), you'll need to access your Windows networking applications from Linux using the IP address of your Windows host in WSL2. You can find more details on how to do that here on our documentation.

@baumstern
Copy link

baumstern commented Sep 27, 2019

@bruno-brant At the time of writing this comment (Insiders Preview Build 18990 is the latest build on the fast ring), you'll need to access your Windows networking applications from Linux using the IP address of your Windows host in WSL2. You can find more details on how to do that here on our documentation.

For impatients,
Running below command on WSL2 and get your host IP from nameserver field.

cat /etc/resolv.conf

@rickywu
Copy link

rickywu commented Oct 10, 2019

@gurrpi But this ip changed everytime startup

@baumstern
Copy link

baumstern commented Oct 11, 2019

@gurrpi But this ip changed everytime startup

@rickywu I'm suffering from the same problem as you. It seems to be a matter to be discussed on a separate issue.

@lpedrosa
Copy link

As @gurrpi mentioned, you can always check the windows 10 host IP in a wsl2 distro by checking the nameserver line on the /etc/resolv.conf.

A quick cheeky helper, that you might want to add to your powershell profile, in case you occasionally use you wsl distro's curl to test things listening on localhost

function Get-WSL2WindowsHost {
  return wsl cat /etc/resolv.conf `| grep nameserver `| cut -d ' ' -f 2
}

You can then do:

# imagine you have something listening on port 3000
wsl curl http://$(Get-WSL2WindowsHost):3000

Hope this helps!

@bakyeono
Copy link

You may add this in your .profile, .bash_profile, .bashrc, .zshrc, or .zprofile if you want to get the IP address of Windows host IP address from your Linux distro.

export WSL_WINDOWS_HOST=`cat /etc/resolv.conf | grep nameserver | cut -d ' ' -f 2`

@dilushan
Copy link

@bruno-brant At the time of writing this comment (Insiders Preview Build 18990 is the latest build on the fast ring), you'll need to access your Windows networking applications from Linux using the IP address of your Windows host in WSL2. You can find more details on how to do that here on our documentation.

For impatients,
Running below command on WSL2 and get your host IP from nameserver field.

cat /etc/resolv.conf

I had to disable firewall too :-|

@bruno-brant
Copy link

You may add this in your .profile, .bash_profile, .bashrc, .zshrc, or .zprofile if you want to get the IP address of Windows host IP address from your Linux distro.

export WSL_WINDOWS_HOST=`cat /etc/resolv.conf | grep nameserver | cut -d ' ' -f 2`

Excellent tip!

@KoltesDigital
Copy link

I had to disable firewall too :-|

Found somewhere else, you could keep your firewall on and run that in a PowerShell admin console:

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

@maple3142
Copy link

Another simple way: route -n | grep UG | head -n1 | awk '{print $2}' Source

If there is no route command, you need install net-tools.

@andreykaipov
Copy link
Member

No need for route:

ip route show default | awk '{print $3}'

We could also ask Powershell, but this is slower:

powershell.exe -Command 'Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias "vEthernet (WSL)" | Select-Object -ExpandProperty IPAddress'

With the firewall rule above, everything works as expected! 😄

@olhybrius
Copy link

Turns out if you also use Docker Desktop, you can access your Windows host with host.docker.internal. Not only this removes the concern of IP adress changes, but it also doesn't need any firewall manipulation.

@fahminlb33
Copy link

@olhybrius This is the only workaround that worked for me. I've been using ngrok to forward local port so I can access it from WSL 2, now I can use host.docker.internal. Brilliant!

@theluk
Copy link

theluk commented Oct 28, 2021

hello, host.docker.internal currently has incorrect values when I am on a VPN.

Is there any other stable method?

@LeOndaz
Copy link

LeOndaz commented Nov 23, 2021

Take those in your .profile

export WINDOWS_HOST=cat /etc/resolv.conf | grep nameserver | cut -d ' ' -f 2
export WSL_HOST=ip addr | grep eth0 | cut -d ' ' -f 6 | tail -1 | cut -d '/' -f 1

The first one is an IP to access Windows from the services on WSL
The second one is an IP to access WSL from the services on Windows

@bendem
Copy link

bendem commented Apr 4, 2022

export WINDOWS_HOST=cat /etc/resolv.conf | grep nameserver | cut -d ' ' -f 2
export WSL_HOST=ip addr | grep eth0 | cut -d ' ' -f 6 | tail -1 | cut -d '/' -f 1

More robust version to get those on recent distros:

WINDOWS_HOST=$(ip --json route show default | jq -re '.[].gateway')
WSL_HOST=$(ip --json --family inet addr show eth0  | jq -re '.[].addr_info[].local')

@ghusta
Copy link

ghusta commented Sep 29, 2022

@bruno-brant At the time of writing this comment (Insiders Preview Build 18990 is the latest build on the fast ring), you'll need to access your Windows networking applications from Linux using the IP address of your Windows host in WSL2. You can find more details on how to do that here on our documentation.

The previous link is obsolete.

You can find a new one here : Accessing network applications with WSL

Particularly this paragraph : Accessing Windows networking apps from Linux (host IP)
It begins by looking at nameserver in /etc/resolv.conf

@ghusta
Copy link

ghusta commented Sep 29, 2022

The nameserver ip should be the same than the ip of the interface "vEthernet (WSL)" defined by Windows host.

You can normally find it with Windows terminal with :

netsh interface ipv4 show addresses "vEthernet (WSL)"

With PowerShell :

Get-NetIPConfiguration "vEthernet (WSL)"

More info :

Get-NetIPInterface "vEthernet (WSL)" -AddressFamily IPv4

@kyoh86
Copy link

kyoh86 commented Dec 1, 2022

We can overwrite nameserver in /etc/resolve.conf.
So I got the host address by PowerShell:

(Get-NetIPConfiguration "vEthernet (WSL)").IPv4Address.IPAddress

@viniciusao
Copy link

On your powershell as admin:

netsh interface portproxy add v4tov4 listenport=<container_port> listenaddress=0.0.0.0 connectport=<host_port> connectaddress=localhost

Then you might be able to access it using your LAN, VPN ip addresses.

@rikka0w0
Copy link

rikka0w0 commented Mar 23, 2023

With this script, you can get one of the ULA IPv6 Addresses (analog to a private IPv4 Address):
powershell.exe -Command 'Get-NetIPAddress -AddressFamily IPv6 -InterfaceAlias "vEthernet (WSL)" | Where-Object -FilterScript {$_.IPAddress.ToString().StartsWith("fd")} | Select-Object -ExpandProperty IPAddress -First 1'

To get one of the globally accessible IPv6 addresses, use:
powershell.exe -Command 'Get-NetIPAddress -AddressFamily IPv6 -InterfaceAlias "vEthernet (WSL)" | Where-Object -FilterScript {!$_.IPAddress.ToString().StartsWith("fd") -and !$_.I PAddress.ToString().StartsWith("fe")} | Select-Object -ExpandProperty IPAddress -First 1'
The above command excludes ULA addresses (fd::/8) and link-local addresses (fe::/8).

You can add these to your ~/.bashrc:

HOSTIP6ULA=$(powershell.exe -Command 'Get-NetIPAddress -AddressFamily IPv6 -InterfaceAlias "vEthernet (WSL)" | Where-Object -FilterScript {$_.IPAddress.ToString().StartsWith("fd")} | Select-Object -ExpandProperty IPAddress -First 1' | tr -d '\r')
HOSTIP6PUB=$(powershell.exe -Command 'Get-NetIPAddress -AddressFamily IPv6 -InterfaceAlias "vEthernet (WSL)" | Where-Object -FilterScript {!$_.IPAddress.ToString().StartsWith("fd") -and !$_.IPAddress.ToString().StartsWith("fe")} | Select-Object -ExpandProperty IPAddress -First 1' | tr -d '\r')

The above script also converter \r\n from Windows to \n, so that bash will be happy.

Example usage (Connecting to a X-Server on Windows with IPv6 ULA):
DISPLAY=[$HOSTIP6ULA]:0 xcalc

@nickshoe
Copy link

nickshoe commented Nov 27, 2023

@bruno-brant At the time of writing this comment (Insiders Preview Build 18990 is the latest build on the fast ring), you'll need to access your Windows networking applications from Linux using the IP address of your Windows host in WSL2. You can find more details on how to do that here on our documentation.

For impatients, Running below command on WSL2 and get your host IP from nameserver field.

cat /etc/resolv.conf

This seems to work as long as you have not set a pre-defined DNS server in the Windows network settings (IT may do this in a company scenario).

The command suggested in this comment have worked for me.

$ ip route show default | awk '{print $3}'

For those who wants to dynamically add an hostname resolution for this IP address to their WSL distro, adding this to your .bashrc (or .bash_profile, or wathever) do the trick (even though the user password is prompt every time a new bash instance is launched...):

### Add name resolution for the Windows host machine ip address ###
# Set Windows Host IP Address
export WINDOWS_HOST_IP=`ip route show default | awk '{print $3}'`
# Add Windows Host Name
export WINDOWS_HOST_NAME="windows-host"
if grep -q "$WINDOWS_HOST_NAME" "/etc/hosts"; then
        echo "$WINDOWS_HOST_NAME -> $WINDOWS_HOST_IP"
else
        echo "Enter your password to add $WINDOWS_HOST_NAME entry to /etc/hosts"
        if printf "" | sudo bash -c 'cat >> /etc/hosts'; then
                if grep -q "$WINDOWS_HOST_NAME" "/etc/hosts"; then
                        echo "Host entry for $WINDOWS_HOST_NAME already added to /etc/hosts"
                else
                        if printf "\n# Entry added by .bashrc\n%s\t%s\n" $WINDOWS_HOST_IP $WINDOWS_HOST_NAME | sudo bash -c 'cat >> /etc/hosts'; then
                                echo "Host entry for $WINDOWS_HOST_NAME added to /etc/hosts"
                        fi
                fi
        else
                echo "There were errors accessing the /etc/hosts file as sudoer"
        fi
fi

I've taken the env variable exporing example from this other comment.

This way you can connect to services running on your windows host by using this hostname windows-host.

Example:

$ telnet windows-host 80

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests