1

I'm connected to a remote host over ssh. To copy something from the remote host to my local host I can copy it using scp.

However this would require typing in the complete path to the file on the remote host. I'm already in the directory to the file on my ssh connection. Does there exists a way to copy the file back to my connecting host (so my local machine) using my current open connection on the remote host?

I found this answer already. However this requires a routable IP for the connecting host. I don't have that.

4
  • 1
    Is there something that stops you from running scp from the same host you're connected from? That would even allow you to use connection sharing (using the ControlMaster and ControlPath ssh options, with no need to authenticate again). Or do you absolutely need the connection to be in the other direction?
    – Kusalananda
    Commented Oct 5, 2022 at 13:21
  • @Kusalananda I found the connection sharing stuff already and it works great. It would just be nice to send a file back using the remote session instead of opening a new terminal (or switching to one) and filling in all the information for the remote when there is an existing context with much of the info already Commented Oct 5, 2022 at 13:53
  • Was my answer here at all useful?
    – larsks
    Commented Feb 2, 2023 at 21:53
  • Does this answer your question? Copy a file back to local system with ssh Commented Sep 5, 2023 at 22:40

1 Answer 1

1

Does there exists a way to copy the file back to my connecting host (so my local machine) using my current open connection on the remote host?

Sure! But we need to dip our toes into the ancient world of serial line connectivity...back when the only thing between you and a remote computer was a copper line, and "ip addresses" weren't a thing that existed (for most people), so there weren't any options for establishing some sort of out-of-band connection to a remote host.

Many of the tools we used back then survived into the modern era, and along the way picked up various networking tricks now that serial connectivity wasn't the only option...but they kept the rest of their feature set, including support for "inline" file transfer protocols (that is, transfer protocols that run over your existing connection, rather than opening a new connection).

Here are two solutions that will do what you want; in both cases you'll need to install software on the local and remote hosts.

Using Kermit

  1. Install kermit on both the local and the remote hosts.

  2. Use kermit to establish the ssh connection to the remote host:

    kermit -EC 'ssh remotehost.example.com'
    
  3. Use kermit on the remote host to start a file transfer:

    remotehost$ kermit -s somefile.txt
    
  4. Watch your local Kermit automatically enter receive mode and receive the file. You'll see it show up in your local current working directory.

Using screen + lrzsz

You can do something similar using screen and the lrzsz package, which includes a Zmodem implementation.

  1. Install screen locally.

  2. Install lrzsz both locally and on the remote host.

  3. Edit your ~/.screenrc to include:

    zmodem catch
    
  4. Start a new screen session and connect to the remote host:

    screen -e '' ssh remotehost.example.com
    
  5. Use the sz command to transfer a file:

    remotehost$ sz somefile.txt
    

    This will bring up an rz command at the bottom of the screen, just press <return>.

  6. Watch the file transfer to your local system. As before, you'll find the file in your local current working directory.

Using zssh + lrzsz

Zssh is a wrapper for ssh that understand Zmodem escapes. The process looks almost exactly like the previous section, but you use zssh instead of screen:

  1. Install lrzsz on both the local and remote hosts.

  2. Connect to the remote host:

    localhost$ zssh remotehost
    
  3. Initiate a file transfer:

    remotehost$ sz somefile.txt
    
  4. Type CTRL-SPACE to enter file transfer mode and type rz (then <return>).


I'm running on Fedora 36, and everything mentioned here (kermit, screen, lrzsz, zssh) is available as a package; to get "all of the above" you would run:

sudo yum install screen ckermit zssh lrzsz

On other distributions you may need to install things manually.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .