JDK-4435662 : InetAddress.getLocalHost() gives 127.0.0.1 under RedHat with DHCP
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.3.0,5.0u4
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux,solaris_10
  • CPU: x86,sparc
  • Submitted: 2001-04-09
  • Updated: 2001-04-17
  • Resolved: 2001-04-17
Related Reports
Duplicate :  
Description

Name: boT120536			Date: 04/09/2001


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)


There doesn't seem to be a way to get the current IP address
of my box with InetAddress. Calling InetAddress.getLocalHost()
always gives 127.0.0.1. I'm running RedHat 7.0 and we use
DHCP here.

Note that this is not the same bug as that reported for
Solaris which can be worked around by modifying the /etc/hosts
file.

 
Sorry for the late reply. Here's some code to
reproduce this issue:


import java.net.*;

public class TestHost
{
    public static void main (String[] arg) 
    throws Exception
    {
        InetAddress local =InetAddress.getLocalHost();
        System.out.println ("local = " + local);
        InetAddress[] hosts = InetAddress.getAllByName("localhost");
        for (int i = 0; i < hosts.length; i++) {
            System.out.println ("host " + i + ": " + hosts[i]);
        }

        ServerSocket socket = new ServerSocket (8907);
        System.out.println ("socket at " +socket.getInetAddress());
        
    }
}


The output I get is:

local = js/127.0.0.1
host 0: localhost/127.0.0.1
socket at 0.0.0.0/0.0.0.0

This is what ifconfig reports:

eth0      Link encap:Ethernet  HWaddr00:10:A4:93:05:E5  
          inet addr:192.168.0.14  Bcast:192.168.0.255 Mask:255.255.255.0
          UP BROADCAST RUNNING  MTU:1500  Metric:1
          RX packets:579 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:245 dropped:0 overruns:0 carrier:245
          collisions:0 txqueuelen:100 
          Interrupt:11 Base address:0x200 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:3924  Metric:1
          RX packets:64 errors:0 dropped:0 overruns:0 frame:0
          TX packets:64 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 

Again, the system is RedHat 7.
(Review ID: 119763) 
======================================================================

Comments
EVALUATION RedHat creates /etc/hosts so that the hostname is mapped to the loopback address. You can fix this by edit /etc/hosts or alternatively updating /etc/nsswitch.conf to query DNS beforing querying /etc/hosts. See also 4424859 and 4408075. In merlin there is a new class java.net.NetworkInterface that can be used to enumerate all interfaces and the InetAddresses bound to each interface. alan.bateman@ireland 2001-04-10
10-04-2001