-1

I have a cloned VirtualBox machine that I've asked to clear MAC address after cloning. The machine name was changed from "node01" to "new-host-7.home" and I changed to "node02" by using conventional methods (hostname, change in traditional files, etc...). All seems to be changed until my Java program get the old machine name...

My /etc/hosts file:

127.0.0.1       node02.home     node02  localhost
192.168.25.25   node01.home     node01
192.168.25.27   node03.home     node03

My /etc/hostname file:

node02

My prompt:

root@node02:/

But when I start a Java program that ask for the machine's name, I'm getting the old machine name:

new-host-7.home

Here is how I got this name:

InetAddress ip = getFirstNonLoopbackAddress(true, false);
this.machineName = ip.getCanonicalHostName();

The method:

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;
} 

I don't know if AskUbuntu is the better place for this question or StackOverflow.

TIA.

EDIT:

ip -o a

1: lo    inet 127.0.0.1/8 scope host lo\       valid_lft forever preferred_lft forever
2: enp0s3    inet 192.168.25.26/24 brd 192.168.25.255 scope global enp0s3\       valid_lft forever preferred_lft forever

getent hosts

127.0.0.1       node02.home node02 localhost
192.168.25.25   node01.home node01
192.168.25.27   node03.home node03
127.0.0.1       localhost ip6-localhost ip6-loopback
  • This should probably be asked on Stack Overflow. Anyway, look at the IPs you have (ip -o a) and run getent hosts on them. – muru Apr 17 '16 at 19:15
  • I meant, run getent hosts with the IP as argument: getent hosts 192.168.25.26. – muru Apr 17 '16 at 19:29
  • @NGRhodes please point where this is a duplicate. Am I asking how to change the host name? – Magno C Apr 17 '16 at 19:30
  • 1
    @MagnoC there is no points to be gained from suggesting duplicates. It's just a standard behavior. My suspicion is that the hostname remains set in the kernel during runtime, and changes only at reboot. Check /proc/sys/kernel/hostname file after changing the hostname. It should keep the old hostname. I suspect that's where your java program gets the old hostname. I've run strace on the hostname command and /etc/hosts is not being opened at all, hence - it's a kernel option – Sergiy Kolodyazhnyy Apr 17 '16 at 19:44
  • 1
    @magnoC, before firing off insults you should of read the marked duplicate and the marked correct answer which confirmed you have set the correct locations and an alternate method of how to set with and without a reboot which you could of used for debugging your situation. – NGRhodes Apr 17 '16 at 20:01
2

Based on the updates to the initial question, you can see the IP command is returning 192.168.25.26 as the IP address in use, yet you have not defined a hostname on this ip address.

/etc/hosts needs the following entry:

192.168.25.26 node02.home

After a restart you should see the DNS on your network update and your Java code return the correct hostname.

  • Makes sense. Without the own machine external IP in the hosts file, Java will ask it to the DNS server since I'm looking for getFirstNonLoopbackAddress...i'll try this ASAP. – Magno C Apr 18 '16 at 20:54
  • Bingo! Right in the point. – Magno C Apr 18 '16 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.