简体   繁体   English

WSL2 SSH RemoteForward 连接回

[英]WSL2 SSH RemoteForward connect back

I'm trying to use rsync on my dev server to download files to my local machine after checking out a branch on the dev server.在签出开发服务器上的分支后,我正在尝试在我的开发服务器上使用rsync将文件下载到我的本地计算机。

Before using wsl2, I used to be able to do the following:在使用 wsl2 之前,我曾经能够做到以下几点:

Remote server远程服务器

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

Local SSH config本地 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

I can then run these with ssh dev and ssh -fvN dev-tunnel if from the remote server I type ssh -p 22001 alex@localhost then I get:然后我可以使用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

I'm guessing this is because WSL2 no longer runs on localhost, and is instead isolated within Hypervisor.我猜这是因为 WSL2 不再在 localhost 上运行,而是在 Hypervisor 中被隔离。 Which probably means windows is receiving this request on localhost:22 (where no SSH server is running) and then hangs up the connection.这可能意味着 windows 正在localhost:22上接收此请求(没有 SSH 服务器正在运行),然后挂断连接。

How can I forward the request to my WSL2 SSH process?如何将请求转发到我的 WSL2 SSH 进程?

It is possible to add a port mapping to WSL2 machines, using the following WSH script:可以使用以下 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!";

Of course, you need to change to port and maybe the IP address (first two lines) Maybe you need to run the script as admin...当然,您需要更改为端口,也许是 IP 地址(前两行)也许您需要以管理员身份运行脚本...

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

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