Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does InetAddress.getLocalHost().getHostName() return a value different from bash "hostname"?

I've got a build.gradle task that works like a champ on my dev box at producing a properties file that records the name of the machine that the build was generated on. The logic is simple enough...

def hostname = InetAddress.getLocalHost().getHostName();

On my dev box this always produces the same value as if I did hostname from the bash shell.

bobk-mbp:DM_Server bobk$ hostname
bobk-mbp.local

On our jenkins CI server, however, bash hostname returns one thing, but my call to InetAddress.getLocalHost().getHostName(); returns something else. What needs to change on the jenkins machine to get these two returning the same value?

like image 668
Bob Kuhar Avatar asked Jun 21 '12 17:06

Bob Kuhar


People also ask

What does InetAddress getLocalHost() return?

getLocalHost. Returns the address of the local host. This is achieved by retrieving the name of the host from the system, then resolving that name into an InetAddress . Note: The resolved address may be cached for a short period of time.

How does InetAddress getLocalHost work?

The getLocalHost() method of Java InetAddress class returns the instance of InetAddress containing local host name and address. In this, firstly the host name is retrieved from the system, then that name is resolved into InetAddress.

How do I find my hostname in InetAddress?

The getHostName() method Java InetAddress returns the host name of a corresponding IP address. If this InetAddress was created with a host name, this host name will be remembered and returned else a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service.

What is Inet address in Java?

The java. net. InetAddress class is Java's encapsulation of an IP address. It is used by most of the other networking classes, including Socket , ServerSocket , URL , DatagramSocket , DatagramPacket , and more. public final class InetAddress extends Object implements Serializable.

What is gethostname () method in Java inetaddress?

The getHostName () method Java InetAddress returns the host name of a corresponding IP address. If this InetAddress was created with a host name, this host name will be remembered and returned else a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service.

How to get local host name in Java inetaddress?

Java InetAddress getLocalHost() method. The getLocalHost() method of Java InetAddress class returns the instance of InetAddress containing local host name and address. In this, firstly the host name is retrieved from the system, then that name is resolved into InetAddress. Syntax:

Why is my inetaddress returning a different host name?

If this InetAddress was created with a host name, this host name will be remembered and returned else a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service.

What is the use of getlocalhost() method in Java?

The getLocalHost() method of Java InetAddress class returns the instance of InetAddress containing local host name and address. In this, firstly the host name is retrieved from the system, then that name is resolved into InetAddress. It returns the instance of the local host.


2 Answers

Assuming you're on linux, the hostname command executed from the o/s returns the kernel's configured hostname.

InetAddress.getHostName() is doing a reverse lookup on the server's IP address using the naming service (DNS) configured in your O/S.

If you need the hostname as understood by the o/s, getting it from an environment variable via System.getenv may be the simplest option. It isn't a completely robust way to do this but it may be enough without needing to delve into network or system admin.

like image 58
Brian Smith Avatar answered Sep 23 '22 22:09

Brian Smith


From the API documentation for InetAddress.getHostName();

If this InetAddress was created with a host name, this host name will be remembered and returned; otherwise, a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service. If a lookup of the name service is required, call getCanonicalHostName.

So you may need to configure the DNS on the Jenkins server. The easiest way to do this is to edit /etc/hosts (I'm assuming your Jenkins runs on Linux) and make sure it looks like this:

127.0.0.1           localhost       localhost.localdomain
<public IP address> <hostname>      <hostname>.<domain>
like image 40
gareth_bowles Avatar answered Sep 20 '22 22:09

gareth_bowles