Remote Monitoring with JMX and jConsole: A Step-by-Step Guide
Java Management Extensions (JMX) provides a way to monitor and manage applications running on the Java platform. This guide will walk you through connecting to a remote JMX-enabled apache tomcat application.
I will also try to show you how you can connect to a remote JMX in a secure way as well.
Step 1: Update the bin/setenv.sh on the Remote Machine where the Tomcat is running
CATALINA_OPTS="
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=20000 \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false \
-Djava.rmi.server.hostname=<your-host-name> \
-Dcom.sun.management.jmxremote.rmi.port=20000"
Explanation:
-Dcom.sun.management.jmxremote.port=20000
: Specifies the port for JMX remote monitoring. I used 20000 as the port number for JMX-Dcom.sun.management.jmxremote.rmi.port=20000
: Ensures RMI registry uses the same port.-Dcom.sun.management.jmxremote.authenticate=false
: Disables authentication for simplicity (not recommended for production).-Dcom.sun.management.jmxremote.ssl=false
: Disables SSL for simplicity.
🔒 Note: While this configuration simplifies setup, it’s recommended to enable authentication and SSL for production environments.
Step 2: Open the Remote Port in the Firewall
Ensure the remote machine allows traffic on the JMX port. Use the following commands to open the port:
ufw allow 20000
ufw allow 20000/tcp
ufw allow 20000/udp
ufw reload
Explanation:
ufw allow 20000
: Opens port 20000 for both TCP and UDP.ufw allow 20000/tcp
: Specifically allows TCP traffic.ufw allow 20000/udp
: Specifically allows UDP traffic.ufw reload
: Applies the updated firewall rules.
Step 3: Launch `jconsole` Locally
Now, you can connect to the JMX service using `jconsole`. Run the following command:
jconsole <your-host-name>:20000
Alternatively, open jconsole
from your terminal or application menu, and in the New Connection dialog:

- Select Remote Process.
- Enter
<your-host-name>:20000
in the connection field.
Click Connect, and you should see your remote Java application’s management interface.
🔒 Note: Always try to have the latest JDK in the terminal and then try to open the jconsole, so that you will always have the latest jconsole.
If you need to enhance the security of your remote JMX deployment, you can include the following JVM arguments:
-Djava.rmi.server.hostname=localhost \
-Dcom.sun.management.jmxremote.local.only=true \
-Djava.rmi.server.useLocalHostname=true
Explanation:
- -Djava.rmi.server.hostname=localhost: This ensures the RMI server binds to
localhost
, restricting access to the local machine and blocking external connections for improved security. - -Dcom.sun.management.jmxremote.local.only=true: This blocks all remote access, ensuring that JMX operations can only be performed from the host running the Java application
- -Djava.rmi.server.useLocalHostname=true: This forces the RMI server to use the local hostname, ensuring compatibility with localhost-only environments and securing JMX access.
However, with the above options, connecting directly to the remote JMX via the jconsole UI will not be possible.
In this case, you will need to use SSH port forwarding to securely tunnel the remote JMX port (20000) to a local port on your machine. You can achieve this by running the following command:
ssh your-username@your-remote-machine-ip -L 20000:localhost:20000
Explantion:
ssh
: Starts the SSH session.-L 20000:localhost:20000
: Forwards the local port 20000 to the remote port 20000.- Replace
your-username
with your SSH username andremote-machine-ip
with the remote machine's IP address.
Now you can connect the same way as mentioned in the Step 3: Launch `jconsole` Locally above.
Troubleshooting Tips
- Cannot Connect to JMX: Double-check the
bin/setenv.sh
configuration and ensure the remote firewall rules are correctly set. - SSH Tunneling Issues: Verify that your SSH session is active and correctly configured.
- Port Conflicts: If port
20000
is in use on your local machine, choose a different local port for forwarding (e.g.,-L 20001:localhost:20000
).
With these steps, you can successfully monitor and manage your remote Java application using JMX while maintaining security and simplicity. 🎉