How to copy a file from a two layer ssh linux
2
0
Entering edit mode
2.0 years ago
Alex S ▴ 20

I am logged into server B hosted on server A, so I need to ssh first server A and then B. I need to copy (scp) a file from server B to my local machine. Anyone knows how to do it without having to copy from B to A and then to my local server? All the servers are Linux machines.

Here is the code I'm currently using:

scp user@serverB:file user@serverA then scp user@serverB:file local-folder

scp linux ssh • 1.1k views
ADD COMMENT
1
Entering edit mode
2.0 years ago
GenoMax 147k

Anyone knows how to do it without having to copy from B to A and then to my local server?

If server B is only accessible (ssh/scp/sftp) via server A then you can't copy the file directly from server B.

ADD COMMENT
1
Entering edit mode
2.0 years ago
Jesse ▴ 850

One thing you could do is create an SSH tunnel from your local system through server A into server B, and then do the copy through the tunnel. To GenoMax's point, you're still not really copying it directly, and it'd be less efficient layering SCP on SSH like that... but at least it'd feel like a direct copy, so, maybe convenient.

For example, making port 9999 on your computer point to port 22 on server B via server A, and then copying a file through the tunnel, with both commands on your local side:

$ ssh -L 9999:serverB:22 serverA # in a separate shell, or with -f and a command; see TCP FORWARDING in man ssh
$ scp -P 9999 127.0.0.1:file-on-server-B .

Feels a bit dumb to SCP-over-SSH though and I wonder if an SCP feature might let you simplify it. If all three (your local system included) are running sshd, you could maybe flip it around and make server A the "local" system and the other two "remote". On server A:

$ scp -3 serverB:file-on-server-B your-computer:

(The -3 tells it to bounce the network traffic through the local system instead of trying to transfer directly, which would fail for your case.)

ADD COMMENT

Login before adding your answer.

Traffic: 1853 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6