Window11 WSL2 添加静态IP的脚本方法 基于评论 https://GitHub.com/microsoft/WSL/issues/4210#issuecomment-648570493 的思路,我给
基于评论 https://GitHub.com/microsoft/WSL/issues/4210#issuecomment-648570493 的思路,我给出一个实现静态IP效果的变通方法。
1.在WSL2 linux中创建一个脚本文件
vim /home/likfees/shell/static-ip.sh
#!/bin/bash
/mnt/c/windows/system32/netsh.exe interface ip show addresses "vEthernet (WSL)" | /mnt/c/WINDOWS/system32/findstr.exe /C:"172.18.16.1" > /dev/null
if [ $? == 1 ]; then
/mnt/c/WINDOWS/system32/netsh.exe interface ip add address "vEthernet (WSL)" address=172.18.16.1/24
fi
注:这段脚本给 Windows 端的"vEthernet (WSL)"加了IPv4 172.18.16.1,给 Linux 端的 eth0 添加 IPv4 172.18.16.1。在使用前,请按需调整网络接口的名称和IP地址。
2.在 WSL2 启动的时候执行该脚本
vim /etc/profile
添加以下内容
sudo /home/likfees/Shell/static-ip.sh
当然如果让 eth0 重新获取了 IP,那么需要手动运行 static-ip.sh
。
3.win11 能够 ping 通 wsl2,但是 wsl2 无法 ping 通 win11, 经分析主要是 win11 防火墙的原因.
在 win11 中 执行如下命令(管理员运行 PowerShell 或 cmd ):
New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow
4.使用静态IP
Windows 端静态IP 172.18.16.1
WSL Linux 端静态IP 172.18.16.100
ps:Win11 wsl2 固定IP方案
背景
win11 上通过wsl2安装了ubuntu,想其他电脑通过ssh登入Linux,但每次电脑重启,wsl的IP会变,网上找了个脚本,可以固定wsl的IP。
脚本
@echo off
setlocal enabledelayedexpansion
::先停掉可能在跑的wsl实例
wsl --shutdown ubuntu
if !errorlevel! equ 0 (
::检查WSL有没有我需要的IP
wsl -u roo
到此这篇关于Window11 WSL2 添加静态IP的文章就介绍到这了,更多相关Window11 WSL2 添加静态IP内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
--结束END--
本文标题: Window11 WSL2 添加静态IP的脚本
本文链接: https://www.lsjlt.com/news/177544.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0