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

Support for setting Windows hosts within WSL #45

Open
Silic0nS0ldier opened this issue May 18, 2020 · 9 comments
Open

Support for setting Windows hosts within WSL #45

Silic0nS0ldier opened this issue May 18, 2020 · 9 comments

Comments

@Silic0nS0ldier
Copy link

Provided an elevated shell session is used, it is possible to modify the Windows hosts file within WSL (Windows Subsystem for Linux). It would be useful if hostile supported working with Windows hosts within WSL.

An example of adding hosts;

declare -a LOCAL_DOMAINS=("some-site.test" "api.some-site.test")

# add Domains to hosts
for domain in ${LOCAL_DOMAINS[@]}
do
    if grep -q microsoft /proc/version; then
        declare -a windowsHosts=$(wslpath -u -a C:\\Windows\\System32\\drivers\\etc\\hosts);
        if ! grep --quiet ${domain} $windowsHosts; then
            echo "Adding ${domain} to Windows hosts file..."
            echo "127.0.0.1 ${domain}" | tee -a $windowsHosts || { echo >&2 "Failed to add entry to hosts file - ${domain}, try running WSL terminal instance as admin" ; exit 1; }
        fi
    fi
done

With standard host drive mount configuration, wslpath -u -a C:\\Windows\\System32\\drivers\\etc\\hosts returns /mnt/c/Windows/System32/drivers/etc/hosts. The resolved path should be treated as case sensitive.

Thoughts?

@ghost
Copy link

ghost commented May 28, 2020

I'm also using wsl2, and I was thinking the same thing.

@Silic0nS0ldier
Copy link
Author

Calling out to a shell will be necessary to resolve the path here. To do so reliably in any case (there can be some funky Windows setups out there).
The following will return needed path irrespective of device specific changes (called from PowerShell Core in Ubuntu 20.04 WSL 2).

wslpath -u $(cmd.exe /c echo "%WINDIR%/System32/drivers/etc/hosts")
# /mnt/c/WINDOWS/System32/drivers/etc/hosts

Interestingly WINDOWS worked fine even though in Windows Explorer that folder shows up as Windows. Probably other factors at play I'm not aware of (or even a WSL 2 update that changed this).

@ghost
Copy link

ghost commented May 28, 2020

I did my own version since it seems this project is abandoned.

@Silic0nS0ldier
Copy link
Author

Host files don't change much over the course of 2 years. I'd go as far to say that they don't change much over the course of a decade. More a case of low attention and prioritisation then abandonment.

@feross (if you happen to see this), would you be open to accepting a PR with the functionality described here? Happy to add this to my project pile if so.

@ghost
Copy link

ghost commented Jun 1, 2020

@feross I hope you respond, we are sitting ducks right now trying to modify your code. could implemented is-wsl from npmjs and go from there, this module verifies if your using wsl and docker.

@unilynx
Copy link
Collaborator

unilynx commented Jun 2, 2020

@benlumia007 I can probably merge any code changes if needed, but I cannot really judge what the expected interfaces are on Windows - (I only test but don't really develop on that platform, wouldn't even have expected them to have two different 'hosts' files. I only know about the one in system32/drivers/...)

Would a solution for WSL2 also apply to WSL?

You said you already have your own solution, is that in a fork somewhere or not based on hostile code ?

@ghost
Copy link

ghost commented Jun 2, 2020

@unilynx

Since I am uisng WSL2 and ubuntu, I had to take out the scripts and put it into my project, What I did was i had to add a module called is-wsl, then, replace WINDOWS = isWSL

const isWSL = require( "is-wsl" );

var WINDOWS = isWSL;
var EOL = WINDOWS
  ? '\r\n'
  : '\n'

exports.HOSTS = WINDOWS
  ? '/mnt/c/Windows/System32/drivers/etc/hosts'
  : '/etc/hosts'

this means that only wsl works, but if you're in win32 won't work. so the if statements may be need a fix.

@ghost
Copy link

ghost commented Jun 2, 2020

is-wsl also checks if is docker included,

@feross
Copy link
Owner

feross commented Nov 19, 2020

Sure, I'm happy to review a PR that adds support for Windows.

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

3 participants