Skip to content

Support for setting Windows hosts within WSL #45

@Silic0nS0ldier

Description

@Silic0nS0ldier

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions