WSL设置添加固定IP
- 在Win端添加一个固定IP 192.168.50.99 用于X-Server界面显示.
- 在WSL端添加一个固定IP 192.168.50.16 用于和Win端通讯
在win端创建批处理文件
创建一个批处理文件 我的文件位置是D:\powershell\static_ip.bat
-
向vEthernet (WSL)网卡添加一个IP 192.168.50.99
-
向wsl子系统网卡添加一个IP 192.168.50.16
@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
netsh interface ip add address "vEthernet (WSL)" 192.168.50.99 255.255.255.0
wsl -d Ubuntu-18.04 -u root ip addr add 192.168.50.16/24 broadcast 192.168.50.255 dev eth0 label eth0:1
exit
ps:运行时使用管理员运行
值得注意的是,在添加了网卡后,有可能存在网络不通外网的情况
编辑/etc/resolv.conf,有可能该文件不存在,直接使用vim编辑就好了,会自动新建的
这个是dns,这里使用的是阿里云的dns,当然,其他dns也可以
nameserver 223.5.5.5
nameserver 223.6.6.6