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
Prev Previous commit
Next Next commit
Simplify commands and link to Networking page
  • Loading branch information
mattwojo authored Jul 16, 2024
commit 36fbd494f2119961897afd2425595140019907a4
42 changes: 4 additions & 38 deletions WSL/basic-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ For example:
wsl --shutdown
```

Immediately terminates all running distributions and the WSL 2 lightweight utility virtual machine. This command may be necessary in instances that require you to restart the WSL 2 virtual machine environment, such as [changing memory usage limits](/windows/wsl/disk-space) or making a change to your [.wslconfig file](./manage.md#).
Immediately terminates all running distributions and the WSL 2 lightweight utility virtual machine. This command may be necessary in instances that require you to restart the WSL 2 virtual machine environment, such as [changing memory usage limits](/windows/wsl/disk-space) or making a change to your [.wslconfig file](./manage.md).

## Terminate

Expand All @@ -168,44 +168,10 @@ To terminate the specified distribution, or stop it from running, replace `<Dist

## Identify IP address

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.
- `wsl hostname -I`: Returns the IP address of your Linux distribution installed via WSL 2 (the WSL 2 VM address)
- `ip route show | grep -i default | awk '{ print $3}'`: Returns teh IP address of the Windows machine as seen from WSL 2 (the WSL 2 VM)

For a more detailed explanation, see [Accessing network applications with WSL: Identify IP Address](./networking.md##identify-ip-address).

## Export a distribution

Expand Down