Automatically update your Windows hosts file with the WSL2 VM IP address

Related tags

Network go-wsl2-host
Overview

go-wsl2-host

As of v0.2.0, I've dropped support for windows.local, if this was important let me know and I can add it back in.

Unable to enter your username? Having trouble starting the service? See issue #10

A workaround for accessing the WSL2 VM from the Windows host.

This program installs as a service and runs under the local user account. It automatically updates your Windows hosts file with the WSL2 VM's IP address.

The program uses the name of your distro, modified to be a hostname. For example "Ubuntu-18.04" becomes ubuntu1804.wsl. If you have more than one running distro, it will be added as well. When the distro stops it is removed from the host file.

I wrote this for my own use but thought it might be useful for others. It's not perfect but gets the job done for me.

To install and run, download a binary from the releases tab. Place it somewhere like your Documents/ folder.

Open an elevated/administrator command prompt:

> .\wsl2host.exe install
Windows Username: 
   
    
Windows Password: 
    

    
   

The program will install a service and start it up. Launch wsl then from a cmd prompt, run ping ubuntu1804.wsl. You can check the Windows hosts file to see what was written. The service will automatically update the IP if the WSL2 VM is stopped and started again.

The Windows hosts file is located at: C:\Windows\System32\drivers\etc\hosts

To remove / uninstall the service:

NOTE: Upgrading Windows Insider will remove the service, but not cleanly. To reinstall after upgrading, first make sure you've downloaded the latest version of wsl2host, then run remove before install

Open an elevated/administrator command prompt:

> .\wsl2host.exe remove

Specifying aliases

As of v0.3 you can now specify aliases that point to your WSL2 VM IP. Having some.client.local, may be useful in your development workflow.

To do this, create the file ~/.wsl2hosts in your default WSL2 distro. Host names are space separated:

some.client.local my-app.local wsl.local
Issues
  • I can't enter my username

    I can't enter my username

    Hello,

    I'm trying to install the service but my username isn't accepted.

    I have a microsoft account, and passing my email address gets me a "specified domain doesn't exist".

    So I tried to get my local username using "net user" which is "Julien Goux", but as soon as I type it I got the error "invalid username", I can't even type my password.

    Maybe it's because I have a space in my username?

    opened by jgoux 19
  • Doesn't Work

    Doesn't Work

    Downloaded 0.11 zip and .exe

    Placed in directory on my Win10 system and extracted the zip (extracted files and .exe all in same directory, opened CMD as administrator in directory ran .\go-wsl2-host.exe install and entered my username and password when asked.

    Launched Debian WSL2

    Opened HOST file no IP added and no wsl.host in list.

    Stopped Debian and restarted windows.

    Launched Debian, still nothing added to HOST file, running ping wsl.local returns "could not find host!

    Did I do something wrong??? I would really like for this to work! updating the HOST file manually every time I reboot is a real pain.

    Going to try alpha 0.1.0 now to see if that is any better!

    Thank You, sidgrafix

    opened by sidgrafix 8
  • Ping request fail

    Ping request fail

    Tried in v0.3.2 and v0.3.0. To get service started, I had to set logon to local system account. After starting wsl (ubuntu 18.04 in my case), and trying ping ubuntu1804.wsl, ping request could not find host. I tried creating an alias wsl.local in ~/.wsl2hosts but still no response on ping. Any other diagnostics I can run to debug?

    opened by phelps-matthew 6
  • Add Windows (host) IP to hosts file + fix install for AD accounts

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

    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!

    opened by DanielSel 6
  • Add config to allow multiple hostnames

    Add config to allow multiple hostnames

    I'd love it if there was a config file that can be used to create more than one hostname with the ip. Maybe even replace the any existing lines of the hostname with the name ip.

    opened by scrossan-crown 5
  • Modify the method of getting ip and host alias

    Modify the method of getting ip and host alias

    Command hostname -I is not available to get VM's IP address in some cases, e.g in Arch Linux, so I did something to let user set custom command. For the reason that ~/.wsl2hosts was already used to set custom host alias, I combined these. Now we can set custom command and custom host alias in one file.

    opened by GoodbyeNJN 4
  • Service won't start but somehow it works

    Service won't start but somehow it works

    Service won't start, either with command or doing it through windows services.

    HOWEVER, somehow the wsl line appears when executing debug.

    Btw, when executing debug, hangs in "starting wslhost service" and then I have to Ctrl-C

    PS C:\Users\lalo> .\wsl2host.exe start
    2019/12/14 21:24:38 failed to start wsl2host: could not start service: The service cannot be started due to an error in the login.
    PS C:\Users\lalo> .\wsl2host.exe debug                                                                   
    wsl2host.info(1): starting wsl2host service                                                                                          
    wsl2host.info(1): wsl2host-0                                                                    
    wsl2host.info(1): wsl2host service stopped  
    

    Another question:

    In hosts file, does order matter? because I've a lot of vhosts and I want to alias them all to the same wsl ip.

    Looks like the wsl line goes always at the end

    opened by eduardoarandah 4
  • Usage examples

    Usage examples

    Hi... Thanks. I downloaded this app and managed to get it running after shortening my password as noted in the other issues here.

    Readme talks about connecting from WSL/Linux to Windows/local.host. I have two questions, hopefully for the general benefit of others.

    • Can the connection be done the other way around: from Windows/local.host to WSL/Linux? How?
    • Can those who are experts in the field share their experience / usage examples? What are you doing with this app?

    Thanks much.

    opened by s-usr 4
  • Could you add the hosts IP address to etc/hosts

    Could you add the hosts IP address to etc/hosts

    Hi,

    first of all thanks for this really helpful tool!

    I run into a situation where I would like to access a service (postgres) I'm running on the Windows host from within wsl2. My solution would be to patch /etc/hosts with the IP from resolve.conf on every start. But I think it might be way better if you could add this functionality to your service as I have this running anyway.

    I'm not sure if this would be the "windows.local" functionality that got removed after 0.2, if so and possible, I'd like to have it back :)

    And if I may ask, it possible please add the same alias feature to it that you added for the wsl2 ip address.

    Let me know if I can help testing this.

    Thanks, Torsten.

    opened by tuhlmann 3
  • Program

    Program "kinda" corrupts the hosts file

    I think this image pretty much explains itself: image

    However if viewed with hex viewer: image

    We can see that there is 3-4 bytes of random data that causes the encoding to change.

    opened by takase1121 3
  • Security issue: Service not removable. Task: Provide uninstall instructions.

    Security issue: Service not removable. Task: Provide uninstall instructions.

    It would be helpful to provide uninstall instructions on the project's README or uninstall functionality in code since this service is registered but not removable. So I thought it would be useful to post this recommendation as a # TODO ?

    Examples:

    Testing wsl2host.exe stop will cause failed to stop wsl2host: could not access service: The specified service does not exist as an installed service.

    Testing wsl2host.exe remove will cause failed to remove wsl2host: service wsl2host is not installed

    But trying to install again wsl2host.exe remove will cause failed to install wsl2host: SetupEventLogSource() failed: SYSTEM\CurrentControlSet\Services\EventLog\Application\wsl2host registry key already exists

    What means the service is still on the system but cannot be removed nor reinstalled.

    opened by diqidoq 3
  • Fix GetHostIP regular expression

    Fix GetHostIP regular expression

    Hello! There is an issue with host ip detection in non-english locales. In my env I have netsh output in the following format: "IP-xxx xxx.xxx.xxx.xxx" There is not space after IP, and there is no colon.

    opened by g9rga 0
  • Using the set account, the wsl2 host service can be started, but it will shut down automatically after a few seconds

    Using the set account, the wsl2 host service can be started, but it will shut down automatically after a few seconds

    Using the set account, the wsl2 host service can be started, but it will shut down automatically after a few seconds However, using the local account system login of the first option can keep the service running, but the hosts are not set successfully image

    opened by FairyWorld 0
  • [service] host file infinite loop of adding all entries.

    [service] host file infinite loop of adding all entries.

    I'm having the issue, that all entries of ~/.wslhosts are being re-added every x-seconds into the hosts file of my system. Making this service unusable. Debug has no output.

    opened by xewl 0
Releases(v0.3.5)
  • v0.3.5(Jul 21, 2020)

  • v0.3.4(Mar 15, 2020)

    v0.3.4 Changes

    • A more robust implementation of detecting the IP address using /proc (fix for #17)

    v0.3.3 Changes

    • Introduce distro agnostic approach to getting WSL IP using /proc/net/fib_trie
    • Ignore Docker Desktop distros (having prefix "docker-desktop")

    To upgrade:

    From an elevated / Administrator console

    > .\wsl2host.exe stop
    > .\wsl2host.exe remove
    > .\wsl2host.exe install
    
    Source code(tar.gz)
    Source code(zip)
    wsl2host.exe(3.37 MB)
  • v0.3.3(Mar 11, 2020)

    v0.3.3 Changes

    • Introduce distro agnostic approach to getting WSL IP using /proc/net/fib_trie
    • Ignore Docker Desktop distros (having prefix "docker-desktop")

    To upgrade:

    From an elevated / Administrator console

    > .\wsl2host.exe stop
    > .\wsl2host.exe remove
    > .\wsl2host.exe install
    
    Source code(tar.gz)
    Source code(zip)
    wsl2host.exe(3.36 MB)
  • v0.3.2(Nov 12, 2019)

    v0.3.1 Changes

    • Fix error logic around getting IP from WSL2 VM
    • Add more Windows Event Viewer logging

    Specifying aliases

    As of v0.3 you can no specify aliases that point to your WSL2 VM IP. Having some.client.local, may be useful in your development workflow.

    To do this, create the file ~/.wsl2hosts in your default WSL2 distro. Host names are space separated:

    some.client.local my-app.local wsl.local
    

    To upgrade:

    From an elevated / Administrator console

    > .\wsl2host.exe stop
    > .\wsl2host.exe remove
    > .\wsl2host.exe install
    
    Source code(tar.gz)
    Source code(zip)
    wsl2host.exe(3.36 MB)
  • v0.3.1(Nov 11, 2019)

    v0.3.1 Changes

    • Add f.Sync() call #9

    Specifying aliases

    As of v0.3 you can no specify aliases that point to your WSL2 VM IP. Having some.client.local, may be useful in your development workflow.

    To do this, create the file ~/.wsl2hosts in your default WSL2 distro. Host names are space separated:

    some.client.local my-app.local wsl.local
    

    To upgrade:

    From an elevated / Administrator console

    > .\wsl2host.exe stop
    > .\wsl2host.exe remove
    > .\wsl2host.exe install
    
    Source code(tar.gz)
    Source code(zip)
    wsl2host.exe(3.36 MB)
  • v0.3.0(Nov 1, 2019)

    v0.3.0 Changes

    • Aliases via ~/.wsl2hosts

    Specifying aliases

    As of v0.3 you can no specify aliases that point to your WSL2 VM IP. Having some.client.local, may be useful in your development workflow.

    To do this, create the file ~/.wsl2hosts in your default WSL2 distro. Host names are space separated:

    some.client.local my-app.local wsl.local
    

    To upgrade:

    From an elevated / Administrator console

    > .\wsl2host.exe stop
    > .\wsl2host.exe remove
    > .\wsl2host.exe install
    
    Source code(tar.gz)
    Source code(zip)
    wsl2host.exe(3.36 MB)
  • v0.2.2(Oct 31, 2019)

    v0.2.2 changes

    • Make remove more robust in cleaning up service and eventlog source (clean remove)

    NOTE: Upgrading Windows Insider will remove the service, but not cleanly. To reinstall after upgrading, first make sure you've downloaded the latest version of wsl2host, then run remove before install

    To upgrade:

    From an elevated / Administrator console

    > .\wsl2host.exe stop
    > .\wsl2host.exe remove
    > .\wsl2host.exe install
    
    Source code(tar.gz)
    Source code(zip)
    wsl2host.exe(3.35 MB)
  • v0.2.1(Oct 7, 2019)

    v0.2.1 changes

    • Only update hosts file if change detected

    Complete re-write

    • Changes to use <distro-hostname>.wsl. Distro names are normalized to hostnames, i.e. Ubuntu-18.04 becomes ubuntu1804.wsl.
    • Supports multiple distros at once, each one <distro-hostname>.wsl
    • Removes support for windows.local, if this was important let me know.

    To upgrade:

    From an elevated / Administrator console

    > .\wsl2host.exe stop
    > .\wsl2host.exe remove
    > .\wsl2host.exe install
    
    Source code(tar.gz)
    Source code(zip)
    wsl2host.exe(3.34 MB)
  • v0.2.0(Oct 7, 2019)

    Complete re-write

    • Changes to use <distro-hostname>.wsl. Distro names are normalized to hostnames, i.e. Ubuntu-18.04 becomes ubuntu1804.wsl.
    • Supports multiple distros at once, each one <distro-hostname>.wsl
    • Removes support for windows.local, if this was important let me know.

    To upgrade:

    From an elevated / Administrator console

    > .\wsl2host.exe stop
    > .\wsl2host.exe remove
    > .\wsl2host.exe install
    
    Source code(tar.gz)
    Source code(zip)
    wsl2host.exe(3.34 MB)
  • v0.1.1-alpha(Jul 1, 2019)

    Support multiple IP addresses from hostname -I

    If docker is running multiple IP addresses are returned by hostname -I, we take the first one now.

    Also if we update the IP address and it's shorter (in characters) than the last IP address we have a corrupt wsl line, we now add a couple empty lines to try and mitigate that.

    Source code(tar.gz)
    Source code(zip)
    go-wsl2-host.exe(2.90 MB)
  • v0.1.0-alpha(Jun 30, 2019)

    To install and run, download a binary from the releases tab. Place it somewhere like your Documents/ folder.

    Open an elevated/administrator command prompt:

    > .\go-wsl2-host.exe install
    Windows Username: <username-you-use-to-login-to-windows>
    Windows Password: <password-for-this-user>
    
    Source code(tar.gz)
    Source code(zip)
    go-wsl2-host.exe(2.90 MB)
Project helps to identify the network, broadcast address and no of possible hosts

network_identifier Project helps to identify the network, broadcast address and no of possible hosts for Ipv4 address To use it directly as a go file

Vedant Pareek 0 Dec 3, 2021
Simple utility to set the WSL2 subnet to a specific range

WSL subnet utility This is a small Go utility to set the WSL2 host and subnet. It achieves this by: deleting the existing WSL network creating a new o

Michael Barber 5 Aug 3, 2022
Use DDNS to Update a Cloudflare Spectrum Application's IP Address

Cloudflare Spectrum DDNS NOTICE - PROJECT IS A WORK IN PROGRESS Cloudflare Spectrum's functionality is limited to specifying IP addresses for SSH and

Connor McKelvey 1 Feb 1, 2022
dynflare is a tool to automatically update dns records at Cloudflare, when the ip changes.

dynflare dynflare is a tool to automatically update dns records at Cloudflare, when the ip changes. How it works The current ips are determined by ask

Lukas Dietrich 0 Dec 7, 2021
Simple application in Golang that retrieves your ip and updates your DNS entries automatically each time your IP changes.

DNS-Updater Simple application in Golang that retrieves your ip and updates your DNS entries automatically each time your IP changes. Motivation Havin

42_atomys 9 Mar 10, 2022
Automatically spawn a reverse shell fully interactive for Linux or Windows victim

Girsh (Golang Interactive Reverse SHell) Who didn't get bored of manually typing the few lines to upgrade a reverse shell to a full interactive revers

null 273 Aug 4, 2022
Simple hosts file management in Golang (deprecated).

Goodhosts (deprecated) This library is now deprecated. See the goodhosts organisation for the current maintained version. Simple hosts file (/etc/host

Lex T 69 Mar 17, 2022
Reverse Proxying + Static File Serving + Let's Encrypt + multiple hosts

Slashing This is a HTTPS server, which aims to replace my personal nginx usages. Currently, it serves Reverse Proxying (e.g. to a Python-Flask,Java,PH

Abby 3 Jul 29, 2021
ScriptTiger 17 Aug 7, 2022
Develop, update, and restart your ESP32 applications in less than two seconds

Jaguar Develop, update, and restart your ESP32 applications in less than two seconds. Use the really fast development cycle to iterate quickly and lea

Toit language 51 Aug 3, 2022
Go pkg for returning your public facing IP address.

#publicip This package returns the public facing IP address of the calling client (a la https://icanhazip.com, but from Go!) Author James Polera james

James Polera 25 Jan 23, 2022
⚡ 🖥️ 👾 Host your own Lightning Address on LND

⚡ ??️ ?? Host your own Lightning Address on LND Lighting Wallets like BlueWallet, Blixt and many more allow us to send sats to Lighting Addresses like

Heebs 8 Jun 12, 2022
Gopi - Simple API for get geo information about your IP Address, Build by go-fiber

gopi Simple API to get information from your IP Address Idea This idea come from IP zxq and literaly i clone it How to download GeoIP2 ? Remember to c

I Made Ocy Darma Putra 3 May 27, 2022
Get ip address with Golang on your computer/system

Get IP Address with Golang Get IP address(es) with go-lang is a simple command line tool to get your IP address vpn, internal, external, etc. Usage ge

Murat Oğuz 7 Jun 2, 2022
Automatically compress podcasts to tiny file sizes for bandwidth constrained devices like cellular.

tinycast Automatically compress podcasts to tiny file sizes for bandwidth constrained connections like cellular or satellite.

Stephen Holiday 4 Jan 15, 2022
Run commands on remote hosts, inspecting key indicators to manage infrastructure

inspector This is a very basic ssh helper tool to manage a smaller (few 100s up to a few 1000s) fleet of servers. The main point of inspector is to pr

null 17 Mar 3, 2022
Fetches one or more DNS zones via AXFR and dumps in Unix hosts format for local use

axfr2hosts About axfr2hosts is a tool meant to do a DNS zone transfer in a form of AXFR transaction of one or more zones towards a single DNS server a

Dinko Korunic 8 Aug 9, 2022
Prometheus exporter for ping metrics such as RTT, packet loss, and jitter to any number of hosts.

ping_exporter Command ping_exporter provides a Prometheus exporter for ping metrics such as RTT, packet loss, and jitter to any number of hosts. Usage

Beard Slayer 6 Jun 29, 2022
Quickly find all IPv6 and IPv4 hosts in a LAN.

invaentory Quickly find all IPv6 and IPv4 hosts in a LAN. Overview ?? This project is a work-in-progress! Instructions will be added as soon as it is

Felix Pojtinger 7 May 17, 2022