# How to download a file on a server you've already ssh'ed into?

I know how to use scp or wget to download a file on a remote server to my local machine. However, if I'm already logged into a server with ssh, is there a command that lets me download a file in the pwd on the server onto my local machine?

I suppose I can use scp, but my local machine is usually behind a router. Would I have to open a port in the router?

• That's called "upload" :) – kolypto Dec 15 '09 at 0:16

## 6 Answers

What you need to answer is why you don't want to log again ... if you logged once, you can log twice ... especially if you've taken the three minutes it takes to create a key and store the key remotely:

$ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/user/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/user/.ssh/id_rsa. Your public key has been saved in /home/user/.ssh/id_rsa.pub. The key fingerprint is: 2b:4c:11:ab:53:d1:15:90:4f:88:a1:42:da:c5:1c:98 user@localhost$ ssh-copy-id user@remotehost
Warning: Permanently added 'remotehost' (RSA) to the list of known hosts.
Password:
$ssh user@remotehost Last login: Mon Dec 14 21:52:45 2009 from 1.2.3.4 [user@remotehost]$


Back in the times before TCP/IP was commonplace, people used kludges such as X/Modem to handle this. Using this nowadays is kinda silly.

• so how do I log in next time...will it "just work" with out the password when I try to login? – mjrider Jun 1 '11 at 19:15
• Yes, though it will ask for a passphrase if you set one up — which is optional. However you can use ssh-agent so that you only have to enter it once per session. – niXar Jun 8 '11 at 9:25

It's a little archaic, but you may be able to use something like kermit to use a modem-era protocol (zmodem, etc.). Looks like there's a program meant just for that purpose, too.

I once needed to download a small-ish file from a remote unix server without any supporting tools, so I uuencoded the file, dumped it with cat to the terminal, and then captured the the resulting text with my local terminal program, where I uudecoded it. Sick, eh? :)

• I feel a little odd voting this one up...but I had the same thought as a solution to this question :) – Alex Dec 15 '09 at 1:20

SCP is the right tool for the job. Just initiate the scp from the local machine, so you would use:

scp user@remotehost:/path/to/file/filename ~/

...which would put the file on your local box in your home directory.

If you want a point and click option you could use Konqueror and the fish protocol. Just open Konqueror and in the address bar write fish://user@remote with user being the the user you wish to log in as and remote being the remote computer.

• Can't Konqueror use sftp instead? In Gnome you can use sftp:// urls; gvfs will automagically mount the remote host with fuse/sshfs for apps that don't support gvfs directly. – niXar Dec 15 '09 at 1:06
• Yes it can but fish is the first one I used in Konqueror so it is what always comes to mind first. They are both secure so i am not sure of the differences. – Nunya Dec 23 '09 at 14:06

I came up with a way to do this with a standard ssh client. 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. From now on 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

One way to do this would be to set up port forwarding when you make your initial ssh connection to the remote machine. Do this:

$ssh -L 9999:remote:22 remote where remote is the name of the remote machine. then in a separate terminal on your local machine you could do this:$ scp -p 9999 localhost:somefile somefile

to copy a file from the remote machine, over the existing ssh connection from step 1, back to your machine.

I realize this doesn't exactly answer your question but I hope it give you some idea of how to go about this. Read up on ssh port forwarding for more details.