shayne / go-wsl2-host

Automatically update your Windows hosts file with the WSL2 VM IP address
1.65k stars 142 forks source link

Add Windows (host) IP to hosts file + fix install for AD accounts #2

Closed DanielSel closed 5 years ago

DanielSel commented 5 years ago

1. Add Windows (host) IP to hosts file

Additionally to making the WSL VM accessible under a static name from the Windows host, implement the same vice versa

2. Fix install for AD accounts

Automatic pre-pending of ".\" before the account name breaks service logon for AD accounts. Changed to only prepending it when no "\" or "@" was found in the username.

Big fat thanks @shayne for idea and implementation of this utility!

shayne commented 5 years ago

Thanks for the PR!

Quick question, can you explain the Windows (host) IP change? What's the use case? I ran your version and can't find a use for the windows.local host.

DanielSel commented 5 years ago

If you have a service running on your windows host and you want to access it from the WSL2 VM. The host ip also changes on every reboot. Since entries in the windows hosts file get replicated into /etc/hosts in WSL, I figured it would be nice to have it all in one place and re-use your logic.

shayne commented 5 years ago

Thanks for the explanation. A couple of comments:

1) So I assumed that's what it was for and tested it. But that IP didn't work for anything I tried.

On the host I opened PowerShell and ran `> nc -l -p 8000`. From WSL2 I tried `$ curl windows.local:8000` and never saw the request come through to PowerShell.

I tried the IP itself too and nada. I then tried my standard Ethernet internal IP the same thing, from WSL2, and it worked fine.

I'm not sure if I'm alone here or not...

2) Perhaps more of a blocker: one of the hidden issues in this projects is /etc/hosts on the WSL2 side. The service can only detect the WSL2 IP once WSL2 has "booted". which makes sense. It then updates the Windows hosts file. This is important, because it means what's in /etc/hosts is stale. It contains the IPs from the last WSL2 "boot". It's always one behind in that sense.

The solution here is tricky. We could write to `/etc/hosts` but that would require super-user privilege and I don't want to have folks allow passwordless `sudo` access. 

There is a `HOSTALIASES` env var that could work, but I haven't tested it yet.

I'd like to pick the changes for AD users, but need to test Microsoft account users first.

DanielSel commented 5 years ago

I think my brain was fried when I wrote the explanation. I disabled the automatic hosts file generation in /etc/wsl.conf. The WSL2 VM uses the windows host as DNS server and therefore indirectly picks up all entries in the windows hosts file.

I tried rebooting a couple of times now and adding random entries to my windows hosts file. It's picked up immediately by WSL.

If you feel like this is a good approach, I can update the documentation to include the setting in /etc/wsl.conf.

Sorry I totally forgot about my config.

shayne commented 5 years ago

Ok that's interesting. I'll try disabling the hosts propagation and see what I get.

Can you try:

# in powershell
> nc -l -p 8000

# in wsl2
$ curl windows.local:8000

After that, do you see any output in PowerShell? I can't seem to access windows.local by name or IP. Ping fails and no ports seem to work for me.

DanielSel commented 5 years ago

Sorry for taking so long to respond. I was super busy at work with other tasks. The output I see is:

GET / HTTP/1.1 Host: windows.local:8000 User-Agent: curl/7.58.0 Accept: */*

I tried with various server applications / clients and everything seems to work fine for me. I use it every day. Is maybe your Windows Firewall blocking the port for you? For the firewall this traffic is coming from another network so you might have to add an exception.