I want to copy a file from my machine A to server C, but only have access to server C through server B.
Instead of first transferring to server B, log in and then transfer to server C, Is is possible to transfer the file directly with SCP or similar programs?
(Emacs tramp-mode has this feature for editing files remotely).
You can add
-o
options toscp
instead of.ssh/config
.$jump_host
is your "server B" in this case.Assuming OpenSSH, add to your SSH configuration in .ssh/config
This will cause SSH to be able to connect "directly" to the machine named distant by proxying through the machine named near. It can then use applications like scp and sftp to the distant machine.
For this to work you need 'nc' aka netcat installed on the machine named near. But a lot of modern systems will have it already.
towo's tar solution is more effective for one-shot problems, assuming you've memorised tar's syntax and rules of operation.
With more recent versions of ssh on the server near (B) machine the following will work without netcat:
It will however require AllowTcpForwarding to be yes (the default) on the near (B) machine
edit: requires OpenSSH 5.4+ on B
You can ssh to server B using something like
Then you can ssh to server C using
Similarly scp would work using
Remember to use correct case of p with scp and ssh
It's possible and relatively easy, even when you need to use certificates for authentication (typical in AWS environments).
The command below will copy files from a
remotePath
onserver2
directly into your machine atlocalPath
. Internally the scp request is proxied viaserver1
.The other way around also works (upload file):
If you use password authentication instead, try with
If you use the same user credentials in both servers:
The
-W
option is built into new(er) versions of OpenSSH, so this will only work on machines that have the minimum version (5.4, unless your distro back-ported any features; e.g., RHEL6 OpenSSH 5.3p1 includes this feature). Per the release notes: http://www.openssh.com/txt/release-5.4%h
and%p
are placeholders for the host and port.This isn't scp (which OP requested), but I found it super simple to use
rsync
to copy from local to remote over a single hop with:Source: http://mjbright.blogspot.com/2012/09/using-rsync-over-multi-hop-ssh.html
I had tried the -o ProxyPass suggestion above and didn't want to change config for my changing needs. As the author in the link above states, the destination file preceding colon (:) is important to indicate the specified path is on the destination server. Also, using rsync, you have the options of date compare, folder sync, etc. I hope this helps someone!
If you want to be really wicked, you could chain ssh and tar, something like
tar c mydir | ssh server "ssh otherserver | tar x"
, but this can run into all hands of problems.The easier way would be just to set up an SSH tunnel with the built-in methods of SSH; look at the
-D
switch in the manpage and just forward some port to the other server's ssh port.You can also do this in reverse and is maybe easier.
Supposing you have an ssh session opened with the machine you want to send the file to. This farthest-hop PC, we'll call this hop2. Your "proxy" host will be hop1. The PC that is file-origin, we'll call that origin.
You can build tunnels making a local port available on a remote PC. We're thereby defining a port to open on the remote PC, which will be a redirect to the port you pulled over with you when you built the tunnel.
On hop2:
Now in that opened tunnel session, you can do the same from hop1 to file_origin.
On hop1:
You are now tunneled from hop2 to hop1 to origin. Coincidentally, now both port 5555 and 6666 are open on origin, which are redirects to hop2's port 22. Within this session, both of the following are valid scp routes to hop2:
On origin:
In this way, you can have some arbitrary number of hops in between, and it's easier to work with in terms of chaining together more than two hops.
Try adapring the following example openssh config for a setup that can be used for multiple hosts:
This presumes a set of servers that begin with "uat-" that are only accessible via the jumpbox/gateway server "bastion-uat". You probably also want to add
ForwardAgent yes
if you are using a key to login.scp -o 'ProxyJump jumpboxname' somefilename.txt finaldestinationhost:/tmp/.