繁体   English   中英

WSL2 SSH RemoteForward 连接回

[英]WSL2 SSH RemoteForward connect back

在签出开发服务器上的分支后,我正在尝试在我的开发服务器上使用rsync将文件下载到我的本地计算机。

在使用 wsl2 之前,我曾经能够做到以下几点:

远程服务器

rsync -ave "ssh -p 22001" --delete --exclude-from ~/rsync_exclude_list.txt ~/as/ alex@localhost:/home/alexmk92/code/project

本地 SSH 配置

Host dev-tunnel
    HostName dev.sever.co.uk
    User as
    ControlMaster auto
    ControlPath ~/.ssh/sockets/%r@%h:%p
    RemoteForward 22001 localhost:22

Host dev
    HostName dev.server.co.uk
    User as
    RequestTTY yes
    RemoteCommand cd as; bash

然后我可以使用ssh devssh -fvN dev-tunnel运行这些,如果我从远程服务器输入ssh -p 22001 alex@localhost然后

debug1: remote forward success for: listen 22001, connect localhost:22
debug1: All remote forwarding requests processed
debug1: client_input_channel_open: ctype forwarded-tcpip rchan 2 win 2097152 max 32768
debug1: client_request_forwarded_tcpip: listen localhost port 22001, originator 127.0.0.1 port 34472
debug1: connect_next: host localhost ([127.0.0.1]:22) in progress, fd=5
debug1: channel 1: new [127.0.0.1]
debug1: confirm forwarded-tcpip
debug1: channel 1: connection failed: Connection refused
connect_to localhost port 22: failed.
debug1: channel 1: free: 127.0.0.1, nchannels 2

我猜这是因为 WSL2 不再在 localhost 上运行,而是在 Hypervisor 中被隔离。 这可能意味着 windows 正在localhost:22上接收此请求(没有 SSH 服务器正在运行),然后挂断连接。

如何将请求转发到我的 WSL2 SSH 进程?

可以使用以下 WSH 脚本向 WSL2 机器添加端口映射:

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

if( $found ) {
  $remoteaddr = $matches[0];
} else {
  echo "Error: ip address of WSL 2 cannot be found";
  exit;
}
Invoke-Expression "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr"
Invoke-Expression "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteaddr"

echo "Success: Port mapping added!";

当然,您需要更改为端口,也许是 IP 地址(前两行)也许您需要以管理员身份运行脚本...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM