Linux 覆盖 windows-linux-subsystem 上的 /etc/hosts

Env*_*nvy 10 linux windows-subsystem-for-linux

我在 Windows Linux 子系统中解析主机名时遇到问题,所以我删除了 /etc/hosts 并编写了它的新版本。该解决方案解决了我的问题,但是在每次重新启动后,系统开始以某个默认版本覆盖 /etc/hosts,因此我需要一次又一次地设置主机。是否可以设置 linux 子系统,以便它复制原始 Windows 主机文件或至少不删除已写入的文件?

小智 7

我使用 Ubuntu 16.04.5 LTS 作为 Windows 下的 Linux 子系统。我在 Ubuntu 下的 /etc/hosts 文件说

#This file is automatically generated by WSL based on the Windows hosts file:
#%WINDIR%\System32\drivers\etc\hosts. Modifications to this file will be overwritten.
Run Code Online (Sandbox Code Playgroud)

所以我只是简单地改变了#%WINDIR%\System32\drivers\etc\hosts


小智 2

您的问题在这里得到解决: https ://github.com/Microsoft/BashOnWindows/issues/398

为了防止 WSL 在每次 bash 启动时自动生成 /etc/hosts 文件,您需要删除 /etc/hosts 中的以下注释行:

# This file was automatically generated by WSL. To prevent automatic generation of this file, remove this line.
Run Code Online (Sandbox Code Playgroud)