Persistent Reverse Tunnel using AutoSSH
When the local server is behind a firewall with blocked ports or has dynamic IP or basically unreachable from the internet, it is possible to access the server using this method.
This requires a VPS or server with open ports and fixed IP address.
Login to remote server (ubuntu in our example) and append to file using vi or nano:
1 | |
Then login to local server and install AutoSSH and SSH:
1 2 3 | |
Then generate a SSH key pair for your server:
1 | |
1 2 3 4 | |
Note that for bottom part, -p 2222 means ssh is running on port 2222 for "remote" server.
1 | |
Create a connection file on local server and grant it permission to execute, and finally open it for editing:
1 2 3 | |
1 2 3 4 5 6 7 8 | |
Exit and save by pressing Ctrl + x, followed by Y and Enter
Note that for above example, the line -R 8080:192.168.1.50:80 means remote port 8080 will connect to local port 80. If no http client is running on remote server, it is possible to forward port 80 from remote to port 80 on local.
Otherwise, it is also possible to run a reverse proxy such as nginx proxy or apache proxy to forward specific domains to the correct port.
Finally initiate the connection on boot-up via cron:
1 | |
1 | |
And reboot the server:
1 | |