在 wsl 2 中开启 ssh 连接
source link: https://nicksxs.me/2023/04/23/%E5%9C%A8-wsl-2-%E4%B8%AD%E5%BC%80%E5%90%AF-ssh-%E8%BF%9E%E6%8E%A5/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
在 wsl 2 中开启 ssh 连接
之前在 wsl 1 中开启 ssh 其实很方便,只要把 sshd 服务起来就好了,但是在 wsl 2 中就不太一样了,
我这边使用的是 wsl 2 中的 Ubuntu 20.04,直接启动 sshd 服务是没法让其他机器连接的,而且都没有 ifconfig 命令可以查看 ip
不过可以用直接用 ip a
来查看,可以看到这个 ip 是172
网段的,而在 wsl 1 中可以看到 ip 就是 win 的 ip,
所以需要做一些操作,首先要安装 openssl-server
sudo apt update
sudo apt install openssh-server
另外如果需要提高安全性,可以wsl 中配置 hosts.allow
sshd:192.168.xx.
先定一个子网段,然后对于ssh的配置,可以做以下修改
Port 22
PasswordAuthentication yes
在进行重启
sudo service ssh --full-restart
配置以后发现上面的问题,没法远程登录,因为 wsl 2 是基于 hyper-v 虚拟机实现的,并且 ip 使用的是一个虚拟出来的子网 ip,所以需要在 Windows 这一层配置端口的转发,可以通过命令netsh interface portproxy show v4tov4
看到
截图是我已经添加好了的,先把原来的删除,再进行添加
netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=22
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=22 connectaddress=172.19.129.207 connectport=22
也可以全量删除
netsh int portproxy reset all
但是这样也不能直接访问了,还需要开启防火墙
netsh advfirewall firewall add rule name="WSL SSH" dir=in action=allow protocol=TCP localport=22
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK