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号35 分钟前
windows文件共享另一台电脑资源管理器网络文件夹无法找到机器
运维·服务器·网络
LetsonH1 小时前
Home Assistant 米家集成:开启智能家居新体验
网络·智能家居
欧先生^_^1 小时前
Docker 的各种网络模式
网络·docker·容器
what_20181 小时前
分布式2(限流算法、分布式一致性算法、Zookeeper )
分布式·网络协议·rpc
彬彬醤2 小时前
查询电脑伪装IP,网络安全速查攻略!
网络·网络协议·tcp/ip·安全·web安全·http·https
还有几根头发呀3 小时前
常见 RPC 协议类别对比
网络协议
兴达易控4 小时前
Profibus DP主站转Modbus TCP网关接E+H流量计通讯案例
网络
熙曦Sakura4 小时前
【Linux网络】TCP全连接队列
linux·网络·tcp/ip
兴达易控4 小时前
ABB电机控制和保护单元与Profibus DP主站转Modbus TCP网关快速通讯案例
网络协议
星星点点洲5 小时前
【网络协议】TCP、HTTP、MQTT 和 WebSocket 对比
网络协议·tcp/ip·http