【问题标题】:WSL hosts file with custom domainWSL 托管具有自定义域的文件
【发布时间】:2022-01-05 08:33:20
【问题描述】:

我创建了一个基本的快递应用

const express = require('express');

const app = express();

app.get("*", (req, res) => {;
  res.contentType('html');
  res.send("HELLO FROM WSL");
});
const port = 80
app.listen(port);

然后我在c:\windows\system32\drivers\etc\hosts中添加以下条目

127.0.0.1        custom.local

然后我关闭 wsl wsl --shutdown 并重新打开以启动我的 express 应用程序。

如果我从 WSL (cat /etc/hosts) 检查主机文件,我会得到以下结果

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
127.0.0.1       localhost
127.0.1.1       LAPTOP-ZECKA.localdomain        LAPTOP-ZECKA
127.0.0.1       custom.local

然后我去http://custom.local windows 中的槽 chome。但它不显示我的快递应用程序。 (如果我在 windows 而不是 wsl 上运行 express 则效果很好)。

我的主机文件有什么问题?

【问题讨论】:

  • 我确实有同样的问题。我在 WSL 中运行 ifconfig 并复制 IP 并将其添加到我的主机文件中。这可行,但现在我需要在每次重新启动时重新配置它,因为 IP 不断变化。

标签: windows-subsystem-for-linux wsl-2 hosts


【解决方案1】:

以前,我的 WSL 是第 1 版,一切正常。然后我决定升级到 WSL 2 - 与许多其他人一样 - 我失去了互联网连接。谢天谢地,我可以通过运行轻松地将其恢复:

sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'

显然,这只是一个 DNS 问题。我可以忍受。但是,然后我重新启动了 WSL,名称服务器被设置回 172.31.208.1。所以我决定完全按照 /etc/resolv.conf 中 cmets 中所写的内容进行操作:

此文件由 WSL 自动生成。要停止自动生成此文件,请将以下条目添加到 /etc/wsl.conf:

[网络]

generateResolvConf = false

然后运行了以下几行:

sudo bash -c 'echo "[network]" > /etc/wsl.conf' sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf' 我很自豪我如此优雅地解决了这个问题,直到我再次重新启动我的 WSL。现在,resolv.conf 为红色,无法从 Ubuntu 访问。当我尝试从 Windows 访问它时,我看到的只是一个空文件。

预期行为 重启 WSL 后,resolv.conf 会保留用户定义的值。

实际行为 重启 WSL 后,resolv.conf 为空或根本无法访问。

【讨论】:

    猜你喜欢
    • 2018-09-13
    • 1970-01-01
    • 2012-06-08
    • 2021-07-23
    • 2016-09-18
    • 2016-03-16
    • 2017-03-23
    • 1970-01-01
    • 2017-01-02
    相关资源
    最近更新 更多