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 message:


(“Cannot connect to localhost:9872 using service:jmx:rmi:////jndi/rmi://localhost:9872/jmxrmi”)

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à:


Tags:  Java  VisualVM  SSH 
Last blog articles:
Mým fuckupům
Ubuntu: Login loop
Leaving LinkedIn
LinkedIn: Odcházení
git gui: Add pull and stash commands

Java Ubuntu česky Gradle personal Software-architecture DIY children React Eclipse DisplayLink AI Spring JavaScript AspectJ neuron SSH LXC JavaFX Immutables Gnome3 GIT FatJar Edison technologies startup science procrastination pokusy podnikání linux lambda kvas ellipsis destilace chemie alkohol VisualVM Upstart Tomcat Selenium REST PrimeFaces Log4j Jooq Jackson JSF Debug DI CSS

Last tweets: