win10 系统带linux子系统有两个版本
第一个是wsl, 它与windows 系统公用同1个ip地址, 但是没有自己内核, 不支持docker
第二个版本是wsl2, 它可以使用docker,但是它的网卡每次启动都随机使用ip, 所以重启后每次都必须手动进行ip 转发。
例如:
netsh interface portproxy reset
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=22 connectaddress=192.168.44.155 connectport=22
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8091 connectaddress=192.168.44.155 connectport=8091
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=3351 connectaddress=192.168.44.155 connectport=3351
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=3352 connectaddress=192.168.44.155 connectport=3352
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=3353 connectaddress=192.168.44.155 connectport=3353
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=3354 connectaddress=192.168.44.155 connectport=3354
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=3355 connectaddress=192.168.44.155 connectport=3355
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=3356 connectaddress=192.168.44.155 connectport=3356
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8888 connectaddress=192.168.44.155 connectport=8888
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8080 connectaddress=192.168.44.155 connectport=8080
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8081 connectaddress=192.168.44.155 connectport=8081
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8761 connectaddress=192.168.44.155 connectport=8761
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8890 connectaddress=192.168.44.155 connectport=8890
netsh interface portproxy show all
但由于微软系统的不稳定性, 经常无故重启, 大部分都是更新引起的, 禁了更新一两周内仍会自己重启, 好智障。
一旦重启后,由于wsl的子系统ip地址变了, 所有的服务都连不上…
痛定思楚, 便有了下面的解决办法。
1. 编写bash 脚本获取子系统ip
由于安装了docker 的关系, 简单的hostname -l 会返回两个ip, 而我们只想要eth0 网卡那个。
gateman@DESKTOP-UIU9RFJ:~$ hostname -I
192.168.77.113 172.17.0.1
但是在上一级语言去 call linux管道命令是很麻烦的, 所以编写了脚本getip 去获取ip
gateman@DESKTOP-UIU9RFJ:~$ cat /usr/bin/getip
ifconfig eth0 | head -n2 | grep inet | awk '{print$2}'
gateman@DESKTOP-UIU9RFJ:~$ getip
192.168.77.113
gateman@DESKTOP-UIU9RFJ:~$
这个就是我们想要的.
2. 编写vbs脚本进行自动ip转发
在下面的目录创建1个posts.vbs文件
C:\Users\xxxxxxx\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
内容
只有3个地方有可能要修改
1是 第一行wsl2 name
2是 要转发的端口list
3是 存放ip地址的临时文件位置
wslname = "Ubuntu" 'the name of your wls system, could be checked by command wsl -l
Dim ports
'the ports which need to process ip forward
ports = Array(22,8080,8091,3351,3352,3353,3354,3355,3356,8888,8081,8761,8890)
'the file to store wls ip
ipTemp = "C:\Temp\wslip.txt"
If WScript.Arguments.Length = 0 Then
'to get the admin right to run the ip forward commands
CreateObject("Shell.Application").ShellExecute "wscript.exe" _
, """" & WScript.ScriptFullName & """ RunAsAdministrator", , "runas", 1
WScript.Quit
End If
Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
'to get the eth0 ip address of the sub linux system, the script getip is in /usr/bin, and save the ipadress to C:\Temp\wslip.txt
writeip = objShell.Run("cmd /c ""wsl -d " + wslname + " -u root getip"">" + ipTemp,0,True)
'wscript.sleep(1000)'just incase some network problem
'get the ip from ip file
Set f = fso.OpenTextFile(ipTemp, 1)
wslip = f.ReadAll()
f.Close()
'execute the ip forward command for each port number from the list
For i = 0 To UBound(ports)
port = ports(i)
command = "cmd /c ""netsh interface portproxy add v4tov4 listenport=" & port & " listenaddress=0.0.0.0 connectport=" & port & " connectaddress=" + wslip
forwarding = objShell.Run(command,0,True)
Next
重启后
用powershell执行下面命令就可以看到自动ip转发是否成功
PS C:\Users\gateman> netsh interface portproxy show all
侦听 ipv4: 连接到 ipv4:
地址 端口 地址 端口
--------------- ---------- --------------- ----------
0.0.0.0 22 192.168.77.113 22
0.0.0.0 8091 192.168.77.113 8091
0.0.0.0 3351 192.168.77.113 3351
0.0.0.0 3352 192.168.77.113 3352
0.0.0.0 3353 192.168.77.113 3353
0.0.0.0 3354 192.168.77.113 3354
0.0.0.0 3355 192.168.77.113 3355
0.0.0.0 3356 192.168.77.113 3356
0.0.0.0 8888 192.168.77.113 8888
0.0.0.0 8080 192.168.77.113 8080
0.0.0.0 8081 192.168.77.113 8081
0.0.0.0 8761 192.168.77.113 8761
0.0.0.0 8890 192.168.77.113 8890