前篇已設置默認爲<font color=Red>root</font>用戶,故全部命令省略sudo
,非root用戶需在命令前自行添加sudo
Ubuntu20.04子系統自帶的ssh服務沒法鏈接,需卸載後從新安裝。ssh
apt remove openssh-server
工具
apt install openssh-server
spa
編輯/etc/ssh/sshd_config
文件。3d
(1)修改ssh服務監聽端口和監聽地址rest
(2)修改ssh服務容許使用用戶名密碼方式登入code
(4)修改ssh服務容許遠程root用戶登入server
(5)重啓ssh服務。blog
service ssh restart
rem
在前篇提到的/etc/init.wsl
文件中添加service ssh start
設置ssh服務開機自啓。it
在Power Shell中經過ssh命令鏈接wsl子系統。
ssh root@localhost -p 2222
其中2222
爲上面設置ssh服務監聽端口。
此時經過PC的IP地址是沒法訪問wsl的,需設置端口轉發和防火牆。
(1)查看wsl的地址
ifconfig
工具apt install net-tools
ifconfig
(2)將端口轉發到wsl,在Power Shell下執行命令,將[IP]和[PORT]替換爲wsl的IP和端口。
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=2222 connectaddress=[IP] connectport=[PORT]
(3)開啓防火牆入站規則(也能夠在控制面板-Windows Defender 防火牆-高級設置-入站規則中設置)
netsh advfirewall firewall add rule name=WSL2 dir=in action=allow protocol=TCP localport=2222
設置完成後,便可經過IP地址遠程訪問wsl。