Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com May 31 '11 at 14:56

This question came from our site for professional and enthusiast programmers.

5 Answers 5

up vote 7 down vote accepted

You can use SSH tunneling for this.

http://en.wikipedia.org/wiki/Tunneling_protocol

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 yourlocaluser@127.0.0.1

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

scp -P12345 filename 127.0.0.1:/tmp/filename

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

The above commands assume that you are using a terminal ssh client. Graphical clients, like putty for windows also supports tunneling

share|improve this answer
    
Doesn't answer OP's question whose IP isn't static. –  Heandel May 30 '11 at 14:41
1  
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 '11 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 '12 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! –  Georgi Kirilov Jan 20 at 8:33
1  
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. –  Georgi Kirilov Jan 20 at 8:38

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.

share|improve this answer

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.

share|improve this answer

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)

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.