Remote debug of a Java App using SSH tunneling (without opening server ports)
Sometimes production code misbehave and it’s complex to replicate the same conditions on test/stage environment. We have almost all ports of our server closed (as it should be), so IMHO the best option is to open a ssh tunnel.
##
This is my receipt:
On the server I start the java virtual machine with debug parameters:
java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=9000 \
-jar myproduct-jar-with-dependencies.jar &> console.out &
You can check the VM is listen to connections using netstat
:
$ netstat -an | grep LISTEN
tcp 0.0.0.0:4949 0.0.0.0:* LISTEN
tcp 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0.0.0.0:9000 0.0.0.0:* LISTEN
tcp 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp :::80 :::* LISTEN
tcp :::22 :::* LISTEN
On the development machine I open the tunnel with the server, having only SSH(22) port opened
ssh -f [email protected] -L 9000:127.0.0.1:9000 -N
The -L
parameter is a little bit confusing, the syntax is -L <local-port>:<remote-host>:<remote-port>
so basically what we are doing here is saying:
- Listen on local (develop machine) port 9000
- Forward any connection to localhost, port 9000 of the remote machine (server)
Now everything is configured you can attach your IDE to remote server:
- Open your Eclipse
- Go to
Run > Debug Configurations
- Create a new
Remote Java Application
- Configure
Host: localhost
andPort: 9000
- Hit Debug button
- When you finish your job, just disconnect
###
Happy Debugging!
##
References
[http://www.revsys.com/writings/quicktips/ssh-tunnel.html](Quick-Tip: SSH Tunneling Made Easy)