Remote Debugging Java over SSH

In most of the production environments, or even in testing environments you won't get lots of open ports in the computing nodes. Generally you will get just a single port (22) open for ssh. Even if you request the system admins to open the ports for you, they hesitate to do that due to security concerns.

Here is what you can do on such occasions to debug your java application.

Step 1 : Start your java application with xdebug flag as you would do on your local machine.

 java -jar <path_to_jar> -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5006  

Step 2 : Open an SSH tunnel from your local machine to the remote server.

 ssh -L 8000:localhost:5006 <username>@<remote-host>  

Looks confusing? Here is an explanation to the SSH tunneling command.

 ssh -L <local_port>:<host_relative_to_the_remote_server>:<remote_port> <username>@<remote-host>  

Now, you have created a secure tunnel between port 8000 of your local machine and port 5006 of your remote machine.

"If you send a 'flower' to port 8000 of your local machine, remote machine will receive that flower on port 5006. If remote machine post back a 'chocolate' to its port 5006, you will receive that chocolate through port 8000 of your local machine"

If you still don't get it, try below command.

 ssh -L <username>@<remote-host>

Now try http://localhost:8000 in your browser. You will see the Google's 404 page.

Step 3 : Now use your favorite IDE to connect to the debug session at localhost:8000.

Enjoy debugging!