Reputation: 2196
Please refer to this question:
As you can see, I'm using this method to get the machine name:
private InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6) throws SocketException {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface i = en.nextElement();
for (Enumeration<InetAddress> en2 = i.getInetAddresses(); en2.hasMoreElements();) {
InetAddress addr = en2.nextElement();
if (!addr.isLoopbackAddress()) {
if (addr instanceof Inet4Address) {
if (preferIPv6) {
continue;
}
return addr;
}
if (addr instanceof Inet6Address) {
if (preferIpv4) {
continue;
}
return addr;
}
}
}
}
return null;
}
The caller:
InetAddress ip = getFirstNonLoopbackAddress(true, false);
this.machineName = ip.getCanonicalHostName();
But the result is I'm getting the old machine name, before I change it in Ubuntu.
How can I get the real machine/host name as in /etc/hostname file?
Upvotes: 0
Views: 633
Reputation: 899
The dns always do cache of names. You should do a flush dns in the OS that you are using; On Ubuntu I found this about how to flush the dns:
https://askubuntu.com/questions/414826/how-to-flush-dns-in-ubuntu-12-04
Or try this approach: Flush DNS using Java
Upvotes: 2