Thursday, 22 November 2012

How to port forward remote port to local machine using ssh [trouble shooting remote server apps in local machine]

Some times, you may need to test a remote application (GUI app) which is running in a linux server. As it is a server, we cannot test the app from the server as there is no GUI for the server. The app can be accessed over a web browser though. But for some other reasons, you are not able to access it through the web browser using the server's ip address. So our first purpose is the trouble shoot the issue. Whether the app is actually running in the server with all the features enabled?

We can ssh to the server and check the whether the app is running using ps -ef | grep appname command. Then we found out that the app is actually running in the server. But why we are not able to access it through server's ip address and the port? Here comes the rescuer in the form of port forwarding.

Yes, we can forward the remote hosts port to our local machine and we can access the interface from our web browser. For example, we need to check whether our web server which is running in the port 80 in the remote server is working fine.

So we will forward the port 80 of remote host to our local machine's port 9000 and access it through the web browser. Here is the command.

ssh [email protected] -L9000:localhost:80

Here the -L will forward the remote host port 80 to our local machine's port 9000. Now we can access it from our web browser. Open a web browser and type localhost:9000 in the address bar. Boom !! Now you can see your remote app running as local app in your web browser.

port forwarding a remote app to local machine using -L option in ssh. The app is actually running in remote server and not in my local machine.

This command has saved me many times as I was able to trouble shoot the issue with some of the apps in our server. The apps were actually running fine on server but was not able to access it through the server's ip address or domain name. I have confirmed it by port forwarding the app to my local machine.

I hope this will come handy for some other users also. Please let me know if you know more ssh tips and tricks in commands and similar situations you have faced.

No comments:

Post a Comment