简体   繁体   中英

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.

Before using wsl2, I used to be able to do the following:

Remote server

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

Local SSH config

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:

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. Which probably means windows is receiving this request on localhost:22 (where no SSH server is running) and then hangs up the connection.

How can I forward the request to my WSL2 SSH process?

It is possible to add a port mapping to WSL2 machines, using the following WSH script:

$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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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