Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensively clarify "Identify IP address" section. #1978

Merged
merged 4 commits into from
Jul 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions WSL/basic-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,44 @@ To terminate the specified distribution, or stop it from running, replace `<Dist

## Identify IP address

- `wsl hostname -i` for the IP address of your Linux distribution installed via WSL 2 (the WSL 2 VM address)
mattwojo marked this conversation as resolved.
Show resolved Hide resolved
- `cat /etc/resolv.conf` for the IP address of the Windows machine as seen from WSL 2 (the WSL 2 VM)
mattwojo marked this conversation as resolved.
Show resolved Hide resolved
There are two scenarios involved.

Scenario One: From the perspective of the Windows host, you want to query a WSL2 distribution's IP address, so that a program on Windows host can connect to a server program running inside the WSL distribution(instance).

The Windows host can use command:

```
wsl -d <DistributionName> hostname -I
```

If we are querying the default distribution, we can omit `-d <DistributionName>` option.

Under the hood, host command `wsl.exe` launches the target instance right away, and tells it to execute Linux command `hostname -I` (which prints WSL instance's own IP to STDOUT) then the STDOUT text content is relayed back to wsl.exe, and finally wsl.exe prints that out to us.


A typical output can be:

```
172.30.98.229
```

Scenario Two: A program running inside WSL2 instance wants to know the Windows host's IP address, so that a Linux program can connect to a Windows host server program.

The WSL2 Linux user can use command `cat /etc/resolv.conf`, which outputs something like:

```
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.30.96.1
```

So the `172.30.96.1` is the Window host's IP address, in this example.

> [!NOTE]
> These above IP address querying action is typically required when WSL2 is running with [NAT network mode](./networking.md#default-networking-mode-nat).
> When the WSL2 is running with new [mirrored mode](./networking.md#mirrored-mode-networking), the Windows host and WSL2 VM can connect to each other using `localhost` (127.0.0.1) as destination address, so the trick of query peer's IP address is not required.


## Export a distribution

Expand Down