Support for setting Windows hosts within WSL
See original GitHub issueProvided 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:
- Created 3 years ago
- Comments:9 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 ?
Sure, I’m happy to review a PR that adds support for Windows.