3

I've got a few host that return an unqualified name for InetAddress.getLocalHost().getCanonicalHostName()(Documented here) e.g. "foo" instead of "foo.example.com". What could cause this and how could I fix it?

Running "hostname -f" on the command line returns the FQDN and nslookup on the short name also returns the FQDN.

CentOS 7.7.1908

JRE 1.8.0_231-b11

Z4-tier
  • 7,287
  • 3
  • 26
  • 42
dlipofsky
  • 289
  • 1
  • 4
  • 18
  • DNS misconfiguration. – user207421 Jan 31 '20 at 23:30
  • What do you mean by DNS misconfiguration? Running nslookup with the shortname returns the FQDN on both the good and bad hosts. – dlipofsky Feb 03 '20 at 16:28
  • 2
    Documentation says getCanonicalHostName() Gets the fully qualified domain name for this IP address. Best effort method, meaning we may not be able to return the FQDN depending on the underlying system configuration. Says a bit more https://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#getCanonicalHostName() and if that's not enough there's plenty more here https://unix.stackexchange.com/questions/186859/understand-hostname-and-etc-hosts – Tony Kennah Feb 08 '20 at 16:25
  • 1
    Have you tried to step debug the implementation in JDK of `getCanonicalHostName`? – Jaec Feb 11 '20 at 03:21

3 Answers3

0

Can you paste contents of /etc/hosts file?

or the output of cat /etc/hosts | grep localhost

The issue is most likely there, as it does not have a FQDN defined for 127.0.0.1

Gireesh
  • 101
  • 1
  • 2
  • It does not have a FQDN defined for 127.0.0.1, but neither do any of my other hosts and they work fine. – dlipofsky Feb 05 '20 at 18:50
0

It's likely your machine's configuration. Check that DNS for the domain resolves properly and that there is a domain set.

Java will either lookup in /etc/hosts or use you OS facilities to do the name address to name resolution.

Since you're looking for localhost, you may need a host specific mechanism to resolve it.

Also your machine likely has many interfaces, and not all of them may resolve to the same FQDN.

It may help to enumerate all network interfaces to see what's wrong:

        final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while(networkInterfaces.hasMoreElements())
        {
            final NetworkInterface iface = networkInterfaces.nextElement();
            System.out.println("Interface: " + iface.getDisplayName());
            final Enumeration<InetAddress> inetAddresses = iface.getInetAddresses();
            while (inetAddresses.hasMoreElements())
            {
                final InetAddress addr = inetAddresses.nextElement();
                System.out.println(addr.getCanonicalHostName());
            }
        }
juancn
  • 2,483
  • 2
  • 20
  • 25
0

getCanonicalHostName() will return a short name for an IP address if there is no reverse lookup zone or no PTR record for the host name in the reverse lookup zone.

helvete
  • 2,455
  • 13
  • 33
  • 37
Marcus
  • 1