Thursday, 12 June 2014

More on Java and Canonical Hostnames

Following up on my earlier post: -


I've refined my Java class - hostStuff.java: -

import java.net.InetAddress;
import java.net.UnknownHostException;

public class hostStuff
{
public static void main(String[] args)
{

try
{
InetAddress address = InetAddress.getLocalHost();
System.out.println("My IP address ( via InetAddress.getLocalHost() ) is " + address.toString());
System.out.println("My hostname ( via InetAddress.getHostName() ) is " + address.getHostName());
System.out.println("My hostname ( via InetAddress.getCanonicalHostname() ) is  " + address.getCanonicalHostName());
}
catch (UnknownHostException e)
{
       System.out.println("I'm sorry. I don't know my own name.");
}
}
}


The class does the following things: -

Get the localhost's IP address InetAddress address = InetAddress.getLocalHost();
Report the IP address System.out.println("My IP address ( via InetAddress.getLocalHost() ) is " + address.toString());
Get the hostname for this IP address System.out.println("My hostname ( via InetAddress.getHostName() ) is " + address.getHostName());
Get the Canonical ( fully qualified ) hostname for this IP address System.out.println("My hostname ( via InetAddress.getCanonicalHostname() ) is  " + address.getCanonicalHostName());

My next trick is to write a class that iterates through ALL of the known IP addresses ( many boxes have multiple Network Interface Cards and, therefore, many addresses ) and report on the hostnames for each.

The rationale behind this is where one configures Kerberos/SPNEGO using the Canonical hostname of a server - it's important to know WHAT the fully qualified hostname is, and to ensure that DNS can return a valid hostname for an IP address, as well as a valid IP address for a hostname.

Here's some examples: -

Red Hat Enterprise Linux running on IBM PureApplication System

$ java hostStuff
My IP address ( via InetAddress.getLocalHost() ) is vm-004-039/10.34.4.39
My hostname ( via InetAddress.getHostName() ) is vm-004-039
My hostname ( via InetAddress.getCanonicalHostname() ) is  vm-004-039.hurpas01.ipas.hursley.ibm.com

Mac OS X

$ java hostStuff
My IP address ( via InetAddress.getLocalHost() ) is DMHMBP.local/10.61.32.78
My hostname ( via InetAddress.getHostName() ) is DMHMBP.local
My hostname ( via InetAddress.getCanonicalHostname() ) is  10.61.32.78


Red Hat Enterprise Linux running on VMware

$ java hostStuff
My IP address ( via InetAddress.getLocalHost() ) is rhel6.uk.ibm.com/127.0.0.1
My hostname ( via InetAddress.getHostName() ) is rhel6.uk.ibm.com
My hostname ( via InetAddress.getCanonicalHostname() ) is  localhost

Of the three, only the IPAS environment is "properly" configured, in that InetAddress.getCanonicalHostname() returns the fully qualified host/domain name of the server.

No comments: