question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for setting Windows hosts within WSL

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
unilynxcommented, 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 ?

0reactions
ferosscommented, Nov 19, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Accessing network applications with WSL - Microsoft Learn
Accessing Linux networking apps from Windows (localhost) ... Accessing a WSL 2 distribution from your local area network (LAN).
Read more >
The Ultimate Guide to Windows Subsystem for Linux ...
Learn how to natively work within Linux on Windows 10 with Windows Subsystem for Linux (WSL in Windows) in this HUGE guide!
Read more >
Linux overrides /etc/hosts on windows-linux-subsystem
#This file is automatically generated by WSL based on the Windows hosts file: #%WINDIR%\System32\drivers\etc\hosts. Modifications to this file will be ...
Read more >
Windows 10: Syncing Windows hosts file with WSL 2 hosts file.
Discus and support Syncing Windows hosts file with WSL 2 hosts file. in Windows 10 Ask Insider to solve the problem; If I...
Read more >
Editing /etc/hosts using WSL vs. using Notepad as an ...
You do realize that c:\Windows\System32\drivers\etc\hosts is the Windows hosts file - this itself has nothing to do with Ubuntu or WSL.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found