14

I can't sftp directly into a particular host. To move a file from my home machine to the host, I must sftp a file to an intermediate host; ssh into the intermediate host; and sftp the file to the final destination. Is it possible to avoid such madness?

5 Answers 5

28

From your local machine you can create am SSH tunnel through the intermediate host to the final host:

ssh user@intermediate -L 2000:final:22 -N

This will open port 2000 on your localhost that will connect directly to the final server on port 22, by tunneling through the intermediate host. Now in another prompt connect with sftp on port 2000 to be tunneled through to the final server, noting that the user specified here is for the final host:

sftp -P 2000 user@localhost

Seems like this belongs on superuser.com or serverfault.com though.

3
  • Thanks, I'll try that; and you're right it is the wrong forum; sorry.
    – Anonymous
    Mar 26, 2011 at 22:58
  • After giving the user@localhost password I received the message: Received message too long 1131376238
    – user74094
    Mar 29, 2011 at 23:14
  • Many versions of sftp use -P for the "sftp_server_path", rather than the port. But sftp -o Port=2000 should work. Although if your sftp is new enough, it's easier just to use -J for a ProxyJump as per the other answer.
    – mwfearnley
    Dec 15, 2022 at 11:24
19

Since I originally wrote this answer, OpenSSH has added an easier way to do this. If you have at least OpenSSH 8.0 on your home machine and 7.3 on the intermediate host, you can use the new jump host (-J) option:

sftp -J user@intermediatehost user@finalhost

If you have at least 7.3 on both the home and intermediate hosts, you can do the same thing with the ProxyJump option in your ~/.ssh/config file:

Host finalhost
    ProxyJump user@intermediatehost

With that entry, you can use sftp, scp, and ssh to finalhost, and it'll automatically invoke the tunnel. The only nontransparent part is that it'll prompt for two passwords (intermediatehost followed by finalhost), but if you want you can eliminate that as well with SSH keypairs.

If you don't have new enough versions of OpenSSH, you can still use the ProxyCommand option to do essentially the same thing:

sftp -o "ProxyCommand=ssh -e none user@intermediatehost exec /usr/bin/nc %h %p 2>/dev/null" user@finalhost

(That's assuming the intermediate host has netcat installed as /usr/bin/nc -- if not, you may have to find/install some equivalent way of gatewaying stdin&stdout into a TCP session.)

And the equivalent ~/.ssh/config option:

Host finalhost
    ProxyCommand ssh -e none user@intermediatehost exec nc %h %p 2>/dev/null

See the OpenSSH Cookbook on Wikibooks for more info.

5
  • Thanks Gordon. It takes both passwords then halts with the message: Received message too long 1131376238
    – user74094
    Mar 29, 2011 at 23:08
  • It sounds like something in the connection -- maybe a login script at one of the hosts, maybe nc -- is writing some extra text over the connection that's confusing sftp (see this FAQ at snailbook.com). 1131376238 is the decimal encoding of the ascii characters "Conn", so it's probably a message like "Connecting to..." "Connected from..." or maybe "Connection failed". Try it with ssh instead of sftp, see if the message is printed visibly and maybe you can tell where it's coming from. Mar 29, 2011 at 23:42
  • ssh worked fine. I should also mention that I am often faced with this question:The authenticity of host 'xxxxx (<no hostip for proxy command>)' can't be established. RSA key fingerprint is 37:40:d4:c7:etc.. Are you sure you want to continue connecting (yes/no)? yes
    – user74094
    Apr 1, 2011 at 23:49
  • Gordon, very useful! I'm adding this to my text file of command line fu oneliners. Thanks a ton! May 15, 2012 at 19:48
  • Is the exec necessary? (It works fine here without that part.)
    – equaeghe
    Feb 9, 2016 at 13:37
3

You can pipe data to the ssh process running on your machine, then run a command on the intermediate machine which reads stdin and sends it to sftp as appropriate.

This can be done in a oneliner on your local machine, though the quoting of arguments to ssh will require care. I am on my phone right now so unfortunately cannot type the details. Perhaps somebody else can complete this answer as an exercise!

0

I'm assuming the final host is firewalled and I can only guess at methods you could use to go around it.

For example - expose ssh from your local machine, then ssh to the first host, then ssh to the second and sftp from the final host to your machine.

0

lets say A and B are the first and second hosts. And the file to be copied is foo

Instead of sftp, you can use the following

cat foo | ssh A "cat - > foo"

Now, you can daisy-chain 2 of these together

cat foo| ssh A "cat - | ssh B \"cat - > foo\" "

1
  • I like it, so I tried it. It took the password for A, then produced the message: Host key verification failed.
    – user74094
    Mar 29, 2011 at 23:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .