Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. # SSH Reverse Tunnels
  2.  
  3. SSH ->
  4. Local Host (NAT/Firewalled) --> Internet --> Remote Host
  5. <- Reverse Port Forward
  6.  
  7. ## Basic Usage
  8.  
  9. 1. Create an ssh key with no passphrase: `ssh-keygen -f ~/.ssh/redir -t rsa -N ''`
  10. 2. Add `~/.ssh/redir.pub` to remote host's `~/.ssh/authorized_keys`. If using lower ports, do this for `root` user.
  11. 3. Create the tunnel: `ssh -i ~/.ssh/redir -R *:80:localhost:80 root@remotehost`
  12. 4. Now any traffic on remote host's port 80 will ride ssh back to 127.0.0.1:80 of the Local Host
  13. 5. If it isn't working, make sure ports are not already in use with `netstat -plant | grep <port>`
  14. 6. If that doesn't work, check ingress firewall/security group rules for remote host
  15.  
  16. ## Daemonized Tunnels
  17.  
  18. Here's a basic tunnel that will be daemonized in the background:
  19.  
  20. `ssh -i ~/.ssh/redir -fnNT -R *:80:localhost:80 root@remotehost`
  21.  
  22. If you want a lot of control over the daemonized tunnel , like checking the status or stopping it on command, you can use a control socket:
  23.  
  24. * Start the tunnel: `ssh -i ~/.ssh/redir -M -S my-socket -fnNT -R *:80:localhost:80 root@remotehost`
  25. * Check the status: `ssh -S my-socket -O check root@remotehost`
  26. * Close the socket: `ssh -S my-socket -O exit root@remotehost`
  27.  
  28. ## Config File Settings
  29.  
  30. Add the following to ~/.ssh/config
  31.  
  32. Host tunnel
  33. HostName remotehost
  34. IdentityFile ~/.ssh/redir
  35. RemoteForward 80 localhost:80
  36. user root
  37.  
  38. Now you can start your tunnel with `ssh -fnNT tunnel`
  39.  
  40. We can also make some aliases to add to `~/.bashrc` or `~/.bash_aliases` (depending on your setup):
  41.  
  42. alias tunnel-start='ssh -M -S my-socket -fnNT tunnel`
  43. alias tunnel-check='ssh -S my-socket -O check root@remotehost
  44. alias tunnel-stop='ssh -S my-socket -O exit root@remotehost
  45.  
  46. ## Initialize on Startup
  47.  
  48. Let's tie this all together into a small bash script that will run on startup and reconnect if the tunnel is down
  49.  
  50. #!/bin/bash
  51. cmd="ssh -fnNT tunnel"
  52. while true; do
  53. pgrep -fx "$cmd" >/dev/null 2>&1 || $cmd
  54. sleep 10
  55. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement