How do I restrict Apache Tomcat to listen only IPv4 address?

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux
  • Apache Tomcat

Issue

  • How do I restrict Tomcat to listen only on ipv4 address ?

  • By default Tomcat listens only on IPv6 addresses. Following netstat output shows Tomcat is listening only on IPv6 address.

      tcp        0      0 :::8009   :::*   LISTEN      91         8474899    29185/java          off (0.00/0/0)
      tcp        0      0 :::8080   :::*   LISTEN      91         8474862    29185/java          off (0.00/0/0)
    

Resolution

The -Djava.net.preferIPv4Stack=true JVM startup option can be used to force IPv4 stack to be used by the JVM.

This can be done by adding -Djava.net.preferIPv4Stack=true as a parameter for JAVA_OPTS for Tomcat. Please see this solution on how to configure Tomcat JVM startup options: How to set the JVM options for Tomcat on Linux

Make sure to restart the Apache Tomcat service after adding the JVM option.

Root Cause

As a default, if IPV6 support enabled in kernel,the Java Runtime will use IPV6 sockets to communicate.

Category
Tags

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.