Friday, 23 November 2012

How to enable internet connectivity to remote server which is not connected to internet using port forwarding [port forward local port to remote machine using ssh tunneling]

Last time we saw that we can port forward the remote port to local machine and use it for testing the apps in the remote server. The opposite of this is also true. Yes, we can port forward the local port to remote server and use it for internet connectivity in the remote machine.

This trick comes handy if the remote server is behind a firewall or in a VPN and there is no direct connectivity to the internet. So what we will do if we need to update the applications in the server. Or what if we need to install a critical security update to the server? Here again comes the port forwarding to our rescue.


For this we need to have the program squid installed in our local machine and remote server. Squid is not only a proxy server but here we use it as a proxy server for this purpose. The default squid port is 3128.

You can install squid using the command

sudo apt-get install squid3

Once it has been installed, you can start it by sudo /etc/init.d/squid3 start command. Now squid will be running in the port 3128 in our localmachine. Now we need to ssh to the remote server using the command,

ssh [email protected] -R9000:localhost:3128

Once you have logged in to the server, we need to export the environmental variable http_proxy to the tunneled port to enable the internet connectivity.

export http_proxy=http://localhost:9000

Now check whether you are able to connect to internet by either pinging to any website or open a text only browser like links and open any web page. Here the -R option in the ssh has done the trick.

-R will port forward the local port 3128 (squid port) to remote machine to remote port 9000. You can not only use it for enabling internet connectivity but you can find many other use cases for this. Please let me know in the comments if you use ssh tunneling for other use cases.



No comments:

Post a Comment