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 being recreated each time I restart. #398

Closed
i-frost opened this issue May 19, 2016 · 20 comments
Closed

/etc/hosts being recreated each time I restart. #398

i-frost opened this issue May 19, 2016 · 20 comments

Comments

@i-frost
Copy link

i-frost commented May 19, 2016

Hi

I want to add the names of my machines and ip addr to the hosts file in /etc but after a reboot the file is recreated thus wiping the changes I added.

I tried adding the IP addr and names to the windows hosts file in windows/system32/drivers/etc/hosts

But it doesn't then pick up the IP addr in the bash shell.

Is this possible as I don't want to have to keep typing in the ip addr to use ssh?

Thanks

@fpqc
Copy link

fpqc commented May 19, 2016

i-frost, doesn't it get imported on bash.exe startup by the C:\Windows\System32\etc\hosts file? Microsoft said that they're trying to have the base environment make use of Windows resources as much as possible, and likely this includes /etc/hosts.

@i-frost
Copy link
Author

i-frost commented May 19, 2016

I tried adding the details to the Windows hosts file but bash does not pick
them up.
On May 19, 2016 6:22 PM, "fpqc" notifications@github.com wrote:

i-frost, doesn't it get provided by the C:\Windows\System32\etc\hosts file?


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
#398 (comment)

@fpqc
Copy link

fpqc commented May 19, 2016

Could you do the following workaround until they fix it: Add the line to .bashrc echo $hostslineyouwantedtoadd | sudo tee -a /etc/hosts

@i-frost
Copy link
Author

i-frost commented May 20, 2016

Hey

I tried adding it to .bashrc but I just get permission denied when i start
the bash shell.

I then tried adding my user name to sudoers and setting NOPASSWD but this
cant get it to write it into the hosts file.

I thought this would save my bacon as I cannot get this laptop to work with
its track-pad in Linux but can in windows:-(

Thanks

On Thu, May 19, 2016 at 6:27 PM, fpqc notifications@github.com wrote:

Could you do the following workaround until they fix it: Add the line to
.bashrc sudo echo $hostslineyouwantedtoadd >> /etc/hosts


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
#398 (comment)

@fpqc
Copy link

fpqc commented May 20, 2016

You tried the original thing I wrote (which was wrong), or the corrected one?

The thing I think will work in bash.rc is actually echo $hostslineyouwantedtoadd | sudo tee -a /etc/hosts. piping to tee -a does the same thing as >> but it is a linux program and so you can run it with sudo (instead of running all of bash with privileges). The original thing I wrote was wrong.

@benhillis
Copy link
Member

benhillis commented May 20, 2016

I'm taking a look at a way to work around this. My current thinking is to add a header to the top of /etc/hosts that says something like:

# This file was automatically generated by WSL. To prevent automatic generation of this file, remove this line.

That way if you don't want WSL to automatically generate this file you'll be able to remove the header. If you want WSL to regenerate the file you can either delete the file or add back the header.

@i-frost @fpqc Does this sound like a decent solution to you?

@i-frost
Copy link
Author

i-frost commented May 20, 2016

Sounds good to me:-)

Thanks Ben!

@fpqc
Copy link

fpqc commented May 20, 2016

@benhillis I guess! Wouldn't a good solution also be to have it just read the Windows HOSTS file as a device called like /dev/hosts (or a process called /proc/hosts) and then make /etc/hosts a symlink?

That's how you guys are doing it for outputting memory management info, at least. I think that's a good idea. The more you rely on Windows-provided stuff, the fewer weird problems you'll have where you have to configure things twice, once for Windows and once for WSL.

@benhillis
Copy link
Member

@fpqc - I'm not sure that will work because the hosts files are different formats. Linux host files are tab separated and Windows are space separated, for example.

@fpqc
Copy link

fpqc commented May 20, 2016

@benhillis I don't doubt the abilities of you guys at Microsoft to parse and reformat the HOSTS file =), but it's up to you!

@niftyhacking
Copy link

On Fri, May 20, 2016 at 11:09 AM, Ben Hillis notifications@github.com
wrote:

I'm taking a look at a way to work around this. My current thinking is to
add a header to the top of /etc/hosts that says something like:

This file was automatically generated by WSL. To prevent the contents of

this file from being overwritten, remove this line.

That way if you don't want WSL to automatically generate this file you'll
be able to remove the header. If you want WSL to regenerate the file you
can either delete the file or add back the header.

@i-frost https://github.com/i-frost @fpqc https://github.com/fpqc
Does this sound like a decent solution to you?

It is half the answer.
Once the file has been modified there needs a way to allow WSL to
install a fresh one.

Better to have a line with instructions to change Yes (default) to No:

WSL Update etc/hosts Yes

WSL Update etc/hosts No

Deletion of the comment defaults to Yes.
Comments are almost free.
In an ideal world the old file should be renamed to /etc/hosts.old
and a new version not installed be /etc/hosts.new

The hosts file is one way to abuse internet connections.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#398 (comment)

T o m M i t c h e l l

@benhillis
Copy link
Member

benhillis commented May 23, 2016

@niftyhacking If the /etc/hosts file is deleted, a new one will be auto-generated (with the comment). I've checked in a fix for this and it will be making its way to the Windows Insider fast ring in a future build (a few weeks out likely).

@niftyhacking
Copy link

@niftyhacking https://github.com/niftyhacking If the /etc/hosts file is
delete, a new one will be auto-generated (with the comment). I've checked
in a fix for this and it will be making its way to the Windows Insider fast
ring in a future build (a few weeks out likely).

Thank you.

T o m M i t c h e l l

@russalex russalex added the fixed label Jun 8, 2016
@russalex
Copy link
Contributor

russalex commented Jun 8, 2016

Ben's fix made it into build 14361.

@i-frost
Copy link
Author

i-frost commented Jun 8, 2016

Thank you so much:-)
On Jun 8, 2016 9:36 PM, "Russ Alexander" notifications@github.com wrote:

Ben's fix made it into build 14361
https://msdn.microsoft.com/en-us/commandline/wsl/release_notes.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#398 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AQGoWK3GHmB0n0qPjqY3NZltQNmp6QKUks5qJyergaJpZM4IiRT6
.

@lucastheisen
Copy link

@benhillis , this solutions feels wrong to me. It seems that if you add something to your hosts file, you would want it to work both Windows side, and in WSL. I often use localhost IP's over ssh tunnels and prefer having aliases for each. I have this in my hosts:

127.0.0.2  myserver.home
127.0.0.3  myotherserver.home

Then have an ssh tunnel:

ssh mybastion.my.domain -n -N -C \
    -L myserver.home:13389:myserver.home:3389 \
    -L myserver.home:13389:myserver.home:3389 \
    &

Allowing me to access remote desktop to myserver.home:13389 and myotherserver.home:13389. Your approach would require i come up with a way to synchronize the 2 hosts files to make this work...

Am I missing something? Perhaps you could add a second option to use windows hosts?

Currently using:

PS C:\Users\ltheisen> [System.Environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      14393  0
PS C:\Users\ltheisen> (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId
1607

@lucastheisen
Copy link

Looks like #2059 states:

The functionality of adding windows hosts to /etc/hosts is currently only available in Insider builds

@ed-alertedh
Copy link

(if anyone else finds this thread on google like me, adding windows hosts to /etc/hosts is now in the regular builds)

@eric-belhomme
Copy link

eric-belhomme commented Apr 25, 2018

Hello,

I'm running build 16299 and the comment I have in /etc/hosts is diferent:

cat /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.
127.0.0.1	localhost
[...]

In my use case, I don't want to modify Windows's hosts file, just the WSL one ! So adding the entries in Windows's hosts file is not an option for me !

Ideally a "diff" mechanism between Windows and WSL whould be IMHO the best approach..

Regards,

PS: I'm running Debian in my WSL

@Brian-Perkins
Copy link

This is configurable via the wsl.conf setting 'generateHosts'.

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

No branches or pull requests

9 participants