How to Force Tomcat 6.x to Bind to IPv4 Address on Ubuntu: Solutions and Configuration Examples


8 views

Many developers encounter unexpected IPv6 binding behavior when running Tomcat on Linux systems. The symptom appears when executing:

lsof -i :8080

Which shows output like:

java    12345 user   46u  IPv6  0xabc123      0t0  TCP *:webcache (LISTEN)

For Tomcat 6.x on Ubuntu, here are the most effective approaches:

Method 1: Java System Property

Create or modify setenv.sh in Tomcat's bin directory:

#!/bin/sh
export JAVA_OPTS="-Djava.net.preferIPv4Stack=true"

Make it executable:

chmod +x bin/setenv.sh

Method 2: Connector Configuration

In conf/server.xml, modify the connector:

<Connector port="8080" protocol="HTTP/1.1" 
           address="0.0.0.0"
           connectionTimeout="20000" 
           redirectPort="8443" />

Method 3: System-wide Java Preference

For persistent configuration, edit /etc/environment:

JAVA_OPTS="-Djava.net.preferIPv4Stack=true"

After applying changes, verify with:

ps aux | grep tomcat
# Should show JVM args containing IPv4 preference
lsof -i :8080
# Should now show IPv4 binding
  • For Ubuntu 11.10, ensure setenv.sh permissions allow Tomcat user execution
  • When using init.d scripts, modify CATALINA_OPTS instead of JAVA_OPTS
  • The order of precedence is: connector address > JVM argument > system default

IPv6 binding can cause issues with:

  • Firewall rules configured for IPv4 only
  • Network equipment not properly handling IPv6
  • Monitoring tools expecting IPv4 traffic

When running Apache Tomcat 6.x on Ubuntu 11.10, you might encounter a situation where the server binds to IPv6 by default even when you want IPv4. This manifests when checking listening ports:

lsof -i :8080
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    1234 tomcat   46u  IPv6  12345      0t0  TCP *:http-alt (LISTEN)

Java's networking stack prefers IPv6 when available in modern operating systems. While IPv6 is generally preferred, some legacy systems or network configurations require explicit IPv4 binding.

Here are three proven methods to force IPv4 binding:

1. Java System Property

The most reliable solution is to set the JVM preference for IPv4. Create or modify setenv.sh in Tomcat's bin directory:

#!/bin/sh
export JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true"

If setenv.sh doesn't exist, create it with executable permissions:

touch $CATALINA_HOME/bin/setenv.sh
chmod +x $CATALINA_HOME/bin/setenv.sh

2. Connector Configuration

Modify your server.xml to explicitly specify the IPv4 address:

<Connector port="8080" protocol="HTTP/1.1"
           address="0.0.0.0"
           connectionTimeout="20000"
           redirectPort="8443" />

3. Global Java Preferences

For system-wide configuration, edit /etc/default/tomcat6 (Ubuntu-specific):

JAVA_OPTS="-Djava.net.preferIPv4Stack=true"

After applying any of these changes and restarting Tomcat, verify the binding:

netstat -tulpn | grep java
# Should show 0.0.0.0:8080 instead of :::8080

If you need both IPv4 and IPv6 support, use this alternative approach:

JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv6Addresses=false"

This maintains IPv6 capability while preferring IPv4 address binding when possible.