【问题标题】:How to access to the web server which running on WSL(Windows Subsystem for Linux) from local network如何从本地网络访问运行在 WSL(Linux 的 Windows 子系统)上的 Web 服务器
【发布时间】:2018-09-24 21:49:19
【问题描述】:

在将 Ubuntu 安装为 WSL(Linux 的 Windows 子系统)后,我运行了:

root@teclast:~# python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 ...

并尝试从我的 Windows 机器 http://0.0.0.0:8000 或 http://192.168.1.178:8000 访问此 Web 服务器,但没有成功,Web 服务器只能通过地址 http://127.0.0.1:8000 或 http://localhost:8000 访问,这意味着我无法连接到此来自我网络中另一台电脑的网络服务器。是否可以从外部访问 WSL?

【问题讨论】:

  • 也许this可以帮助你。
  • 感谢您的帮助,但服务器仍然只能通过 localhost 访问。并且无法从另一台 PC 连接到此服务器。

标签: linux windows ubuntu webserver windows-subsystem-for-linux


【解决方案1】:

对于刚接触这篇文章的任何人,请查看 Microsoft doc https://docs.microsoft.com/en-us/windows/wsl/networking#accessing-linux-networking-apps-from-windows-localhost。

根据文档,如果您运行的是旧版本的 Windows (构建 18945 或更低版本),您将需要获取的 IP 地址 Linux 主机虚拟机

这应该是 Windows Build 18945 之后的非问题。

【讨论】:

    【解决方案2】:

    类似于@countach 的回答:

    如果使用 Ubuntu,请在 WSL 终端中输入 ip address。查找显示#: eth0 ... 的条目,其中# 是一个小数字。那里有一个ip地址。使用它。

    【讨论】:

      【解决方案3】:

      我关注the answer by @toran-sahu 向 Windows Defender 防火墙添加入站规则,但最近(在添加第二个 wsl2 实例后)它再次停止工作。我遇到了this issue thread 并在 cmd 提示符下运行以下命令让它再次为我工作。

      wsl --shutdown
      

      更新:这个问题似乎来自启用了快速启动https://stackoverflow.com/a/66793101/8917310

      【讨论】:

      • 说实话,wsl --shutdown 可能解决不了这个问题。当它是快速启动问题时,甚至 localhost 转发都会中断,因此原始发布者将无法通过 Windows 中的localhost 访问服务器。既然他们说这行得通,那么这不太可能是您正在考虑的问题。
      • wsl --shutdown 本身,没有添加任何防火墙规则,实际上对我有用。
      【解决方案4】:

      现有的答案都不适合我,因为 WSL2 在自己的虚拟机中运行并且有自己的网络适配器。您需要某种桥接或端口转发才能使非本地主机请求正常工作(即来自同一网络上的另一台主机)。

      我在https://github.com/microsoft/WSL/issues/4150 中找到了一个可以解决问题的脚本:

      $remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
      $found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
      
      if( $found ){
        $remoteport = $matches[0];
      } else{
        echo "The Script Exited, the ip address of WSL 2 cannot be found";
        exit;
      }
      
      #[Ports]
      
      #All the ports you want to forward separated by coma
      $ports=@(80,443,10000,3000,5000);
      
      
      #[Static ip]
      #You can change the addr to your ip config to listen to a specific address
      $addr='0.0.0.0';
      $ports_a = $ports -join ",";
      
      
      #Remove Firewall Exception Rules
      iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
      
      #adding Exception Rules for inbound and outbound Rules
      iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
      iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";
      
      for( $i = 0; $i -lt $ports.length; $i++ ){
        $port = $ports[$i];
        iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
        iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
      }
      

      从提升的/管理员 Powershell 会话运行此脚本对我来说是诀窍。这允许访问在一个端口上运行的 WSL2 服务,该端口可从同一端口上的 Windows 主机 IP 访问。

      【讨论】:

      • 对我来说效果很好——确保你的 WSL2 发行版上安装了 ifconfig。这也可以在以powershell -executionpolicy bypass -f path\to\script.ps1 身份登录时添加到任务计划程序,延迟至少 10 秒以允许 WSL2 首先启动(使您的 Linux 实例可用于从中获取机器的 IP)。
      【解决方案5】:

      在你的虚拟机中,执行 ifconfig

      您将在第一部分 (eth0:) inet x.x.x.x 中看到您的 IP

      这个 x.x.x.x 是你必须在浏览器中输入的 IP。

      【讨论】:

        【解决方案6】:

        我已经在重新安装 WSL 的 Microsoft Windows 更新 (KB4532132) 上进行了测试,它按预期工作。似乎问题与旧 Windows 版本或旧 WSL 版本有关。

        【讨论】:

          【解决方案7】:

          请按照@erazerbrecht 分享的link 中提到的步骤,通过提供您的IP 地址(而不是使用本地主机)和端口号来运行您的HTTP 服务器。

          示例:
          root@teclast:~# python3 -m http.server -b 192.168.1.178 8000 Serving HTTP on 192.168.1.178 port 8000 (http://192.168.1.178 :8000/) ...

          否则你也可以这样做而不是关注link:
          1. 转到 Windows Defender 防火墙
          2.选择inbound
          3.创建new rule;下一个
          4.选择Program作为规则类型;下一个
          5.选择All Program;下一个
          6.选择allow the connection;下一个
          7. 检查所有 3 个(域、私有、公共);下一个
          8. 提供规则名称
          9. 完成
          10.你很高兴

          【讨论】:

          • 这些是 Microsoft 提供的说明。 Windows 防火墙正在阻止 WSL 公开的连接。
          • 它确实有效。确保将 IP 替换为本地 Windows IP。访问网页时也可以使用 IP:PORT 访问。
          • 我可以确认遵循所有这些说明不起作用。对我来说,在端口 8000 上查看我的 IP 会在浏览器中显示 Windows 用户配置文件目录的文档树。然而,Linux HTTP 服务器确实看到并记录了请求。
          • 这个答案中的任何建议都不适用于 WSL2。服务无法绑定到 Windows IP 地址,因为 WSL2 有自己的网络适配器。防火墙也一样。在 Windows 中运行服务时,这些端口已经在工作,因此再次允许它们没有用。
          • @mlibby 你找到解决方案了吗? ?
          猜你喜欢
          • 2019-01-02
          • 2021-04-17
          • 2016-05-29
          • 2017-05-21
          • 2019-02-13
          • 2020-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多