reverse ssh tunnels

2020-08-04 • 2020-04-10


You have 3 machines:

The problem is that home is behind some network you don't control. You can use a reverse ssh tunnel to get access. We will use 22222 as our example port on vps.

1 From home

ssh -R 22222:localhost:22 username@vps

This will set a shim on vps:22222 -> home:22 through the ssh connection. Some more detail and visualizations may be found here.

2 From laptop

# to access home after ssh'ing into vps:
ssh localhost -p 22222

# to access home in one go from laptop:
ssh -t vps 'ssh localhost -p 22222'

# to Jump from the laptop (-J is the jumpbox target):
ssh -J username@vps:22 localhost -p 22222

3 TRAMP syntax

emacs TRAMP syntax for jumping across a local reference on a different port:

find-file /ssh:username@vps|ssh:username@localhost#22222:/path/to/some/file

and then here's a cool trick for providing the connection info to a remote shell through the environment with $TRAMP_INFO: (this will also do the jumping for you!)

(let* ((tramp-connection-path "/ssh:username@vps|ssh:username@localhost#22222:")
        (default-directory (concat tramp-connection-path "/home/username"))
        (process-environment (cons (format "TRAMP_INFO=%s" tramp-connection-path) process-environment)))
  (shell))

4 Other