WSL2-Ubuntu常见使用问题
# 默认root登录
先找到ubuntu.exe
的路径,如:C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu18.04onWindows_1804.2020.824.0_x64__79rhkp1fndgsc/ubuntu1804.exe
ubuntu.exe config --default-user root
1
再次打开后就默认是使用root
登录
# 错误提示
# SSH相关
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
1
2
2
解决办法:
ssh-keygen -t ecdsa -P '' -f /etc/ssh/ssh_host_ecdsa_key
ssh-keygen -t ed25519 -P '' -f /etc/ssh/ssh_host_ed25519_key
1
2
2
# 设置固定IP
# 修改子系统默认的dns
创建/etc/wsl.conf,写入以下内容,默认为true,true 将 WSL 设置为生成 /etc/resolv.conf
[network]
generateResolvConf=false
1
2
2
resolv.conf是/run/resolvconf/resolv.conf(generateResolvConf=true的时候自动生成)符号链接文件,需要先删除;DNS列表自行设置
rm /etc/resolv.conf
echo "nameserver 114.114.114.114" > /etc/resolv.conf
1
2
2
# ip配置脚本
这个脚本每次 ubuntu
重新启动都需要重新运行一遍
@echo off
rem 设置网段
set localnet=192.168.174
rem 设置子系统的ip
set ip=%localnet%.2
rem 需要设置的子系统名称
set linux=Ubuntu-18.04
wsl -d %linux% -u root ip addr del $(ip addr show eth0 ^| grep 'inet\b' ^| awk '{print $2}' ^| head -n 1) dev eth0
wsl -d %linux% -u root ip addr add %ip%/24 broadcast %localnet%.255 dev eth0
wsl -d %linux% -u root ip route add 0.0.0.0/0 via %localnet%.1 dev eth0
powershell -c "Get-NetAdapter 'vEthernet (WSL)' | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$False; New-NetIPAddress -IPAddress %localnet%.1 -PrefixLength 24 -InterfaceAlias 'vEthernet (WSL)'; Get-NetNat | ? Name -Eq WSLNat | Remove-NetNat -Confirm:$False; New-NetNat -Name WSLNat -InternalIPInterfaceAddressPrefix %localnet%.0/24;"
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
上次更新: 2023/09/22, 16:54:32