合 Ubuntu20.04配置开机自动运行脚本(开机启动)--适用于WSL2
Tags: OSWindows 11WSL2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | cp /lib/systemd/system/rc-local.service /etc/systemd/system cat >> /etc/systemd/system/rc-local.service <<"EOF" [Install] WantedBy=multi-user.target Alias=rc-local.service EOF cat > /etc/rc.local <<"EOF0" #!/bin/sh ip addr del $(ip addr show eth0 | grep 'inet\b' | awk '{print $2}' | head -n 1) dev eth0 ip addr add 192.168.8.8/24 broadcast 192.168.8.255 dev eth0 ip route add 0.0.0.0/0 via 192.168.8.1 dev eth0 cat > /etc/resolv.conf <<"EOF" nameserver 114.114.114.114 nameserver 8.8.8.8 EOF export WSL_INTEROP=/run/WSL/8_interop export PATH=$PATH:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Users/lhr/AppData/Local/Microsoft/WindowsApps:/mnt/f/platform-tools/ cmd.exe /c "f:\ubuntu_static_ip.bat" 1> nul exit 0 EOF0 sudo chmod +x /etc/rc.local 文件f:\ubuntu_static_ip.bat,文件内容: powershell -c "Get-NetAdapter 'vEthernet (WSL)' | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$False; New-NetIPAddress -IPAddress 192.168.8.1 -PrefixLength 24 -InterfaceAlias 'vEthernet (WSL)'; Get-NetNat | ? Name -Eq WSLNat | Remove-NetNat -Confirm:$False; New-NetNat -Name WSLNat -InternalIPInterfaceAddressPrefix 192.168.8.0/24;" |