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

/etc/hosts file was overwritten by WSL at some point #3043

Closed
ownmaster opened this issue Mar 20, 2018 · 8 comments
Closed

/etc/hosts file was overwritten by WSL at some point #3043

ownmaster opened this issue Mar 20, 2018 · 8 comments
Labels

Comments

@ownmaster
Copy link

I had a hosts file with different content inside WSL for quite a long period of time (a few months at least, if not a year) and today I saw that it's content has been overwritten by System32\drivers\etc\hosts.
Now /etc/hosts starts with:

# 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.

I would like to understand:

  1. Since I never added those lines manually, it must be added during some system update - if I remove these lines does it really mean, that the file inside WSL will not be overwritten?
  2. Does WSL store a backup of this file somewhere, so I can restore it?
  3. If I decide to enable automatic file generation, can I force this process somehow without system reboot?

Windows version: 10.0.16299.309

@benhillis
Copy link
Member

The ability to disable automatic generation of the /etc/hosts file became available in build 17093 and is coming to stable builds of Windows in the next cumulative update which is due soon:
https://docs.microsoft.com/en-us/windows/wsl/wsl-config#set-wsl-launch-settings

@ownmaster
Copy link
Author

@benhillis so, I assume that the answer for 2 other questions is 'no': there is no backup by WSL and user can't trigger file update manually?

@benhillis
Copy link
Member

benhillis commented Mar 21, 2018

Unfortunately the old /etc/hosts file was not backed up. The generated hosts file will be re-created when a new instance is launched.

@ownmaster
Copy link
Author

@benhillis seems that all existing WSL console sessions must be closed before new session can get an updated hosts file.

@benhillis
Copy link
Member

@ownmaster - Correct, that is what I mean by a new instance.

@ajit555db
Copy link

Add the hosts as follows:

Open the following file in Notepad admin mode:
"C:\Windows\System32\drivers\etc\hosts"

add your hosts this at the end of the file e.g.

192.168.2.30 ubmaster
192.168.2.31 ubworker1
192.168.2.32 ubworker2
192.168.2.33 ubsrv

@CosmicCatnap
Copy link

Add the hosts as follows:

Open the following file in Notepad admin mode:
"C:\Windows\System32\drivers\etc\hosts"

add your hosts this at the end of the file e.g.

192.168.2.30 ubmaster
192.168.2.31 ubworker1
192.168.2.32 ubworker2
192.168.2.33 ubsrv

The issue in question is not how to find a workaround, its why WSL thinks it should be responsible for a core linux feature and why it transparently erases changes in a way that is against virtually every use case for WSL I can think of.. You should be given a blank hosts file because this issue was self-inflicted.

@treysis
Copy link

treysis commented Apr 20, 2022

Additionally to the windows hosts file, Ubuntu adds some own entries at the end of /etc/hosts. What is the basis for these lines? I want to add ::1 localhost because for some reason Ubuntu's default decided that this would not be good. I disagree. It's the default on many systems, including Debian, Windows, and macOS.

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

No branches or pull requests

5 participants