1 min read

SCP tunneling through a gateway

SCP tunneling through a gateway

Let’s suppose you want to transfer a file from and to a remote server which is accessible only via a gateway (jump host), how would you do that?

Solution: This could be achieved through ssh tunneling as described below.

Steps:

  1. Assume that your server details are as per the below diagram.
Example network setup

2. Open a new terminal from your client PC (Laptop) and create a tunnel in the Gateway Server by executing the command below:

ssh -L <TempoaryPort>:<IP_RS>:<SSHPort_RS> <userId_GW>@<IP_GW>

Example: ssh -L 2222:172.16.1.1:22 [email protected]

3. Open another terminal and try to ssh through the tunnel you’ve just created. Once you’ve established an ssh tunnel, you could access the remote server as localhost.

ssh -p <TemporaryPort> <userId_RS>@localhost

Example: ssh -p 2222 rsuser@localhost

4. Similarly, you could use SCP command to transfer the file as below.

scp -P <TemporaryPort> <rsuser>@localhost:<path_to_file><local_path_to_copy_file>

Example: scp -P 2222 rsuser@localhost:/home/rsuser/file1.txt .