Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

If I'm logged in to a system via SSH, is there a way to copy a file back to my local system without firing up another terminal or screen session and doing scp or something similar or without doing SSH from the remote system back to the local system?

share|improve this question
1  
If you have an ssh server on your client you could always try scp file.foo user@myclient.com:file.foo :P – rahmu Nov 28 '11 at 20:22
For sure, but I don't want to allow access to my computer from a server owned by a company I work for :) I only have key-based authentication here anyway, and it wouldn't be too secure to put my private key up on the server! – TK Kocheran Nov 28 '11 at 20:35
2  
I don`t understand your problem. Generate a new key pair, copy the public part to your computers authorized_keys and after your transfer delete that line again. – Nils Nov 28 '11 at 21:48
1  
I do this a lot, so it'd be inefficient to do this every time I want to copy a file in the middle of a SSH session. I'm just looking for a way, while in a SSH terminal session, to hook back to my local computer and have it send up a file to the remote server without having to leave the current SSH session. – TK Kocheran Nov 28 '11 at 22:21
There's no need to delete that key if you add a passphrase to your company-side key. – glglgl Nov 29 '11 at 8:35
show 1 more comment

9 Answers

up vote 25 down vote accepted

Master connection

It's easiest if you plan in advance.

Open a master connection the first time. For subsequent connections, route slave connections through the existing master connection. In your ~/.ssh/config, set up connection sharing to happen automatically:

ControlMaster auto
ControlPath ~/.ssh/control:%h:%p:%r

If you start an ssh session to the same (user, port, machine) as an existing connection, the second session will be tunneled over the first. Establishing the second connection requires no new authentication and is very fast.

So while you have your active connection, you can quickly:

On an existing connection, you can establish a reverse ssh tunnel. On the ssh command line, create a remote forwarding by passing -R 22042:localhost:22 where 22042 is a randomly chosen number that's different from any other port number on the remote machine. Then ssh -p 22042 localhost on the remote machine connects you back to the source machine; you can use scp -P 22042 foo localhost: to copy files.

You can automate this further with RemoteForward 22042 localhost:22. The problem with this is that if you connect to the same computer with multiple instances of ssh, or if someone else is using the port, you don't get the forwarding.

If you haven't enabled a remote forwarding from the start, you can do it on an existing ssh session. Type Enter ~C Enter -R 22042:localhost:22 Enter. See “Escape characters” in the manual for more information.

There is also some interesting information in this Server Fault thread.

share|improve this answer

SSH does support a few commands, via the ~ escape character:

$ ~?
Supported escape sequences:
  ~.  - terminate connection (and any multiplexed sessions)
  ~B  - send a BREAK to the remote system
  ~C  - open a command line
  ~R  - Request rekey (SSH protocol 2 only)
  ~^Z - suspend ssh
  ~#  - list forwarded connections
  ~&  - background ssh (when waiting for connections to terminate)
  ~?  - this message
  ~~  - send the escape character by typing it twice
(Note that escapes are only recognized immediately after newline.)

$ ~C
ssh> help
Commands:
      -L[bind_address:]port:host:hostport    Request local forward
      -R[bind_address:]port:host:hostport    Request remote forward
      -D[bind_address:]port                  Request dynamic forward
      -KR[bind_address:]port                 Cancel remote forward
      !args                                  Execute local command

The !args seems to be closest to what you want. Note that you'll need to have PermitLocalCommand enabled in your /etc/ssh_config file in order for the ~C commands to work (see man ssh_config).

You can re-use the same ssh session if you set up a ControlMaster in ssh_config. If you do this:

$ ~C
ssh> !scp file user@myserver:

you've technically never left the ssh session, and don't need to re-authenticate. Probably more complicated than you'd like, but I can't think of another easy way.

share|improve this answer
That's exactly what I'm looking for, thanks!!! – TK Kocheran Nov 28 '11 at 23:25

Those are all very complicated methods.
You can mount the remote file system on your local machine with sshfs:

mkdir -p /mnt/sshfs

root@IS1300:~# sshfs 192.168.1.2:/ /mnt/sshfs
root@IS1300:~# umount /mnt/sshfs

Then you can copy paste the file with nautilus, gnome, konqueror, dolphin, bash or whatever.

share|improve this answer

An even simpler approach: Open Filezilla (or your favorite ftp browser), open an ssh connection to the same site, find the file and drag it across to your local file structure. If you're new to Filezilla, use the "site manager" feature to reconnect fast next time.

Yes, I know this is obvious to most of you (and not precisely on point), but some (like me) who found this thread searching for a terminal-only solution may have overlooked the obvious.

share|improve this answer
Yes, or sftp as suggested here. – atomicules Jul 13 '12 at 11:08

This isn't possible in the "same session", but to make it easier to open another session for copying, you can either use SSH keys or the "Control Master" feature.

share|improve this answer
  • Use ssh-xfer, a modified ssh-agent which effectively overloads an existing ssh side-channel for file-transfer use.
  • Use zssh, which is effectively zmodem over ssh. If you've ever used rzsz this will seem very familiar.
  • Reverse (-R, for remote-to-local) or forward (-L, for local-to-remote) ports to run file transfers over, assuming you have some file-transferring daemon listening on the other end.

But none of these are really needed, IMO. The SSH protocol supports multiple channels on a single connection, and the OpenSSH client supports multiplexing. Assuming you have ControlMaster and ControlPath set up (ControlPersist is useful too),

  # first connection
$ ssh remote

  # will multiplex over the same connection the original ssh opened
$ sftp remote
share|improve this answer

There is an explanation of how to get the reverse tunnel automatically set up

I have not tried this myself yet, but have kept it in my bookmarks for trying it out at some point in the future.

share|improve this answer
This seems to be the same as Gilles' answer – Michael Mrozek Oct 8 '10 at 16:24

What I've found to be the best and most efficient solution is to use xclip-copyfile and xclip-pastefile.

On the server, you use xclip-copyfile to copy one or more files. These files are then available on your local server. There, you can use xclip-pastefile.

This bypasses the need to use scp or have a local ssh server. I use this with cygwin for instance. The only problem is that this requires installing xclip if you don't already have it. Oh, and this works with binary files too.

share|improve this answer

Use "!" to convert the file to a ASCII-representation of your file (e.g. uuencode). Then use ! cat uuencode.dat >target.dat. After that use uudecode on the target side.

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.