Is there any way to transfer files from a server to a host computer, from within the server ssh connection? I am storage limited on an HPC, and so some jobs require transferring and then deleting files at intermediate stages. It would be great to be able to transfer the files to my host machine from within a job, but I think this might not be possible, given that the unix within an ssh connection does not know that it is an ssh connection.

  • 2
    You can use SFTP for secure file transfer over SSH. Note that it will not use your current session, but rather create a new connection/session. – Goodies Jan 11 '18 at 22:45
  • @Goodies: I would say that classifies as an answer... :-) – Fabby Jan 11 '18 at 23:09
  • "Unix within an ssh connection doesn't know that it is an ssh connection"...not sure if this is what you mean but there are environment variables set on the remote end when you are connected via ssh. $SSH_CLIENT and $SSH_CONNECTION for instance. And they both contain the IP address to reach the local/originating host. Try echo "${SSH_CLIENT%% *}" to display the IP. – B Layer Jan 11 '18 at 23:25

As B Layer pointed out whenever, you are in a ssh tunnel your command/shell doesn't really know this. Basicly your command/shell works just like it wasn't in a tunnel (the beauty about ssh). That's also why tunnels inside tunnels work!

Goodies gave the answer you were looking for I think. SCP is a very nice tool using ssh to copy files in both a pull and push way depending your situation.

This link gives a very nice overview of the possibilities and the syntax of SCP!

Code you could use: where is your server and your client. Note that dns-names can be used as well.

Pulling: Let's say you have a terminal on your host open, you could do:

scp user@ /tmp/files/myfile

This way you are creating a "second tunnel" towards your server.

Pushing: (The other way around) so from your server to your host (essentially it is now a tunnel in your original tunnel):

scp /tmp/myfile user@

I hope this explains the concepts a little!

  • Thanks for this suggestion. My host is an virtualbox ubuntu VM running within windows. At the terminal, I see "nb80a@nb80a-VirtualBox:~$". I have tried both nb80a and nb80a@nb80a-VirtualBox in place of "user" in your second suggestion using the address acquired from "${SSH_CLIENT%% *}". I haven't gotten it to work yet. Any ideas? – nb80a Jan 12 '18 at 16:13
  • Your virtualbox shell shows that your user is indeed nb80a on host nb80a-VirtualBox. How is your vm connected to your host? Has your vm his own ip address? Did you use the right one? For and in depth answer you should give a clear overview of your machines, users and ip. Do you have a client on your windows like putty, git bash, bash from wsl? – Ciberth Jan 12 '18 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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