Copying files over chained SSH connection

I am using a remote server and want to copy files from the server.
When using the server, I need to log in with 2 steps.
ssh username@A
after loging into server A, I need to log in again.
ssh username@B.
So I am in server B now. (by uname -a)
Is there anyway to download files in server B to local computer?
I tried sftp and scp, but I don’t know how to specify the server name.
Both of username@A and username@B have been tested, which don’t work.
Any suggestion will be appreciated.

Answer

First fire up an SSH tunnel from your local machine to Server B, through A:

$ ssh user@serverA -L2222:serverB:22

Then from another terminal, scp directly to/from B:

$ scp -P 2222 /path/to/file user@localhost:/path/to/destination

Note that due to port tunnelling, user@localhost is actually user@serverB, so make sure to use the appropriate username for that remote server.

Attribution
Source : Link , Question Author : user1503 , Answer Author : EEAA

Leave a Comment