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.
[hao.xie@hao-xie-vm ~]$ hostname #hostname返回当前值 hao-xie-vm [hao.xie@vhao-xie-vm ~]$ more /proc/sys/kernel/hostname #当前值,或者说修改后生效 hao-xie-vm [hao.xie@hao-xie-vm ~]$ more /etc/sysconfig/network #这个文件需要重点说下,reboot的时候,系统会读取它,并给hostname赋值,但是在运行过程中修改它不会立即生效,需要配合hostname命令生效 # Created by anaconda
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.
描述很清楚,就是再通过ip反向来查hostname,原因和上面一样。
我遇到过的问题
我之前在服务器上发现hostname返回的是A,但是InetAddress.getLocalHost.getHostName返回异常unknown host name : host name B。原因就是2次dns的结果差异造成hostname A -> ip1;ip1 -> B这种尴尬的事情。这个时候就需要nslookup工具来检查,最后在dns服务器上进行修改了。