沉冰浮水

沉冰浮水

做最终到的事,成为最终成为的人!
github
bilibili
mastodon
zhihu
douban

"WSL2 Notes" WSL2 Network Configuration and Stuff

Background#

When using WSL2 + Docker to write PHP, you can access it directly using 127.0.0.1[];

But when I installed IIS to write ASP, I found that the services inside WSL2 couldn't be accessed;

Finally, I found out that the real reason was a permission issue caused by a symbolic link used in a file mounted into Docker. Since that file was no longer needed, I deleted it and the issue was resolved;

However, during the process of finding the cause, I followed an article on setting up WSL2 network: "Setting a Static IP Address for WSL2 - Zhihu";

One problem is that the configuration inside WSL2 will be restored after a restart, but the corresponding vEthernet (WSL) on the host machine will not, which means that I have to execute a command to change the internal configuration every time, otherwise the network won't connect...

Issues on Windows 11#

You can use the command Get-NetAdapter 'vEthernet (WSL)' | Get-NetIPAddress to perform the necessary operations, but on Windows 11, this vEthernet (WSL) is hidden, so you need to add a parameter and write it as Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPAddress;

So is there any way to make it display directly?

How to unhide WSL2 vEthernet (WSL) on Windows 11? - Zhihu

https://www.zhihu.com/question/571415099

Exploration#

In the previous tutorial, when configuring the internal of WSL2, the /etc/resolv.conf file will be modified, and it will be restored after a restart. Although it can be set to execute automatically, I found it troublesome, so I didn't do it;

Then I found that the default generated /etc/resolv.conf file contains the following content:

# 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.80.1

It turns out that this file can be controlled by the /etc/wsl.conf file...

Actually, the current requirement is to restore the network connectivity inside WSL2 and keep it that way. Whether the IP is fixed or not doesn't matter, especially since I don't understand how to configure vEthernet (WSL) through the command line;

# Configure the network inside WSL2 and write the configuration file
# Setting a Static IP Address for WSL2 - Zhihu
# https://zhuanlan.zhihu.com/p/380779630
sudo ip addr del $(ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | head -n 1) dev eth0
sudo ip addr add 192.168.50.2/24 broadcast 192.168.50.255 dev eth0
sudo ip route add 0.0.0.0/0 via 192.168.50.1 dev eth0
sudo echo nameserver 192.168.50.1 > /etc/resolv.conf

# Write the configuration to /etc/wsl.conf
# sudo echo -e "[user]" > /etc/wsl.conf
# sudo echo -e "default = wdssmq" >> /etc/wsl.conf
sudo echo -e "[network]" >> /etc/wsl.conf
sudo echo -e "generateResolvConf = false" >> /etc/wsl.conf

In theory, the above commands should only need to be executed once...

Then execute the following commands in powershell to set a fixed IP for vEthernet (WSL):

# Setting a Static IP Address for WSL2 - Zhihu
# https://zhuanlan.zhihu.com/p/380779630
Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$False
New-NetIPAddress -IPAddress 192.168.50.1 -PrefixLength 24 -InterfaceAlias 'vEthernet (WSL)'
Get-NetNat | ? Name -Eq WSLNat | Remove-NetNat -Confirm:$False
New-NetNat -Name WSLNat -InternalIPInterfaceAddressPrefix 192.168.50.0/24;

Testing the Internal Network of WSL2#

# Test network connectivity
ping -c 1 baidu.com

# View IP
ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | head -n 1
# 192.168.50.2/24

# View routes
ip route show
# default via 192.168.50.1 dev eth0
# 192.168.50.0/24 dev eth0 proto kernel scope link src 192.168.50.2

Notes#

Various commands that were found and tried during the process are not well understood, so I'll keep a record for now:

Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPAddress

Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPConfiguration

Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPInterface

Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPInterface -Dhcp -- Enabled

Get-NetAdapter -IncludeHidden 'vEthernet (WSL)' | Get-NetIPInterface -AddressFamily "IPv4"


# ----------------------

# Using PowerShell to Set Static and DHCP IP Addresses – Part 1 | PDQ
# https://www.pdq.com/blog/using-powershell-to-set-static-and-dhcp-ip-addresses-part-1/

$IPType = "IPv4"
$adapter = Get-NetAdapter -IncludeHidden 'vEthernet (WSL)'
$interface = $adapter | Get-NetIPInterface -AddressFamily $IPType
If ($interface.Dhcp -eq "Disabled") {
    # Remove existing gateway
    If (($interface | Get-NetIPConfiguration).Ipv4DefaultGateway) {
    $interface | Remove-NetRoute -Confirm:$false
    }
    # Enable DHCP
    $interface | Set-NetIPInterface -DHCP Enabled
    # Configure the DNS Servers automatically
    $interface | Set-DnsClientServerAddress -ResetServerAddresses
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.