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
相关推荐
惘嘫、冋渞2 小时前
AWS同一账号下创建自定义VPC并配置不同区域的对等链接
网络·云计算·aws
云知谷4 小时前
【HTML】网络数据是如何渲染成HTML网页页面显示的
开发语言·网络·计算机网络·html
呉師傅8 小时前
关于联想ThinkCentre M950t-N000 M大师电脑恢复预装系统镜像遇到的一点问题
运维·网络·windows·电脑
代码AI弗森8 小时前
无状态的智慧:从 HTTP 到大模型的系统进化论
网络·网络协议·http
酷熊代理9 小时前
安卓手机 IP 切换指南:告别卡顿,轻松换 IP
网络·网络协议·tcp/ip·socks5
不做菜鸟的网工9 小时前
PIM SM +MSDP 组播跨域配置案例
网络协议
月上柳青9 小时前
快速创建无线AP热点
网络·智能路由器
K_i13410 小时前
云原生网络基础:IP、端口与网关实战
网络·ip·接口隔离原则
m0_6515939111 小时前
Netty网络架构与Reactor模式深度解析
网络·架构
大面积秃头11 小时前
Http基础协议和解析
网络·网络协议·http