Search code examples
scp

How to SCP a file from a 2-deep connection


Say I SSH into a server Server1 and from there SSH into server Server2 which is only accessible from a connection to Server1. Below simulates the example terminal commands for this behaviour:

[name@mylaptop]$ ssh user@Server1
user@Server1's password:
*** Welcome to Server1! ***
[user@Server1]$ ssh user2@Server2
user2@Server2's password:
*** Welcome to Server2! ***
[user2@Server2]$ 

Now I have a file, named file.txt in my home directory on Server2:

[user2@Server2]$ ls
file.txt
[user2@Server2]$ 

Is it possible to use scp to copy file.txt from Server2 onto mylaptop with a single command (i.e. not needing to first copy the file to Server1)?

In other words, can this be done easier than the following:

[name@mylaptop]$ ssh user@Server1
user@Server1's password:
*** Welcome to Server1! ***
[user@Server1]$ scp user2@Server2:~/file.txt .
user2@Server2's password:
file.txt                                            100%  690     0.7KB/s   00:00
[user@Server1]$ logout
Connection to Server1 closed.
[name@mylaptop]$ scp user1@Server1:~/file.txt .
user@Server1's password:
file.txt                                            100%  690     0.7KB/s   00:00
[name@mylaptop]$ ls
file.txt

Solution

  • Try the answers on ServerFault : https://serverfault.com/questions/37629/how-do-i-do-multihop-scp-transfers.

    The answers cover a variety of flavours of ssh.