How to connect VisualVM to directly invisible server using single port


I would like to check JVM state on my server using VisualVM and JMX. But I can’t connect directly to it because is behind firewall or in inaccessible network. Luckily I can connect through SSH to machine which can access my server.



Naïve setup

I need to enable JMX for my application on server. Add following parameters into java command line of application:

Check that you can connect to your application with this setting when debugging on local computer using JMX:


Naïve solution

OK, so it works locally, great! Now try to deploy your app to server and try to run ssh with port forwarding from your computer:

ssh -L 9872: user@

So try to redirect port 9872 on server through tunnel between my and mediator computer to my local port 9872. But when I try to connect to localhost 9872 I get:


The Problem

Beside communication on JMX port (in my case 9872) VisualVM tries to communicate by random other port using RMI. But because only one port was redirected this fails.


Working setup

So let’s try to add 2 more parameters that will fix interface for RMI to So only one port will be used:


I use because that should work on all computers and therefore this setting is suitable for installation package. If you can use fixed IP address you can avoid nesting of SSH tunnel below. But usage of loopback interface is more secure because should not be visible from outside of machine.

Working solution


So now lets try to do nested port forwarding, please check that port 9872 is available on both mediator and your computer. If not, choose different one. On my computer launch:

ssh -L 9872:localhost:9872 user@

That will open tunnel between my computer and mediator. Then on mediator (you can use opened console from command above) execute:

ssh -L 9872: user2@

And that will open tunnel between mediator and server. Add JMX connection to localhost:9872:


And voilà:



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s