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

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
Comments
  • 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?

  • 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

  • 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?

  • 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!

  • 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.

  • 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.

  • 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

  • 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.

  • 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.

  • 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.

  • 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.

  • Fails to start due to invalid memory address or nil pointer dereference

    Fails to start due to invalid memory address or nil pointer dereference

    I've been using wsl2host for a few months now and it worked fine until I noticed it was acting weird today. Multiple times today I've had issues where a domain isn't pointing to an ip. While checking the event viewer I noticed a lot of errors. I assumed this might have to do with an update so I ran build.bat on the latest commit and used that executable to try again. Unfortunately without any luck. Below the output of 3 separate attempts at running wsl2host debug.

    C:\Users\Slice\Documents\wsl2host>wsl2host.exe debug
    wsl2host.info(1): starting wsl2host service
    panic: runtime error: invalid memory address or nil pointer dereference
    [signal 0xc0000005 code=0x0 addr=0x0 pc=0xfcccd7]
    
    goroutine 1 [running]:
    github.com/shayne/go-wsl2-host/cmd/wsl2host/pkg/service.updateHostIP({0x102fc68, 0xc000040640}, {0xc0001c0038, 0x1, 0xc00007bc58?})
            C:/Users/Slice/Documents/wsl2host/git/go-wsl2-host/cmd/wsl2host/pkg/service/service.go:109 +0x437
    github.com/shayne/go-wsl2-host/cmd/wsl2host/pkg/service.Run({0x102fc68, 0xc000040640})
            C:/Users/Slice/Documents/wsl2host/git/go-wsl2-host/cmd/wsl2host/pkg/service/service.go:40 +0x76
    github.com/shayne/go-wsl2-host/cmd/wsl2host/internal.(*windowserver).Execute(0xc0000521e0?, {0xc000040660, 0x1, 0x1}, 0xc000052120, 0x4?)
            C:/Users/Slice/Documents/wsl2host/git/go-wsl2-host/cmd/wsl2host/internal/service.go:34 +0x2f8
    golang.org/x/sys/windows/svc/debug.Run({0xffb213, 0x8}, {0x102f628, 0x113e5b8})
            C:/Users/Slice/go/pkg/mod/golang.org/x/[email protected]/windows/svc/debug/service.go:40 +0x144
    github.com/shayne/go-wsl2-host/cmd/wsl2host/internal.RunService({0xffb213, 0x8}, 0x1)
            C:/Users/Slice/Documents/wsl2host/git/go-wsl2-host/cmd/wsl2host/internal/service.go:82 +0x210
    main.main()
            C:/Users/Slice/Documents/wsl2host/git/go-wsl2-host/cmd/wsl2host/main.go:42 +0x14e
    
    C:\Users\Slice\Documents\wsl2host>wsl2host.exe debug
    wsl2host.info(1): starting wsl2host service
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    wsl2host.info(1): wsl2host-0
    wsl2host.info(1): wsl2host service stopped
    
    C:\Users\Slice\Documents\wsl2host>wsl2host.exe debug
    wsl2host.info(1): starting wsl2host service
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    wsl2host.error(1): failed to get infos: failed to get IP for distro "Ubuntu": exit status 1
    panic: runtime error: invalid memory address or nil pointer dereference
    [signal 0xc0000005 code=0x0 addr=0x0 pc=0xfcccd7]
    
    goroutine 1 [running]:
    github.com/shayne/go-wsl2-host/cmd/wsl2host/pkg/service.updateHostIP({0x102fc68, 0xc000040640}, {0xc00000a070, 0x1, 0xffa39a?})
            C:/Users/Slice/Documents/wsl2host/git/go-wsl2-host/cmd/wsl2host/pkg/service/service.go:109 +0x437
    github.com/shayne/go-wsl2-host/cmd/wsl2host/pkg/service.Run({0x102fc68, 0xc000040640})
            C:/Users/Slice/Documents/wsl2host/git/go-wsl2-host/cmd/wsl2host/pkg/service/service.go:40 +0x76
    github.com/shayne/go-wsl2-host/cmd/wsl2host/internal.(*windowserver).Execute(0xc0000521e0?, {0xc000040660, 0x1, 0x1}, 0xc000052120, 0x4?)
            C:/Users/Slice/Documents/wsl2host/git/go-wsl2-host/cmd/wsl2host/internal/service.go:34 +0x2f8
    golang.org/x/sys/windows/svc/debug.Run({0xffb213, 0x8}, {0x102f628, 0x113e5b8})
            C:/Users/Slice/go/pkg/mod/golang.org/x/[email protected]/windows/svc/debug/service.go:40 +0x144
    github.com/shayne/go-wsl2-host/cmd/wsl2host/internal.RunService({0xffb213, 0x8}, 0x1)
            C:/Users/Slice/Documents/wsl2host/git/go-wsl2-host/cmd/wsl2host/internal/service.go:82 +0x210
    main.main()
            C:/Users/Slice/Documents/wsl2host/git/go-wsl2-host/cmd/wsl2host/main.go:42 +0x14e
    
    C:\Users\Slice\Documents\wsl2host>
    
  • Doesn't work well as service.

    Doesn't work well as service.

    It only works on the command line wsl2host debug, not in the service. I tried versions 0.3.5 and 0.2.1 and they both had the same problem.

    WSL version: 0.70.5.0 core version: 5.15.68.1 WSLg version: 1.0.45 MSRDC version: 1.2.3575 Direct3D version: 1.606.4 DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp Windows version: 10.0.22621.755

  • 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

Related tags
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

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

Oct 22, 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

Sep 15, 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

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

Mar 10, 2022
Simple hosts file management in Golang (deprecated).
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

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

Jul 29, 2021
Sep 23, 2022
Automatically spawn a reverse shell fully interactive for Linux or Windows victim
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

Dec 14, 2022
Develop, update, and restart your ESP32 applications in less than two seconds
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

Jan 8, 2023
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

Nov 21, 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

Dec 22, 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

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

Sep 5, 2022
Automatically compress podcasts to tiny file sizes for bandwidth constrained devices like cellular.
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.

Sep 18, 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

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

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

Sep 24, 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

May 17, 2022