Ask
23
@wsl_wanda ·

How do I get the Linux subsystem's IP address from the Windows side?

I am running a service inside the Linux subsystem and I want to reach it from Windows and from other machines. The subsystem gets its own address on a virtual network, and that address changes on reboot, so hard-coding it is not an option.

My plan was to have something inside Linux write its address to a file the Windows side can read, and then use it from a script.

Before I build that, is there a direct way to ask for the address from the Windows side?

4 answers Share
Report

Answering anonymously — a moderator will review it first.

  • @wsl_wanda · 5d ago

    There is a direct way and you do not need the file. From the Windows side you can run a command inside the distribution and capture its output, which means any command that reports the address works:

    wsl hostname -I
    

    That starts the default distribution if it is not running and returns its address. It takes a few seconds on a cold start and is instant afterwards.

    If your distribution has several interfaces, hostname -I returns all of them and is ambiguous. In that case be specific:

    wsl ip -4 addr show eth0 | wsl grep -oP '(?<=inet\s)\d+(\.\d+){3}'
    

    or simply run the whole pipeline inside the distribution with a single wsl bash -c '...', which is easier to get right than mixing the two shells.

    29
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @ssh_serkan · 5d ago

    Worth knowing that the networking model here has changed across versions, and advice you find online may describe a different arrangement than the one you have.

    The original design gives the subsystem its own virtual network with an address that changes on each restart, which is the situation you describe. Newer configurations offer a mode where the subsystem shares the host's network directly, and in that mode the whole question disappears — services are reachable as if they were running on Windows.

    If you are fighting the address problem regularly, checking whether your version supports that mode is a better investment than automating around it. It is a configuration setting rather than a reinstall.

    22
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @hyperv_hakan · 4d ago

    Before building anything around the address, check whether you need it at all — because in a lot of cases you do not.

    Reaching a Linux service from Windows. Ports listened to inside the subsystem are generally reachable from Windows at localhost directly. If you are trying to open a web service in a Windows browser, try localhost first; there is a good chance the address hunt is unnecessary.

    Reaching a Windows service from Linux. This is the direction that genuinely needs an address, because localhost inside Linux means Linux. The host's address is available from inside the distribution, and depending on your configuration there is also a name that resolves to it.

    Reaching the Linux service from another machine on the network. This is the hard case, and the address does not help you directly — other machines cannot route to that virtual network. You need a port forward on the Windows host, and the built-in port proxy tool is the usual way to set one up.

    26
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @winadmin_wiktor · 3d ago

    If you do end up scripting it, one detail that will bite: output from a command run through the subsystem often arrives with a trailing newline and sometimes with different line endings than the Windows shell expects.

    Trim it explicitly before using it in a connection string. Half the reports of "the address is correct but the connection fails" come down to an invisible character on the end.

    12
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report