Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

So I'm SSHed into my Ubuntu server from my Ubuntu desktop. I'm at a certain path and I want to download a file to my local filesystem (preferably the path I was at before I entered the SSH session).

I could mount SSH and pull the file across by mouse but what if I was trying to get a root file and logging in by root directly is disallowed? Even if that wasn't the case (it isn't now), surely there must be a simple way of pulling back a file over an active SSH connection.

Surely!

share|improve this question
    
Interesting question! It really shows how peoples' ideas of what is sensible to do get shaped by the tools they use. zssh is probably closest to the zmodem-like workflow you may be remembering. –  poolie Nov 17 '10 at 1:30
    
I'm really surprised that even after 8 answers, there's not really any way to do this –  endolith Apr 21 '13 at 4:22
add comment

8 Answers 8

up vote 20 down vote accepted

You may want to check out zssh, which is available in universe, and therefore available with

sudo apt-get install zssh

You need it on your ubuntu server and on your client, but basically when logged in with zssh, you just hit 'ctrl-@' and it brings up the "File transfer mode" which allows you to send files back down the pipe to your client machine, or upload them from client to server.

However, you don't have to re-auth or open a new window to scp.

If you're using ssh keys, and an ssh agent, you can quite easily do:

[enter]~[ctrl]-Z

Which will background ssh, and then just scp $!:/whatever/whatever .'

Once the file is transferred, fg to get ssh back.

If you aren't using ssh keys, you can still use the "ControlMaster" and "ControlPath" options added to recent OpenSSh versions, but that gets tricky, check man ssh_config

share|improve this answer
add comment

Assuming you're running an ssh server on your desktop (there are ways around this, but I think they all add complexity, and possibly have security problems), you can set up a reverse ssh tunnel. See SSH easily copy file to local system. over at unix.SE.

  • Type Enter ~C Enter -R 22042:localhost:22 Enter to create a reverse port forwarding from your server to your desktop (22042 can be any port number between 1024 and 65534 that's not in use).
  • Then scp -P 22042 foo localhost: will copy the file foo in your current directory on the server to your home on the desktop.
  • Now move the file into your current directory on the desktop by typing Enter ~ Ctrl+Z mv ~/foo . Enter fg Enter.

Ssh escape sequences begin with ~; the tilde is only recognized after a newline. ~ Ctrl+Z puts ssh into the background. ~C enters a command line where you can create or remove a forwarding.

share|improve this answer
    
Using 2 layers of encryption & compression is a bit overkill, that's why I suggested using rsh or ftp for the "back link". –  JanC Nov 16 '10 at 0:30
    
@JanC: That requires more setup: you have to install an rsh or ftp server, and make sure its configuration is secure. The overhead of encryption is minimal even on a netbook. –  Gilles Nov 16 '10 at 0:42
    
An ssh-server isn't installed by default either. ;) –  JanC Nov 16 '10 at 0:50
    
@JanC: Another advantage of ssh is that if you've enabled agent forwarding, you won't have to type a password to do the copy. Workflow efficiency over computing microefficiency. –  Gilles Nov 16 '10 at 8:23
    
You can do something like that with (some) other services too of course. In any case, there are multiple similar solutions that all involve setting up a tunneled connection back and an extra daemon... ;) –  JanC Nov 16 '10 at 8:56
add comment

I came up with a way to do this with standard ssh. It's a script that duplicates the current ssh connection, finds your working directory on the remote machine and copies back the file you specify to the local machine. It needs 2 very small scripts (1 remote, 1 local) and 2 lines in your ssh config. The steps are as follows:

1) Add these 2 lines to your ~/.ssh/config
ControlMaster auto
ControlPath ~/.ssh/socket-%r@%h:%p

Now if you have an ssh connection to machineX open, you wont need passwords to open another one.

2) Make a 1-line script on the remote machine called ~/.grabCat.sh
#!/bin/bash
cat "$(pwdx $(pgrep -u $(whoami) bash) | grep -o '/.*' | tail -n 1)"/$1

3) Make a script on the local machine called ~/.grab.sh
#!/bin/bash
[ -n "$3" ] && dir="$3" || dir="."
ssh "$1" ".grabCat.sh $2" > "$dir/$2"

4) and make an alias for grab.sh in (~/.bashrc or wherever)
alias grab=~/.grab.sh

That's it, all done. Now if you're logged in to "machineX:/some/directory", just fire up a new terminal and type
grab machineX filename

That puts the file in your current working directory on the local machine. You can specify a different location as a third argument to "grab".

Note: Obviously both scripts must be "executable", ie chmod u+x filename

share|improve this answer
    
Interesting, could you explain how this works? Are you able to extend this to support "putting" files as well as grabbing them? –  Peter Gibson Jun 22 at 0:55
add comment

If your client machine (the machine you are sitting at) is called machineA and the machine you are currently SSH'ed into is called machine B. MachineA, your local machine must have SSHD running and port 22 open. Then:

scp myfile machineA:

Copies myfile on MachineB to my MachineA home directory on machineA. This assumes userid/password are the same.

scp myfile machineA:/newdir/newname

Copies myfile one MachineB to /newdir/newname on machineA. This assumes userid/password are the same.

scp MachineA:/path/to/my/otherfile . 

Gets a copy of otherfile from my MachineA directory on MachineA and puts it in my current working directory on the MachineB machine (designated in standard UNIX fashion by the "dot" (.) character). This assumes userid/password are the same.

If the userid/password are not the same then use:

scp myfile user@MachineA: to get file.

scp user@MachineA:/path/to/my/otherfile . to put files

NOTES about SCP:

Just like the cp command, scp has a -p option to propagate the permission settings of the original file to the copy (otherwise the copy is made with the normal settings for new files), and a -r option to copy an entire directory tree with one command.

scp creates a completely transparent encrypted data channel between the two machines, so binary data (such as images or executable programs) is preserved correctly. This also means that scp is unable to perform automatic end-of-line termination conversion between different types of operating systems, as can be done with ftp in "ascii" mode. That will not be a problem when copying between Unix systems, which all use the same end-of-line convention.

share|improve this answer
add comment

This is not possible with a default ssh session, but you could use a script instead of ssh, that starts something like a simple ftp or rsh server on you local system and runs ssh with the necessary options to set up a tunnel back to your desktop for connecting to this server.

share|improve this answer
add comment

It isn't over the active SSH connection, but scp copies files using the same mechanisms and permissions as does ssh.

share|improve this answer
3  
I'm aware of scp but it's a very disjointed workflow. If I'm working in a given remote directory, I need to get the remote path, disconnect (or spawn another shell), do the scping writing out the paths and then reconnect. I'm looking for something that is akin to writing get file and it magically appears back on my local machine. If it has to do that via some service tunnelled over SSH, so be it.. But it should be session-bound. –  Oli Nov 15 '10 at 16:30
    
Plus it was superseded by sftp (see my answer) –  Olivier Lalonde Nov 16 '10 at 1:40
add comment

Haven't found any better way to do this (requires opening a second tab):

  • Copy the file path of the file you want to download.
  • Open a new terminal tab (Ctrl + Shift + T)
  • Type: `sudo sftp user@remotehost:/paste/remote/path/here local/path/here
share|improve this answer
add comment

if you access server via ssh, you get the ability to connect via sftp as well. Keep filezilla client (GUI) handy and paste the path you are currently on

enter image description here

share|improve this answer
    
This would benefit by greater detail, including how to connect to an SSH server with FileZilla (it's not obvious). Screenshots may also help, in addition to explanatory text. –  Eliah Kagan Sep 8 '12 at 6:49
add comment

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.