Skip to content

Instantly share code, notes, and snippets.

@xeb
Last active March 17, 2023 14:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xeb/38f153eb397530c26b20 to your computer and use it in GitHub Desktop.
Save xeb/38f153eb397530c26b20 to your computer and use it in GitHub Desktop.
Reverse SSH

SSH Reverse Proxy

Some simple notes to setup SSH reverse proxy

  1. on source machine (behind the network), run: ssh -N -R 2222:localhost:22 YOURUSER@YOURSERVER

  2. on YOURSERVER, run: ssh -l SOURCEUSER -p 2222 localhost

  3. If you want to automated it, do something like...

#!/bin/bash
createTunnel() {
  /usr/bin/ssh -N -R 2222:localhost:22 serverUser@25.25.25.25
  if [[ $? -eq 0 ]]; then
    echo Tunnel to jumpbox created successfully
  else
    echo An error occurred creating a tunnel to jumpbox. RC was $?
  fi
}
/bin/pidof ssh
if [[ $? -ne 0 ]]; then
  echo Creating new tunnel connection
  createTunnel
fi
  1. Get the right permissions chmod +x create_ssh_tunnel.sh

  2. Cron it up! crontab -e then jam in:

*/1 * * * * ~/create_ssh_tunnel.sh > tunnel.log 2>&1

Consider adding a sleep $(( ( RANDOM % 10 ) + 1 )) if you'd like the tunnel re-establishment to be a little less deterministic. Someone could notice an SSH session opening every minute on the minute. Mix it up to look human.

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