【WSL】WSL折騰之旅(3)SSH遠程鏈接

前篇已設置默認爲<font color=Red>root</font>用戶,故全部命令省略sudo,非root用戶需在命令前自行添加sudo

1. ssh配置

1.1 ssh服務安裝

Ubuntu20.04子系統自帶的ssh服務沒法鏈接,需卸載後從新安裝。ssh

  • 卸載ssh服務

apt remove openssh-server工具

  • 重裝ssh服務

apt install openssh-serverspa

1.2 修改配置信息

編輯/etc/ssh/sshd_config文件。3d

(1)修改ssh服務監聽端口和監聽地址rest

image-20210309135901898

(2)修改ssh服務容許使用用戶名密碼方式登入code

image-20210309135954995

(4)修改ssh服務容許遠程root用戶登入server

image-20210309135935749

(5)重啓ssh服務。blog

service ssh restartrem

1.3 設置開機自啓

在前篇提到的/etc/init.wsl文件中添加service ssh start設置ssh服務開機自啓。it

2. ssh鏈接

2.1 本機鏈接

在Power Shell中經過ssh命令鏈接wsl子系統。

ssh root@localhost -p 2222

其中2222爲上面設置ssh服務監聽端口。

image-20210309142310026

2.2 遠程鏈接

此時經過PC的IP地址是沒法訪問wsl的,需設置端口轉發和防火牆。

image-20210309143222820

(1)查看wsl的地址

  • 安裝ifconfig工具

apt install net-tools

  • 查看IP地址,紅框位置爲wsl地址

ifconfig

image-20210309142726972

(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。

image-20210309144255804

相關文章
相關標籤/搜索