WSL 启动Ubuntu18.04默认设置网络和开启SSH

1、wsl-ip.bat脚本

@echo on
REM 先关闭虚拟子系统
wsl --shutdown

REM 以下的Ubuntu为第2步查到的Linux子系统名称
REM IP地址根据自己的需要配置
wsl -d Ubuntu-18.04 -u root ip addr del $(ip addr show eth0 ^| grep 'inet\b' ^| awk '{print $2}' ^| head -n 1) dev eth0
wsl -d Ubuntu-18.04  -u root ip addr add 172.30.208.2/24 broadcast 172.30.208.255 dev eth0
wsl -d Ubuntu-18.04  -u root ip route add 0.0.0.0/0 via 172.30.208.1 dev eth0

powershell -c "Get-NetAdapter 'vEthernet (WSL)' | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$False; New-NetIPAddress -IPAddress 172.30.16.1 -PrefixLength 24 -InterfaceAlias 'vEthernet (WSL)'; Get-NetNat | ? Name -Eq WSLNat | Remove-NetNat -Confirm:$False; New-NetNat -Name WSLNat -InternalIPInterfaceAddressPrefix 172.30.16.0/24;"

REM 启动对应Linux系统
wt -p Ubuntu-18.04

2、service.sh

sudo service ssh --full-restart ## 将该命令保存为service.sh

posted @ 2023-05-25 23:25  一只桔子2233  阅读(419)  评论(0编辑  收藏  举报