从远程服务器通过SSH连接WSL或WSL2

方法一(简单方法):

参考THE EASY WAY how to SSH into Bash and WSL2 on Windows 10 from an external machine - Scott Hanselman's Bloghttps://www.hanselman.com/blog/the-easy-way-how-to-ssh-into-bash-and-wsl2-on-windows-10-from-an-external-machine

首先确认 Windows 的 bash 已经绑定给 WSL/WSL2,然后使用

ssh -t username@servername "bash"

即可连接。如果要设置为默认,在 PowerShell Core 中执行

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "bash.exe" -PropertyType String -Force

即可。

方法二(复杂方法):

参考

WSL 2 Networkingicon-default.png?t=L9C2https://youtu.be/yCK3easuYm4在 WSL 2 中执行

ip addr | grep eth0

得到 WSL 2 的 IP 地址,然后以管理员身份运行 PowerShell Core,执行

netsh interface portproxy add v4tov4 listenport=3390 listenaddress=0.0.0.0 connectport=3390 connectaddress=<wsl2_ip_addr>

然后为端口 3390 设置 Windows Defeder 防火墙入站规则允许通过。

WSL2 的 IP 地址例如 172.23.239.4/20 连接到 Windows 的 Ethernet adapter vEthernet (WSL) 的 IP 地址例如 172.23.224.1/20,再通过 Wireless LAN adapter WLAn 的 IP 地址例如 192.168.1.132/24 连接到 WiFi。

由于 WSL 的 IP 地址每次都会发生改变,所以可以参考[WSL 2] NIC Bridge mode 🖧 (Has TCP Workaround🔨) · Issue #4150 · microsoft/WSL (github.com)icon-default.png?t=L9C2https://github.com/microsoft/WSL/issues/4150

 使用以下脚本一键配置。

$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

if( $found ){
  $remoteport = $matches[0];
} else{
  echo "The Script Exited, the ip address of WSL 2 cannot be found";
  exit;
}

#[Ports]

#All the ports you want to forward separated by coma
$ports=@(80,443,10000,3000,5000);


#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";


#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";

#adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";

for( $i = 0; $i -lt $ports.length; $i++ ){
  $port = $ports[$i];
  iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
  iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}

 将其保存为一个 ps1 文件,然后以 Execution 为 ByPass 执行

wslbridge.ps1 -ExecutionPolicy Bypass

参考How to SSH into WSL2 on Windows 10 from an external machine - Scott Hanselman's Blogicon-default.png?t=L9C2https://www.hanselman.com/blog/how-to-ssh-into-wsl2-on-windows-10-from-an-external-machine可以用

netsh interface portproxy show v4tov4

显示所有的 portproxy,用

netsh int portproxy reset all

重置(移除)所有 portproxy。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Windows Subsystem for Linux 2(WSL2)是Windows 10操作系统中的一个功能,它允许用户在Windows中运行Linux操作系统。对于那些熟悉Linux的开发人员来说,WSL2是一种非常方便的方式来使用他们所熟悉的命令行界面和工具。 在WSL2中,用户可以使用多种方法进行远程访问。其中,其中之一是通过ssh远程登录。 SSH(Secure Shell)是一种安全的网络协议,允许用户在不安全的网络上进行加密通信。通过ssh远程登录WSL2,用户可以像在本地一样使用WSL2,同时还可以利用Windows的GUI界面。 要使用ssh远程登录WSL2,用户需要先启用ssh服务器。在Windows PowerShell或命令提示符中,可以使用以下命令来启动ssh服务器: ``` sudo apt update sudo apt install openssh-server sudo service ssh start ``` 安装和启动过程完成后,可以使用ssh客户端连接WSL2。在本地电脑上,打开一个终端窗口并输入以下命令: ``` ssh [username]@[IP address of WSL2] ``` 其中,[username]是WSL2中的用户名,[IP address of WSL2]是WSL2的IP地址。默认情况下,WSL2会动态分配IP地址,可以使用以下命令在WSL2中查找其IP地址: ``` ip addr show eth0 | grep inet | awk '{print $2}' | cut -d / -f 1 ``` 使用这种方法进行远程访问,需要确保WSL2和Windows的防火墙设置正确。在Windows防火墙中,需要允许ssh流量通过。在WSL2中,需要允许ssh服务器通过WSL2的防火墙。可以使用以下命令配置WSL2防火墙: ``` sudo ufw allow ssh ``` 使用ssh远程登录WSL2时,还需要注意ssh客户端和服务端之间的证书问题。如果出现证书错误,需要检查证书是否正确配置。 总之,使用ssh远程登录WSL2是一种方便的方式,可让用户像在本地一样使用WSL2。通过配置Windows和WSL2的防火墙,并确保证书正确配置,用户可以安全、可靠地进行远程访问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ayka

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值