The Chalkboard


Transfer data to your local machine over ssh using a reverse tunnel

Last Updated: [2024-07-19 Fri 04:49]

Introduction

Sometimes you'll find yourself needing to copy large amounts of data to your local machine from a remote server running Linux.

Here's a little handy, dandy, way of setting up a reverse tunnel and using that to copy data to your local system running macOS or Linux (or Windows using WSL).

Local config changes

Firstly, you'll want to add the following to your ~/.ssh/config file:

RemoteForward 127.0.0.1:2222 127.0.0.1:22

This will set up a reverse tunnel automatically when you connect giving you access to your own machine on port 2222.

Next, you need to ensure you have OpenSSH server installed and running. This will vary from system to system, so looking at your distribution's documentation is recommended. On macOS you'll need to enable Remote Login from: System Settings -> Sharing -> Remote Login

Copying something to your local machine

Next, whenever you want to send anything to your local machine, you can use something like the scp command, or even pipe data to an application on your machine through the ssh command:

scp -P 2222 <somefile> [email protected]:~

In this example, you'll want to change the user to the username on your local machine. First time you run the command, you'll be asked to confirm the remote machine's thumbprint, then your local user's password.

Copying something from stdout to your machine's clipboard

A very useful trick is to copy data from stdout to your local clipboard. On macOS, pbcopy can be used as an applicaiton to recieve data to place on the clipboard. Under Linux, xclip (for X11 sessions) or wl-copy (part of wl-clipboard for Wayland sessions) can be used instead. Finally if you're using WSL on Windows, you can use clip.exe:

cat <somefile> | ssh -p 2222 [email protected] pbcopy

Then on your local machine, you can simply paste the data into another application.

Again, you'll want to change user to your username. You may be asked to confirm the machine's thumbprint if it's the first time you've connected to your loopback tunnel, then you'll be asked for your local password.


DISCLAIMER: The information provided on this website is generated from my own notes and is provided "as is" and without warranties. Robert Ian Hawdon can not be held responsible for damages caused by following a guide published on this site. This website contains links to other third-party websites. Such links are provided as convienice of the reader. I do not endorce the contents of these third party sites.