wls2 与宿主机的ip不在同一网段的问题

wls2 与宿主机的ip不在同一网段的问题
总结:1、当wsl2的ip为172开头的网段,宿主机为192开头的时候。主机可以访问wsl2,wls2由于防火墙不能访问宿主机的ip

2、当上面的ip不是同一网段在局域网时候,wls2(172)不能之间连同另外一台主机的wls2(172开头)。但是另外一台主机的wls2ip也是192开头,则可以单向连同。所以综上所述,只有wls2的ip跟主机的ip都在同一网段,才能互相连通。

!!!!!!wsl1的ip跟window主机的ip是一样的,所以才在局域网内连接没有问题,所以下面这种情况是可以局域网内互相访问的!wls2必须设置成这样才可以

C:\Users\lilili>ipconfig.exe

Windows IP 配置

以太网适配器 以太网:

连接特定的 DNS 后缀 . . . . . . . :
本地链接 IPv6 地址. . . . . . . . : fe80::59:7ce7:ba19:f24a%9
IPv4 地址 . . . . . . . . . . . . : 192.168.106.236
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . : 192.168.106.1

无线局域网适配器 本地连接* 3:

媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :

以太网适配器 以太网 2:

媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :

C:\Users\lilili>
(base) yypli@DESKTOP-A37DDP7:~$ ifconfig
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.106.236 netmask 255.255.255.0 broadcast 192.168.106.255
inet6 fe80:a19:f24a prefixlen 64 scopeid 0xfd<compat,link,site,host>

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

sudo ifconfig eth0 172.20.121.18 netmask 255.255.240.0

C:\Users\admin>ipconfig.exe

Windows IP Configuration

Ethernet adapter 以太网:

Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::fdea:a84e:638b:91ab%14
IPv4 Address. . . . . . . . . . . : 192.168.106.91
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.106.1

Ethernet adapter Npcap Loopback Adapter:

Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::ed9d:b144:c7e9:49f0%10
Autoconfiguration IPv4 Address. . : 169.254.73.240
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :

Ethernet adapter 以太网 2:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :

Ethernet adapter vEthernet (WSL):

Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::69ee:d485:eea4:1c2b%31
IPv4 Address. . . . . . . . . . . : 172.20.112.1
Subnet Mask . . . . . . . . . . . : 255.255.240.0
Default Gateway . . . . . . . . . :

C:\Users\admin>ping 172.20.121.18

Pinging 172.20.121.18 with 32 bytes of data:
Reply from 172.20.121.18: bytes=32 time=17ms TTL=64
Reply from 172.20.121.18: bytes=32 time<1ms TTL=64
Reply from 172.20.121.18: bytes=32 time<1ms TTL=64
Reply from 172.20.121.18: bytes=32 time<1ms TTL=64

(base) yyp@DESKTOP-LRUCLJJ:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.121.18 netmask 255.255.240.0 broadcast 172.20.127.255
inet6 fe80::215:5dff:fe04:d3ba prefixlen 64 scopeid 0x20
ether 00:15:5d:04:d3:ba txqueuelen 1000 (Ethernet)
RX packets 8144 bytes 1224992 (1.2 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 669 bytes 62554 (62.5 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

(base) yyp@DESKTOP-LRUCLJJ:~$ ping 172.20.112.1
PING 172.20.112.1 (172.20.112.1) 56(84) bytes of data.
^C
— 172.20.112.1 ping statistics —
6 packets transmitted, 0 received, 100% packet loss, time 5206ms

(base) yyp@DESKTOP-LRUCLJJ:~$ sudo ifconfig eth0 172.20.121.18 netmask 255.255.240.0 【主要是240这里 ip不变】
[sudo] password for yyp:
(base) yyp@DESKTOP-LRUCLJJ:~$ ping 172.20.112.1
PING 172.20.112.1 (172.20.112.1) 56(84) bytes of data.
^C
— 172.20.112.1 ping statistics —
8 packets transmitted, 0 received, 100% packet loss, time 7283ms

#########wsl2 ping不通主机,关闭防火墙后可以
(base) yyp@DESKTOP-LRUCLJJ:~$ ping 172.20.112.1
PING 172.20.112.1 (172.20.112.1) 56(84) bytes of data.
64 bytes from 172.20.112.1: icmp_seq=1 ttl=64 time=0.401 ms
64 bytes from 172.20.112.1: icmp_seq=2 ttl=64 time=0.221 ms
64 bytes from 172.20.112.1: icmp_seq=3 ttl=64 time=0.301 ms
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#####修改入站规则后,打开防火墙也可以
(base) yyp@DESKTOP-LRUCLJJ:~$ ping 172.20.112.1
PING 172.20.112.1 (172.20.112.1) 56(84) bytes of data.
64 bytes from 172.20.112.1: icmp_seq=1 ttl=64 time=0.280 ms
64 bytes from 172.20.112.1: icmp_seq=2 ttl=64 time=0.245 ms
64 bytes from 172.20.112.1: icmp_seq=3 ttl=64 time=0.233 ms
64 bytes from 172.20.112.1: icmp_seq=4 ttl=64 time=0.652 ms
^C
— 172.20.112.1 ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 3155ms
rtt min/avg/max/mdev = 0.233/0.352/0.652/0.173 ms

https://www.jianshu.com/p/ba2cf239ebe0
WSL2 网络异常排查 [ping 不通、网络地址异常、缺少默认路由、被宿主机防火墙拦截]

实现10.0.0.0/24【172.20.121.18 】网段与192.168.2.0/24【192.168.106.236】网段相互访问.帮你明白这一个问题解决方式。

(base) yyp@DESKTOP-LRUCLJJ:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.121.18 netmask 255.255.240.0 broadcast 172.20.127.255

route add -net 192.168.106.0/24 eth0

(base) yyp@DESKTOP-LRUCLJJ:~$ sudo route add -net 192.168.106.0/24 eth0
(base) yyp@DESKTOP-LRUCLJJ:~$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default DESKTOP-LRUCLJJ 0.0.0.0 UG 0 0 0 eth0
172.20.112.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0
192.168.106.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
(base) yyp@DESKTOP-LRUCLJJ:~$
注:route规则中,必须同时存在两个网段,否则,一个网段的IP地址将无法访问到另一网段的IP地址。

ping 192.168.106.236

(base) yyp@DESKTOP-LRUCLJJ:~$ ping 192.168.106.236
PING 192.168.106.236 (192.168.106.236) 56(84) bytes of data.

From 172.20.121.18 icmp_seq=1 Destination Host Unreachable
From 172.20.121.18 icmp_seq=2 Destination Host Unreachable
From 172.20.121.18 icmp_seq=3 Destination Host Unreachable
From 172.20.121.18 icmp_seq=4 Destination Host Unreachable
From 172.20.121.18 icmp_seq=5 Destination Host Unreachable
(base) yyp@DESKTOP-LRUCLJJ:~$ ssh yy00@192.168.106.236
ssh: connect to host 192.168.106.236 port 22: No route to host
route del -net 192.168.106.0/24 eth0
(base) yyp@DESKTOP-LRUCLJJ:~$ sudo route del -net 192.168.106.0/24 eth0
(base) yyp@DESKTOP-LRUCLJJ:~$ ssh yyp00@192.168.106.236
yyp00@192.168.106.236’s password:
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.4.0-19041-Microsoft x86_64)

(base) yypli@DESKTOP-A37DDP7:~$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.106.1 255.255.255.255 U 0 0 0 eth1
192.168.106.0 0.0.0.0 255.255.255.0 U 256 0 0 eth1
192.168.106.236 0.0.0.0 255.255.255.255 U 256 0 0 eth1
192.168.106.255 0.0.0.0 255.255.255.255 U 256 0 0 eth1
224.0.0.0 0.0.0.0 240.0.0.0 U 256 0 0 eth1
255.255.255.255 0.0.0.0 255.255.255.255 U 256 0 0 eth1
127.0.0.0 0.0.0.0 255.0.0.0 U 256 0 0 lo
127.0.0.1 0.0.0.0 255.255.255.255 U 256 0 0 lo
127.255.255.255 0.0.0.0 255.255.255.255 U 256 0 0 lo
224.0.0.0 0.0.0.0 240.0.0.0 U 256 0 0 lo
255.255.255.255 0.0.0.0 255.255.255.255 U 256 0 0 lo
(base) yypli@DESKTOP-A37DDP7:~$ exit
logout
Connection to 192.168.106.236 closed.
(base) yyp@DESKTOP-LRUCLJJ:~$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default DESKTOP-LRUCLJJ 0.0.0.0 UG 0 0 0 eth0
172.20.112.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0【宿主机】
(base) yyp@DESKTOP-LRUCLJJ:~$

总结:1、当wsl2的ip为172开头的网段,宿主机为192开头的时候。主机可以访问wsl2,wls2由于防火墙不能访问宿主机的ip

2、当上面的ip不是同一网段在局域网时候,wls2(172)不能之间连同另外一台主机的wls2(172开头)。但是另外一台主机的wls2ip也是192开头,则可以单向连同。所以综上所述,只有wls2的ip跟主机的ip都在同一网段,才能互相连通。

#! /bin/bash

BEGIN INIT INFO

Provides: hybors.com

Required-Start: $all

Required-Stop: $all

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: Output the IP of wsl2 to the host

Description: When wsl2 starts, output the IP address to the hosts file of the host, the entry

format is “xxx.xxx.xxx.xxx wsl2-distro-name.wsl”, and delete the entries that no longer exist

END INIT INFO

Enter the path where the hosts file is located

cd /mnt/c/Windows/System32/drivers/etc/

check hosts permission

ls -l hosts | grep “^-rw.rw.rw.”
result=$?
if [ ${result} == 1 ]
then
echo “[error] Please add read and write permissions to hosts for the current user in the Windows system !”
exit 1
fi

check package “iproute2”

apt list iproute2 | grep “installed”
result=$?
if [ ${result} == 1 ]
then
apt update
apt install -y iproute2
fi

get wsl distro’s name and ip

WSL_DISTRO_NAME is a built-in variable

host_name=" W S L D I S T R O N A M E . w s l " i p = {WSL_DISTRO_NAME}.wsl" ip= WSLDISTRONAME.wsl"ip=(ip a show eth0 | grep "inet " | awk ‘{print $2}’ | awk -F “/” ‘{print $1}’)

check hosts’s content

cat hosts | grep h o s t n a m e r e s u l t = {host_name} result= hostnameresult=?
if [ ${result} == 1 ] # add a new record
then
echo ${ip} KaTeX parse error: Expected 'EOF', got '#' at position 27: … >> hosts else #̲ modify an exis…{host_name}/${ip} ${host_name}/" > hosts
fi

get all wsl distro names

result=$(wsl.exe -l -q)

get all host-names ending in “wsl”

for i in sed -r -n "s/^[0-9.]+\s//p" hosts | sed -r -n "s/\.wsl$//p"
do

If the host-name is not in the existing distros, delete

if ! [[ “ r e s u l t " =   " {result}" =~ " result"= "i” ]]
then
cat hosts | sed -r “/$i/d” > hosts
fi
done
exit 0

https://blog.csdn.net/fleaxin/article/details/110877622?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162277026016780271542381%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162277026016780271542381&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-1-110877622.first_rank_v2_pc_rank_v29&utm_term=%E8%AE%BE%E7%BD%AEwls2%E7%9A%84ip%E4%B8%8E%E4%B8%BB%E6%9C%BAip%E4%B8%80%E6%A0%B7&spm=1018.2226.3001.4187

(base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc$ apt list iproute2
Listing… Done
iproute2/focal,now 5.5.0-1ubuntu1 amd64 [installed]
(base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc$ apt list iproute2 | grep “installed”

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

iproute2/focal,now 5.5.0-1ubuntu1 amd64 [installed]
(base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc$ result= ? ( b a s e ) y y p @ D E S K T O P − L R U C L J J : / m n t / c / W i n d o w s / S y s t e m 32 / d r i v e r s / e t c ? (base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc ?(base)yyp@DESKTOPLRUCLJJ:/mnt/c/Windows/System32/drivers/etc echo r e s u l t 0 ( b a s e ) y y p @ D E S K T O P − L R U C L J J : / m n t / c / W i n d o w s / S y s t e m 32 / d r i v e r s / e t c result 0 (base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc result0(base)yyp@DESKTOPLRUCLJJ:/mnt/c/Windows/System32/drivers/etc host_name=" W S L D I S T R O N A M E . w s l " ( b a s e ) y y p @ D E S K T O P − L R U C L J J : / m n t / c / W i n d o w s / S y s t e m 32 / d r i v e r s / e t c {WSL_DISTRO_NAME}.wsl" (base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc WSLDISTRONAME.wsl"(base)yyp@DESKTOPLRUCLJJ:/mnt/c/Windows/System32/drivers/etc echo h o s t n a m e U b u n t u − 20.04. w s l ( b a s e ) y y p @ D E S K T O P − L R U C L J J : / m n t / c / W i n d o w s / S y s t e m 32 / d r i v e r s / e t c host_name Ubuntu-20.04.wsl (base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc hostnameUbuntu20.04.wsl(base)yyp@DESKTOPLRUCLJJ:/mnt/c/Windows/System32/drivers/etc ip=$(ip a show eth0 | grep "inet " | awk ‘{print $2}’ | awk -F “/” '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲') (base) yyp@D… echo i p 172.20.121.18 ( b a s e ) y y p @ D E S K T O P − L R U C L J J : / m n t / c / W i n d o w s / S y s t e m 32 / d r i v e r s / e t c ip 172.20.121.18 (base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc ip172.20.121.18(base)yyp@DESKTOPLRUCLJJ:/mnt/c/Windows/System32/drivers/etc
(base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc$
(base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc$ cat hosts | grep h o s t n a m e t = {host_name} t= hostnamet=?(base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc$ result= ? ( b a s e ) y y p @ D E S K T O P − L R U C L J J : / m n t / c / W i n d o w s / S y s t e m 32 / d r i v e r s / e t c ? (base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc ?(base)yyp@DESKTOPLRUCLJJ:/mnt/c/Windows/System32/drivers/etc echo r e s u l t 1 ( b a s e ) y y p @ D E S K T O P − L R U C L J J : / m n t / c / W i n d o w s / S y s t e m 32 / d r i v e r s / e t c result 1 (base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc result1(base)yyp@DESKTOPLRUCLJJ:/mnt/c/Windows/System32/drivers/etc cat hosts
# Copyright © 1993-2009 Microsoft Corp.

This is a sample HOSTS file used by Microsoft TCP/IP for Windows.

This file contains the mappings of IP addresses to host names. Each

entry should be kept on an individual line. The IP address should

be placed in the first column followed by the corresponding host name.

The IP address and the host name should be separated by at least one

space.

Additionally, comments (such as these) may be inserted on individual

lines or following the machine name denoted by a ‘#’ symbol.

For example:

102.54.94.97 rhino.acme.com # source server

38.25.63.10 x.acme.com # x client host

localhost name resolution is handled within DNS itself.

127.0.0.1 localhost

::1 localhost

Added by Docker Desktop

192.168.106.91 host.docker.internal
192.168.106.91 gateway.docker.internal

To allow the same kube context to work on the host and the container:

127.0.0.1 kubernetes.docker.internal

End of section

(base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc$ echo ${ip} h o s t n a m e > > h o s t s ( b a s e ) y y p @ D E S K T O P − L R U C L J J : / m n t / c / W i n d o w s / S y s t e m 32 / d r i v e r s / e t c {host_name} >> hosts (base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc hostname>>hosts(base)yyp@DESKTOPLRUCLJJ:/mnt/c/Windows/System32/drivers/etc cat hosts
# Copyright © 1993-2009 Microsoft Corp.

This is a sample HOSTS file used by Microsoft TCP/IP for Windows.

This file contains the mappings of IP addresses to host names. Each

entry should be kept on an individual line. The IP address should

be placed in the first column followed by the corresponding host name.

The IP address and the host name should be separated by at least one

space.

Additionally, comments (such as these) may be inserted on individual

lines or following the machine name denoted by a ‘#’ symbol.

For example:

102.54.94.97 rhino.acme.com # source server

38.25.63.10 x.acme.com # x client host

localhost name resolution is handled within DNS itself.

127.0.0.1 localhost

::1 localhost

Added by Docker Desktop

192.168.106.91 host.docker.internal
192.168.106.91 gateway.docker.internal

To allow the same kube context to work on the host and the container:

127.0.0.1 kubernetes.docker.internal

End of section

172.20.121.18 Ubuntu-20.04.wsl
(base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc$ cat hosts | sed -r “s/1+\s+ h o s t n a m e / {host_name}/ hostname/{ip} ${host_name}/”
# Copyright © 1993-2009 Microsoft Corp.

This is a sample HOSTS file used by Microsoft TCP/IP for Windows.

This file contains the mappings of IP addresses to host names. Each

entry should be kept on an individual line. The IP address should

be placed in the first column followed by the corresponding host name.

The IP address and the host name should be separated by at least one

space.

Additionally, comments (such as these) may be inserted on individual

lines or following the machine name denoted by a ‘#’ symbol.

For example:

102.54.94.97 rhino.acme.com # source server

38.25.63.10 x.acme.com # x client host

localhost name resolution is handled within DNS itself.

127.0.0.1 localhost

::1 localhost

Added by Docker Desktop

192.168.106.91 host.docker.internal
192.168.106.91 gateway.docker.internal

To allow the same kube context to work on the host and the container:

127.0.0.1 kubernetes.docker.internal

End of section

172.20.121.18 Ubuntu-20.04.wsl
(base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc$ result= ( w s l . e x e − l − q ) − b a s h : w a r n i n g : c o m m a n d s u b s t i t u t i o n : i g n o r e d n u l l b y t e i n i n p u t ( b a s e ) y y p @ D E S K T O P − L R U C L J J : / m n t / c / W i n d o w s / S y s t e m 32 / d r i v e r s / e t c (wsl.exe -l -q) -bash: warning: command substitution: ignored null byte in input (base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc (wsl.exelq)bash:warning:commandsubstitution:ignorednullbyteininput(base)yyp@DESKTOPLRUCLJJ:/mnt/c/Windows/System32/drivers/etc echo r e s u l t d o c k e r − d e s k t o p − d a t a ( b a s e ) y y p @ D E S K T O P − L R U C L J J : / m n t / c / W i n d o w s / S y s t e m 32 / d r i v e r s / e t c result docker-desktop-data (base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc resultdockerdesktopdata(base)yyp@DESKTOPLRUCLJJ:/mnt/c/Windows/System32/drivers/etc for i in sed -r -n "s/^[0-9.]+\s//p" hosts | sed -r -n "s/\.wsl$//p"
o> do

echo i d o n e U b u n t u − 20.04 ( b a s e ) y y p @ D E S K T O P − L R U C L J J : / m n t / c / W i n d o w s / S y s t e m 32 / d r i v e r s / e t c i done Ubuntu-20.04 (base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc idoneUbuntu20.04(base)yyp@DESKTOPLRUCLJJ:/mnt/c/Windows/System32/drivers/etc ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.20.121.18 netmask 255.255.240.0 broadcast 172.20.127.255
inet6 fe80::215:5dff:fe04:d3ba prefixlen 64 scopeid 0x20
ether 00:15:5d:04:d3:ba txqueuelen 1000 (Ethernet)
RX packets 13700 bytes 2229157 (2.2 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 966 bytes 87676 (87.6 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 16 bytes 1720 (1.7 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 16 bytes 1720 (1.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

(base) yyp@DESKTOP-LRUCLJJ:/mnt/c/Windows/System32/drivers/etc$

192.168.106.91
C:\Users\admin>ipconfig

Windows IP Configuration

Ethernet adapter 以太网:

Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::fdea:a84e:638b:91ab%14
IPv4 Address. . . . . . . . . . . : 192.168.106.91
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.106.1
host_name="${WSL_DISTRO_NAME}.wsl"

@echo off
setlocal enabledelayedexpansion

wsl -u root service docker start | findstr “Starting Docker” > nul
if !errorlevel! equ 0 (
echo docker start success
:: set wsl2 ip
wsl -u root ip addr | findstr “192.168.169.2” > nul
if !errorlevel! equ 0 (
echo wsl ip has set
) else (
wsl -u root ip addr add 192.168.169.2/28 broadcast 192.168.169.15 dev eth0 label eth0:1
echo set wsl ip success: 192.168.169.2
)

:: set windows ip
ipconfig | findstr "192.168.169.1" > nul
if !errorlevel! equ 0 (
    echo windows ip has set
) else (
    netsh interface ip add address "vEthernet (WSL)" 192.168.169.1 255.255.255.240
    echo set windows ip success: 192.168.169.1
)

)
pause


  1. 0-9. ↩︎

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值