-
Notifications
You must be signed in to change notification settings - Fork 852
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
Comments
@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? |
WSL (Bash.exe) shares the IP address (and port availability) with Windows 10. From Windows CMD, you can issue 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. |
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. |
Thanks. |
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. |
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. |
@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,
|
@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. |
As @gurrpi mentioned, you can always check the windows 10 host IP in a wsl2 distro by checking the A quick cheeky helper, that you might want to add to your powershell profile, in case you occasionally use you wsl distro's 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! |
You may add this in your
|
I had to disable firewall too :-| |
Excellent tip! |
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 |
Another simple way: If there is no |
No need for 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! 😄 |
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. |
@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 |
hello, Is there any other stable method? |
Take those in your export WINDOWS_HOST= The first one is an IP to access Windows from the services on WSL |
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') |
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) |
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 :
With PowerShell :
More info :
|
We can overwrite
|
Then you might be able to access it using your LAN, VPN ip addresses. |
With this script, you can get one of the ULA IPv6 Addresses (analog to a private IPv4 Address): To get one of the globally accessible IPv6 addresses, use: You can add these to your
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): |
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 ### 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 Example: $ telnet windows-host 80 |
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?
The text was updated successfully, but these errors were encountered: