I regularly find myself wanting to copy a file from remote terminal session to my local machine. Usually I log out of the remote session and call an scp transfer from local to copy the file from remote to local. But this feels a little long winded. I would like to transfer the file whilst logged into the remote over SSH to save time. My local machine is connected to the internet from a dynamic IP range so I'm never quite sure how to connect to it remotely. But surely, as the remote session originates from my laptop, there must be a shortcut in scp to get back to my laptop... Right?

6 Answers 6


You can use SSH tunneling for this.

Using tunneling you can forward a TCP port either from your local machine to the remote machine, or from the remote machine to your local machine. I use it frequently to forward e.g. SMTP or IMAP ports from a remote machine behind a firewall to my local machine (and then access the services locally, as if they were running locally).

To forward port 22 (SSH) from you local machine to the remote machine try this:

ssh -R12345:localhost:22 yourremoteuser@remotemachine

(Note that localhost refers to the local name of the remote machine)

After running this you should be able to ssh back home using:

ssh -p12345 [email protected]

When using scp, you would do something like (scp has an uppercase P for port forwarding):

scp -P12345 filename

Port forwarding in the other direction (from remote to local) uses -L instead of -R.

The above commands assume that you are using a terminal ssh client. Graphical clients, like PuTTY for Windows, also support tunneling

  • Doesn't answer OP's question whose IP isn't static.
    – Heandel
    May 30, 2011 at 14:41
  • 3
    Unless I missed something this should fine for the OP to connect from the currently remote machine back to the local. The tunneling will be done through the SSH session. As the SSH connection is setup from the dynamic local machine to the remote it will succeed and will thus be able to setup a listening port on the remote machine that tunnels back to where it came from.
    – Daniel Lundmark
    May 31, 2011 at 5:50
  • This will require OP to set up SSH server on his local machine and take care of authentication. So this may be more trouble than it's worth in some use cases.
    – Septagram
    Jan 19, 2012 at 9:20
  • Not only this works for dynamic IPs, it also works across firewalls that prohibit ssh connections from remote to local. Very useful answer!
    – user15617
    Jan 20, 2014 at 8:33
  • 3
    You can have it even more convenient by setting the remote port forwarding with the ~C escape. Just type <Enter>~C-R 12345:localhost:22<Enter>, and you have your tunnel without leaving your already established SSH session.
    – user15617
    Jan 20, 2014 at 8:38

Try the following in the command line from the remote machine, you may need to enable port forwarding on your router.

scp <file on remote machine> ${SSH_CLIENT%% *}:<directory on local machine>

Source: Easily scp a file back to the host you're connecting from (commandlinefu.com)

  • 1
    To explain what this does for people came across this, ${SSH_CLIENT%% *} output the IP of your local machine. So the whole command open a scp session from remote machine to your local machine for file transfering. This can only be done if you can SSH to your local machine from the remote machine.
    – bizi
    Jan 11, 2016 at 20:00
  • 2
    This can be further improved by adding something like export ME="${SSH_CLIENT%% *}" into your shell rc file. You can then simply use scp <file> $ME:<local path>
    – kralyk
    Mar 16, 2016 at 13:03
  • 1
    Great, this works, however note: If you login via VPN the files will not end up on your machine, but somewhere else.
    – kap
    Jun 11, 2019 at 7:32

Maybe zssh?

zssh (Zmodem SSH) is a program for interactively transferring files to a remote machine while using the secure shell (ssh). It is intended to be a convenient alternative to scp , allowing to transfer files without having to open another session and re-authenticate oneself.

zssh is an interactive wrapper for ssh

It uses the venerable rz, sz implementations of zmodem file transfer.


Upvoted this question, this is something I would like to do achieve easily as well.

Here is related answer: How do I SCP from remote machine to local machine when I am outside of my home network?

You need to allow access to ssh from outside your network. This is done by forwarding a port on the your broadband router to the lan ip of your server. However there's some security concerns with allowing ssh access from outside, so you may also want to look into methods to secure ssh, particually key-based authentication and disable password authentication entirely.


One solution would be to suspend the ssh session on the local machine, perform the copy on the local machine and then resume your ssh session where you left off.

For example, suppose I'm connected to myuser@remote, and I want to copy the file lol.txt to my local computer. First I want to suspend ssh on my local machine. Normally you would use Ctrl-Z to suspend, but this won't work in ssh because it will be sent to the remote shell. Instead, you must use the ssh escape sequence Enter~. So to suspend ssh press Enter~ Ctrl-Z. Now you can run commands on your local machine, and can do

user@localmachine:~$ scp user@remote:/path-to-file/lol.txt /dest-path/lol.txt

to copy the file and then return to ssh exactly where you left it with

user@localmachine:~$ fg

  • but the problem is not copying from remote, the problem is copying to remote, I don't see how suspending shell does anything to address that...
    – alex
    Oct 29, 2015 at 15:30
  • Well it works the other way too. The idea is just that you suspend the session instead of exiting it, which makes it at least a bit easier. Feb 9, 2017 at 12:28

If you want a simpler solution, just open dropbox account, go to remote machine and do wget (modified version from here): wget --no-check-certificate https://www.dropbox.com/s/2123jshf/ABC.pdf?dl=1 -O abc.pdf

You must log in to answer this question.

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