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

前端 未结 2 1152
生来不讨喜
生来不讨喜 2020-12-18 22:37

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

相关标签:
2条回答
  • 2020-12-18 23:11

    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.

    0 讨论(0)
  • 2020-12-18 23:24

    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>
    
    0 讨论(0)
提交回复
热议问题