WSL 2 自动更新 虚拟 IP 到 window hosts

window下的wsl2 开发中使用到 域名映射,但是WSL2 每次启动都会被分配一个虚拟的 ip 地址,每次启动虚拟ip 都不一样,导致要频繁 更改 window 的 hosts 文件,太麻烦了,所以写一个自动执行的 .sh 脚本,每次启动之后 自动获取虚拟ip,并写入到 window 的 hosts 文件中。

1、在 当前用户目录下 新建 script/get_wsl_ip.sh 文件脚本,我的路径 \\wsl$\Ubuntu\home\user1\scripts

bash 复制代码
#!/usr/bin/bash

# 为 win 设置 wsl host

# win hosts 文件路径
win_hosts_path="/mnt/c/Windows/System32/drivers/etc/hosts"

# 为 wsl2 设置的域名
wsl_domain="dev.domain.com"

# 获取 wsl2 的 ip
wsl_ip=$(ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')

if [ -z "$wsl_ip" ]; then
    echo "无法获取 WSL2 的 IP 地址,请检查网络配置。"
    exit 1
fi

# 判断是否已存在 wsl2 的域名,如果存在则修改,否则追加
if grep -wq "$wsl_domain" "$win_hosts_path"; then
    # 备份原始 hosts 文件
    cp "$win_hosts_path" "$win_hosts_path.bak"
    
    # 使用临时文件避免权限问题
    temp_file=$(mktemp)
    sed "s/.* $wsl_domain/$wsl_ip $wsl_domain/" "$win_hosts_path" > "$temp_file"
    sudo mv "$temp_file" "$win_hosts_path"
else
    # 追加记录到 hosts 文件
    echo "$wsl_ip $wsl_domain" | sudo tee -a "$win_hosts_path" > /dev/null
fi

# 为 wsl 设置 win host
wsl_hosts_path="/etc/hosts"
win_domain="win"
win_ip=$(grep "nameserver" /etc/resolv.conf | awk '{print $2}')

if [ -z "$win_ip" ]; then
    echo "无法获取 Windows 主机的 IP 地址,请检查网络配置。"
    exit 1
fi

if grep -wq "$win_domain" "$wsl_hosts_path"; then
    # 使用临时文件避免权限问题
    temp_file=$(mktemp)
    sed "s/.* $win_domain/$win_ip $win_domain/" "$wsl_hosts_path" > "$temp_file"
    sudo mv "$temp_file" "$wsl_hosts_path"
else
    # 追加记录到 hosts 文件
    echo "$win_ip $win_domain" | sudo tee -a "$wsl_hosts_path" > /dev/null
fi

echo "设置完成。"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

2、修改当前用户目录下 \\wsl$\Ubuntu\home\user1\.bashrc,在末尾加

bash 复制代码
# Call custom script to get WSL IP and save it for host update
/home/user1/scripts/get_wsl_ip.sh

3、重启 wsl 即可

bash 复制代码
wsl --shutdown

4、window 的 hosts 会自动添加下面的 ip 地址映射

bash 复制代码
# wsl 2 虚拟的IP
172.23.38.130 dev.domain.com
相关推荐
白鹭24 分钟前
基于LNMP架构的分布式个人博客搭建
linux·运维·服务器·网络·分布式·apache
sx2436941 小时前
day33:零基础学嵌入式之网络——TCP并发服务器
网络·网络协议·http
wmm_会飞的@鱼5 小时前
FlexSim-汽车零部件仓库布局优化与仿真
服务器·前端·网络·数据库·数学建模·汽车
-XWB-6 小时前
【安全漏洞】网络守门员:深入理解与应用iptables,守护Linux服务器安全
linux·服务器·网络
还是朝夕6 小时前
OSPF路由协议 多区域
网络
消失的旧时光-19436 小时前
Android网络框架封装 ---> Retrofit + OkHttp + 协程 + LiveData + 断点续传 + 多线程下载 + 进度框交互
android·网络·retrofit
晴天¥7 小时前
阶段1--域名服务器
运维·服务器·网络
许愿OvO7 小时前
IP--MGER综合实验报告
网络·tcp/ip·智能路由器
thginWalker7 小时前
图解网络-小林coding笔记(持续更新)
网络·笔记
小刘|8 小时前
Https以及CA证书
网络·网络协议·https