Skip to content

Instantly share code, notes, and snippets.

@harshilpatel312
Last active August 25, 2021 21:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save harshilpatel312/b327f7bc99499a4c768623db40121f7f to your computer and use it in GitHub Desktop.
Save harshilpatel312/b327f7bc99499a4c768623db40121f7f to your computer and use it in GitHub Desktop.
Alternative to VPN: reverse-ssh + autossh

Description

You want to access a remote computer from a local computer. The remote computer does not have a public IP, is on its own local network and can access internet.

Solution: To access the remote computer, you need a middleman (proxy server) which is accessible by both, the remote and the local computer. You open an ssh tunnel from remote to middleman. Use that ssh tunnel on local to access the remote computer.

Method

  1. Create VM on a cloud service (like https://www.digitalocean.com/products/droplets/). Get its public IP and password.
  • As a test, access the VM from local computer: ssh hkpatel@203.162.43.12. If successful, proceed. If not, inspect.
  1. On remote computer:
  • Get username of remote computer (use this in Step 3.): whoami

  • Install autossh: sudo apt install autossh

  • Copy remote computer's ssh keys to proxy server: ssh-copy-id ~/.ssh/id_rsa.pub VM_USER_NAME@VM_PUBLIC_IP

  • Open ssh tunnel: autossh -R VM_PUBLIC_IP:AUTOSSHPORT:localhost:22 'VM_USER_NAME@VM_PUBLIC_IP'

    Example: autossh -R 203.162.43.12:5090:localhost:22 'hkpatel@203.162.43.12'

  1. On local computer:
  • Get remote computer's username.
  • Access remote computer via the ssh tunnel: ssh -p AUTOSSHPORT REMOTEUSERNAME@VM_PUBLIC_IP. Example: ssh -p 5090 remoteusername@203.162.43.12
  • Enter remote computer's password and it should work.

Debug

If Step 3 says "Connection refused", ssh into proxy server first and ssh REMOTEUSERNAME@localhost -p 5090

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment