Is it possible to SCP from a remote to local whilst logged into the remote and without knowing the local's IP address?

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?


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 127.0.0.1:/tmp/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


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)