wsl2重启:
管理员权限打开powershell ,然后执行下面命令
powershell切换为管理员权限:
`Start-Process powershell -Verb runAs`
关闭服务:`net stop LxssManager`
重启服务:`net start LxssManager`
wsl2设置固定ip:
编写一个bat脚本,管理员方式运行:
@echo off
setlocal enabledelayedexpansion
::先停掉可能在跑的wsl实例
wsl --shutdown ubuntu
if !errorlevel! equ 0 (
::检查WSL有没有我需要的IP
wsl -u root ip addr | findstr "192.168.0.155" > nul
if !errorlevel! equ 0 (
echo wsl ip has set
) else (
::IP不存在则绑定IP
wsl -u root ip addr add 192.168.0.155/24 broadcast 192.168.0.255 dev eth0 label eth0:1
echo set wsl ip success: 192.168.0.155
)
::检查宿主机有没有我需要的IP
ipconfig | findstr "192.168.0.156" > nul
if !errorlevel! equ 0 (
echo windows ip has set
) else (
::IP不存在则绑定IP
netsh interface ip add address "vEthernet (WSL)" 192.168.0.156 255.255.255.0
echo set windows ip success: 192.168.0.156
)
)
::为主机设置SSH转发端口
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=23 connectaddress=192.168.0.155 connectport=23
pause
但是此方法有个弊端是,每次重启电脑的时候都要运行一下。
ssh连接wsl2:
1.进入wsl子系统卸载重装ssh服务:
卸载
sudo apt remove openssh-server
安装
sudo apt install openssh-server
2.修改配置信息:
(1) 修改SSH Server的监听端口,这里为23
sudo sed -i '/Port /c Port 23' /etc/ssh/sshd_config
(2) 修改SSH Server的监听地址
sudo sed -i 'ListenAddress 0.0.0.0/c ListenAddress 0.0.0.0' /etc/ssh/sshd_config
(3) 修改SSH Server允许使用用户名密码方式登录
sudo sed -i '/PasswordAuthentication /c PasswordAuthentication yes' /etc/ssh/sshd_config
(4) 修改SSH Server允许远程root用户登录
sudo sed -i '/PermitRootLogin /c PermitRootLogin yes' /etc/ssh/sshd_config
3.重启ssh服务
sudo service ssh restart
4.使用ssh登录
ssh root@192.168.0.156 -p 23